简体   繁体   English

使用 HttpWebRequest 添加自定义标头

[英]Add Custom Headers using HttpWebRequest

I am not really sure what type of headers these highlighted values are, but how should I add them using HttpWebRequest?我不确定这些突出显示的值是什么类型的标头,但是我应该如何使用 HttpWebRequest 添加它们?

HTTP 标头

Is the highlighted part considered body of the http request or header data?突出显示的部分是否被视为 http 请求或标头数据的主体? In other words, which way is correct?换句话说,哪种方式是正确的?

Here is the code I am currently using:这是我目前使用的代码:

HttpWebRequest request = (HttpWebRequest) WebRequest.Create("/securecontrol/reset/passwordreset");
request.Headers.Add("Authorization", "Basic asdadsasdas8586");
request.ContentType = "application/x-www-form-urlencoded";
request.Host = "www.xxxxxxxxxx.com";
request.Method = "POST";
request.Proxy = null;
request.Headers.Add("&command=requestnewpassword");
request.Headers.Add("&application=netconnect");

But should I use the following instead to build the Http Request above?但是我应该使用以下内容来构建上面的 Http 请求吗?

string reqString = "&command=requestnewpassword&application=netconnect";
byte[] requestData = Encoding.UTF8.GetBytes(reqString);

HttpWebRequest request = (HttpWebRequest) WebRequest.Create("/securecontrol/reset/passwordreset");
request.Headers.Add("Authorization", "Basic ashAHasd87asdHasdas");
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = requestData.Length;
request.Proxy = null;
request.Host = "www.xxxxxxxxxx.com";
request.Method = "POST";

using (Stream st = request.GetRequestStream())
st.Write(requestData, 0, requestData.Length);

A simple method of creating the service, adding headers and reading the JSON response,创建服务、添加标头和读取 JSON 响应的简单方法,

private static void WebRequest()
{
    const string WEBSERVICE_URL = "<<Web Service URL>>";
    try
    {
        var webRequest = System.Net.WebRequest.Create(WEBSERVICE_URL);
        if (webRequest != null)
        {
            webRequest.Method = "GET";
            webRequest.Timeout = 20000;
            webRequest.ContentType = "application/json";
            webRequest.Headers.Add("Authorization", "Basic dcmGV25hZFzc3VudDM6cGzdCdvQ=");
            using (System.IO.Stream s = webRequest.GetResponse().GetResponseStream())
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(s))
                {
                    var jsonResponse = sr.ReadToEnd();
                    Console.WriteLine(String.Format("Response: {0}", jsonResponse));
                }
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

IMHO it is considered as malformed header data.恕我直言,它被视为格式错误的标头数据。

You actually want to send those name value pairs as the request content (this is the way POST works) and not as headers .您实际上希望将这些名称值对作为请求内容(这是 POST 的工作方式)而不是作为 headers 发送

The second way is true.第二种方式是正确的。

你应该做ex.StackTrace而不是ex.ToString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM