C#HttpWebRequest.GetResponse编码URL

时间:2015-10-16 22:20:39

标签: c# httpwebrequest

我正在使用带有未编码的URL的C#HttpWebRequest.GetResponse。但是当我使用fiddler检查请求时,我发现URL已被编码。我想确认HttpWebRequest Class是在内部编码URL但在文档中找不到它。有人能指出我可以找到这个或其他方式验证它的文档吗?

  • KR

3 个答案:

答案 0 :(得分:2)

这是:

WebRequest.Create(您正在使用该方法创建HttpWebRequest对象,对吧?)" Create方法使用requestUriString参数创建一个Uri实例,并将其传递给新的WebRequest"
https://msdn.microsoft.com/en-us/library/bw00b1dc(v=vs.110).aspx

Uri的构造函数(由Create方法调用)表示它"解析URI,将其置于规范格式,并进行任何必需的转义编码 "
https://msdn.microsoft.com/en-us/library/z6c2z492(v=vs.110).aspx

答案 1 :(得分:1)

//端点是url,方法可以发布或获取...以下内容将帮助您从服务器中捕获任何错误..

使它成为一个功能,因此您可以轻松地在任何您想要的地方使用...

   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);
        }

答案 2 :(得分:0)

或者你想要类,你想用来创建api lib然后只需复制粘贴这个类..这是用于c#但几乎是java的similer语法......

这个你可以访问任何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
        }
    }
}