C# HttpWebRequest.GetResponse 编码 URL

C# HttpWebRequest.GetResponse encoding URL

我正在使用 C# HttpWebRequest.GetResponse 和未编码的 URL。但是当我使用提琴手检查请求时,我发现 URL 已编码。我想确认 HttpWebRequest Class 正在对 URL 进行内部编码,但无法在文档中找到它。有人可以指点我可以找到这个或其他验证方法的文档吗?

这里是:

WebRequest.Create(您正在使用该方法创建 HttpWebRequest 对象,对吗?)说 "The Create method uses the requestUriString parameter to create a Uri instance that it passes to the new WebRequest"
https://msdn.microsoft.com/en-us/library/bw00b1dc(v=vs.110).aspx

Uri 的构造函数(由 Create 方法调用)表示 "parses the URI, puts it in canonical format, and makes any required escape encodings"
https://msdn.microsoft.com/en-us/library/z6c2z492(v=vs.110).aspx

//endpoint 是 url 方法可以是 post 或 get... 下面将帮助您捕获来自服务器的任何错误..

让它成为一个函数,这样你就可以很容易地在任何你想用的地方使用它......

   HttpWebRequest httpRequest = (HttpWebRequest)HttprequestObject(endpoints, method);
        using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
        {
            streamWriter.Write(jsonObject);
            streamWriter.Flush();
            streamWriter.Close();
        }
        var result = "";
        HttpWebResponse httpResponse;
        try
        {
            httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                result = streamReader.ReadToEnd();
            }
        }
        catch (WebException e)
        {
            Console.WriteLine("This program is expected to throw WebException on successful run." +
                                "\n\nException Message :" + e.Message);
            if (e.Status == WebExceptionStatus.ProtocolError)
            {
                Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
                Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
                using (Stream data = e.Response.GetResponseStream())
                using (var reader = new StreamReader(data))
                {
                    string text = reader.ReadToEnd();
                    Console.WriteLine(text);
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

或者你想要 class 并且你想使用它来创建 api lib 然后只需复制粘贴这个 class ..这是用于 c# 但几乎更相似的语法 java ...

有了这个你可以访问任何 api 或 url 或者也可以用来将 json 文件传递​​到服务器..

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ExternalAPIs_SynapsePay.Helpers
{
    class RESTHelpers
    {
        public dynamic APICalls(JObject jsonObject, string endpoints, string method)
        {
            HttpWebRequest httpRequest = (HttpWebRequest)HttprequestObject(endpoints, method);
            using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
            {
                streamWriter.Write(jsonObject);
                streamWriter.Flush();
                streamWriter.Close();
            }
            var result = "";
            HttpWebResponse httpResponse;
            try
            {
                httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    result = streamReader.ReadToEnd();
                }
            }
            catch (WebException e)
            {
                Console.WriteLine("This program is expected to throw WebException on successful run." +
                                    "\n\nException Message :" + e.Message);
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
                    Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
                    using (Stream data = e.Response.GetResponseStream())
                    using (var reader = new StreamReader(data))
                    {
                        string text = reader.ReadToEnd();
                        Console.WriteLine(text);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return result;
            //return "Success";
            //not sure what to return 
            //here i have to add sql server code to enter into database
        }

        public HttpWebRequest HttprequestObject(string endpoints, string method)
        {
            string url = Setting.API_TEST_VALUE + endpoints;
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = method;
            return httpWebRequest;
        }

        /// <summary>
        /// This method is useful when you have to pass JSON as string not as object... This is basically a constructor..
        /// Even if u pass json object it will accept, so no need to worry about that.
        /// </summary>
        /// <param name="ljson"></param>
        /// <param name="endpoints"></param>
        /// <param name="method"></param>
        /// <returns></returns>
        public dynamic APICalls(string jsonString, string endpoints, string method)
        {
            HttpWebRequest httpRequest = (HttpWebRequest)HttprequestObject(endpoints, method);
            using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
            {
                streamWriter.Write(jsonString);
                streamWriter.Flush();
                streamWriter.Close();
            }
            var result = "";
            HttpWebResponse httpResponse;
            try
            {
                httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    result = streamReader.ReadToEnd();
                }
            }
            catch (WebException e)
            {
                Console.WriteLine("This program is expected to throw WebException on successful run." +
                                    "\n\nException Message :" + e.Message);
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
                    Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
                    using (Stream data = e.Response.GetResponseStream())
                    using (var reader = new StreamReader(data))
                    {
                        string text = reader.ReadToEnd();
                        Console.WriteLine(text);
                        result = text;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return result;
            //return "Success";
            //not sure what to return 
            //here i have to add sql server code to enter into database
        }
        public void HttlpResponseObject(HttpWebRequest httpResponse)
        {
            var response = (HttpWebResponse)httpResponse.GetResponse();
            using (var streamReader = new StreamReader(response.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
            }
            //entry into databse can be done from here 
            //or it should return some value
        }
    }
}