C# HttpWebRequest vs HttpClient vs RestSharp
Posted
技术标签:
【中文标题】C# HttpWebRequest vs HttpClient vs RestSharp【英文标题】: 【发布时间】:2021-10-23 13:54:07 【问题描述】:我正在尝试以 3 种不同的方式发送一个简单的发布请求,前两个以 400 Bad Request 失败,只有 RestSharp 有效。我会说请求的正文和标头以相同的方式设置,所以我不明白出了什么问题。
使用HttpWebRequest
var httpRequest = (HttpWebRequest) WebRequest.Create(uri);
httpRequest.Method = "POST";
httpRequest.Accept = "application/json";
httpRequest.Headers["Authorization"] = $"Bearer token";
httpRequest.Headers["Client_Id"] = clientId;
httpRequest.ContentType = "application/json";
using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
streamWriter.Write(json);
var httpResponse = (HttpWebResponse) httpRequest.GetResponse();
使用HttpClient
using (var client = new HttpClient())
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
client.DefaultRequestHeaders.Add("Client_Id", clientId);
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/json"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/x-json"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/javascript"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml"));
var content = new StringContent(json, Encoding.UTF8, "application/json");
var result = client.PostAsync(uri, content).Result;
Fiddler 中的原始文件
POST https://uri HTTP/1.1
Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
Content-Type: application/json
Client_Id: 123456
Authorization: Bearer token
Accept-Encoding: gzip, deflate
Host: uri
Content-Length: 314
Expect: 100-continue
Connection: Keep-Alive
使用RestClient
var restClient = new RestClient(uri);
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Client_Id", clientId);
request.AddHeader("Authorization", $"Bearer token");
request.AddParameter("application/json", json, ParameterType.RequestBody);
IRestResponse response = restClient.Execute(request);
Fiddler 中的原始文件
POST https://uri HTTP/1.1
Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
Content-Type: application/json
Client_Id: 123456
Authorization: Bearer token
User-Agent: RestSharp/106.12.0.0
Host: uri
Content-Length: 314
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
【问题讨论】:
用户提琴手来确定您的请求与任何未来问题之间的差异,尽管除了缺少内容类型之外,它们看起来都非常相似 我用来自 Fiddler 的 httpclient 和 restsharp 的原始数据更新了帖子 为什么标准客户端的标头中有Expect: 100-continue
?他们通常不会自动设置该标题。服务器无法理解该标头并导致错误。
【参考方案1】:
HttpClient 的一部分:StringContent 在 Content-Type 的末尾添加字符集值,例如 application/json; charset=utf-8
,因此可能会导致 400 错误。
您可以使用此代码content .Headers.ContentType.CharSet = "";
删除它
【讨论】:
试过了,没用,我用我从 Fiddler 获得的两个请求的原始数据更新了帖子以上是关于C# HttpWebRequest vs HttpClient vs RestSharp的主要内容,如果未能解决你的问题,请参考以下文章
使用 HttpWebRequest.Create 时的 C# vs VB 语法
c# HttpWebRequest 使用代理访问网页,在vs中release调试的时候可以,直接运行出错
C# HttpWebRequest - 如何确定是不是发生了 HTTP 301?