C# 使用 HttpWebRequest 添加自定义标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9842539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add Custom Headers using HttpWebRequest
提问by Tarik
I am not really sure what type of headers these highlighted values are, but how should I add them using HttpWebRequest?
我不确定这些突出显示的值是什么类型的标头,但是我应该如何使用 HttpWebRequest 添加它们?


Is the highlighted part considered body of the http request or header data? In other words, which way is correct?
突出显示的部分是否被视为 http 请求或标头数据的主体?换句话说,哪种方式是正确的?
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);
采纳答案by Peter Aron Zentai
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.
第二种方式是正确的。
回答by SharK
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());
}
}
回答by Meet Shah
You should do ex.StackTraceinstead of ex.ToString()
你应该做ex.StackTrace而不是ex.ToString()

![C# 找不到与绑定 WebHttpBinding 的端点的方案 https 匹配的基地址。注册的基地址方案是 [http]](/res/img/loading.gif)