Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 与此RestClient JSON POST等效的HttpClient JSON POST是什么?_C#_.net_Http_Dotnet Httpclient - Fatal编程技术网

C# 与此RestClient JSON POST等效的HttpClient JSON POST是什么?

C# 与此RestClient JSON POST等效的HttpClient JSON POST是什么?,c#,.net,http,dotnet-httpclient,C#,.net,Http,Dotnet Httpclient,鉴于: 以下RestClient代码生成服务器接受的HTTP流量 Uri location = ...; // Remote 3rd party HTTP Rest API string body = "SOME JSON"; 但是,下面的HttpClient代码必须生成不同的HTTP流量(由拒绝请求的服务器指示) 为什么HttpClient代码的序列化方式不同?找到精确差异的一种简单方法是运行Fiddler或其他调试代理并检查原始请求。以下是我从HttpClient获得的信息: using

鉴于:

以下
RestClient
代码生成服务器接受的HTTP流量

Uri location = ...; // Remote 3rd party HTTP Rest API
string body = "SOME JSON";
但是,下面的
HttpClient
代码必须生成不同的HTTP流量(由拒绝请求的服务器指示)


为什么HttpClient代码的序列化方式不同?

找到精确差异的一种简单方法是运行Fiddler或其他调试代理并检查原始请求。以下是我从HttpClient获得的信息:

using (var client = new HttpClient())
{
  var request = new HttpRequestMessage();
  request.Method = HttpMethod.Post;
  request.RequestUri = location;
  var bodyContent = new StringContent(body, Encoding.UTF8, "application/json");
  request.Content = bodyContent;
  request.Headers.Add("cache-control", "no-cache");
  client.Timeout = TimeSpan.FromMinutes(5.0);
  var response = await client.SendAsync(request);
}
而对于RestSharp:

POST http://example.com/ HTTP/1.1
cache-control: no-cache
Content-Type: application/json; charset=utf-8
Host: example.com
Content-Length: 4
Expect: 100-continue
Connection: Keep-Alive

test

根据您的系统配置、版本等,您的结果可能会有所不同,因此您应该亲自尝试以确保。

当您说服务器拒绝它时,需要更多详细信息。状态代码、正文等。您拥有服务器吗?它是在寻找一个特定的标题吗?您是否尝试过旋转本地api以查看请求是如何接收的?感谢您的反馈和投票。我重申了这个问题,以强调我正在尝试复制RestClient所表现出的行为(不一定要尝试获得一个工作的客户端/服务器API)。
POST http://example.com/ HTTP/1.1
cache-control: no-cache
Content-Type: application/json; charset=utf-8
Host: example.com
Content-Length: 4
Expect: 100-continue
Connection: Keep-Alive

test
POST http://example.com/ HTTP/1.1
cache-control: no-cache
Content-Type: application/json; charset=utf-8
Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
User-Agent: RestSharp/106.6.9.0
Host: example.com
Content-Length: 4
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

test