httpwebrequest.headers
时间: 2023-04-27 16:01:29 浏览: 74
httpwebrequest.headers是一个属性,它表示HTTP请求头。HTTP请求头是在HTTP请求中发送的一些元数据,包括请求的方法、URL、协议版本、主机名、用户代理、内容类型、内容长度等信息。在使用HttpWebRequest类发送HTTP请求时,可以通过设置该属性来添加自定义的HTTP请求头。
相关问题
HttpWebRequest.Headers.Add 的value 传中文报错
在使用 `HttpWebRequest.Headers.Add` 方法时,如果要传入中文字符串,需要先将中文字符串使用 `System.Text.Encoding` 进行编码,再将编码后的字符串作为value传入。常用的编码方式有 UTF-8、GB2312 等,具体使用哪种编码方式需要根据你的实际情况来确定。
示例代码如下:
```
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";
// 将中文字符串编码为 UTF-8 格式
byte[] bytes = Encoding.UTF8.GetBytes("中文字符串");
// 将编码后的字符串作为 value 传入 Headers
request.Headers.Add("HeaderName", bytes);
using (var stream = request.GetRequestStream())
{
// 在请求体中添加数据
}
var response = (HttpWebResponse)request.GetResponse();
```
这样就能够避免传入中文字符串时报错了。
public static T Post<T>(string url, string data, string staffId, string contentType) { try { byte[] bytes = Encoding.UTF8.GetBytes(data); HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); string timeStamp = ApiHelper.GetTimeStamp(); string random = ApiHelper.GetRandom(); httpWebRequest.Headers.Add("staffid", staffId.ToString()); httpWebRequest.Headers.Add("timestamp", timeStamp); httpWebRequest.Headers.Add("nonce", random); //ResultMsg signToken = ApiHelper.GetToken(url, staffId); // httpWebRequest.Headers.Add("signature", ApiHelper.GetSignature(timeStamp, random, staffId, data, signToken)); httpWebRequest.Headers.Add("jwtcookie", GetJwt(url, data));//cwj 直接通过jwt验证身份 httpWebRequest.Method = "POST"; httpWebRequest.ContentLength = (long)bytes.Length; httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"; httpWebRequest.ContentType = contentType; Stream requestStream = httpWebRequest.GetRequestStream(); requestStream.Write(bytes, 0, bytes.Length); httpWebRequest.Timeout = 300000; httpWebRequest.Headers.Set("Pragma", "no-cache"); HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); Stream responseStream = httpWebResponse.GetResponseStream(); StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8); string text = streamReader.ReadToEnd(); requestStream.Close(); streamReader.Close(); responseStream.Close(); httpWebRequest.Abort(); httpWebResponse.Close(); return JsonConvert.DeserializeObject<T>(text); } catch (Exception) { throw; } }
这段代码是用于进行 HTTP POST 请求的,其中包括对请求头的设置,请求体的编码和发送,以及响应的解析和反序列化成对象。其中,请求头中包括了 staffid、timestamp、nonce(随机数)和 jwtcookie,这些都是用来验证身份和防止重复请求的。请求体中传入了 data 参数,表示要发送的数据。最后,将响应的文本反序列化成 T 类型的对象并返回。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)