GET请求适用于HttpWebRequest,Postman。但不是在HttpClient或RestSharp中

时间:2018-04-22 15:53:36

标签: c# httpwebrequest dotnet-httpclient

以下是我的要求

GET /api/v3/countries HTTP/1.1
Host: api-sandbox.grnconnect.com
Content-Type: application/json
Accept: application/json
api-key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX

我在邮差中尝试过,它正在运作。 此外,它使用HttpWebRequest使用以下代码。

var httpWebRequest = (HttpWebRequest) WebRequest.Create(url);
httpWebRequest.Headers.Clear();
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
httpWebRequest.Headers.Add("api-key", "XXXXXXXXXXXXXXXXXXX");
httpWebRequest.Accept = "application/json";
string result = ""

using(HttpWebResponse response = httpWebRequest.GetResponse() as HttpWebResponse) {
    if (response.StatusCode != HttpStatusCode.OK) throw new Exception(String.Format("Server error (HTTP {0}: {1}).", response.StatusCode, response.StatusDescription));
    Stream stream1 = response.GetResponseStream();
    StreamReader sr = new StreamReader(stream1);
    result = sr.ReadToEnd();
}
return result;

但是当我尝试使用httpclient时,它会使用Code 400响应,"不支持的媒体类型"。 以下是代码。

string response = "";
using(var httpClient = new HttpClient()) {
httpClient.DefaultRequestHeaders.Clear();
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("ContentType", "application/json; charset=utf-8");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("api-key", "XXXXXXXXXXXXXXXXXXXXX");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json");

response = httpClient.GetAsync(url).Result.
Content.
ReadAsStringAsync().Result;
}
return response;
}

还有HttpClient.SendAsync

string response = "";
using(var httpClient = new HttpClient()) {
var requestMessage = new HttpRequestMessage(HttpMethod.Post, url);
requestMessage.Headers.Add("Accept", "application/json");
requestMessage.Headers.Add("ContentType", "application/json");
requestMessage.Headers.Add("api-key", "XXXXXXXXXXXXXXXXXXXXXXXX");
response = httpClient.SendAsync(requestMessage)
.Result.Content.ReadAsStringAsync().Result;
}
return response;
}

使用Microsoft网络监视器分析网络流量。 两者看起来都相同

的WebRequest

- Tcp: Flags=...AP..., SrcPort=5403, DstPort=HTTP(80), PayloadLen=193, 
Seq=1190693031 - 1190693224, Ack=2045565534, Win=68 (scale factor 0x8) = 17408
....
....
- Http: Request, GET /api/v3/countries 
Command: GET
- URI: /api/v3/countries
 Location: /api/v3/countries 
ProtocolVersion: HTTP/1.1
- ContentType:  application/json
 MediaType:  application/json
api-key:  XXXXXXXXXXXXXXXXXX
Accept:  application/json
Host:  api-sandbox.grnconnect.com
Connection:  Keep-Alive
HeaderEnd: CRLF

HttpClient的

- Tcp: Flags=...AP..., SrcPort=5390, DstPort=HTTP(80), PayloadLen=192, 
Seq=3916100376 - 3916100568, Ack=2674317805, Win=256 (scale factor 0x8) = 
65536
.....
.....
- Http: Request, GET /api/v3/countries 
Command: GET
- URI: /api/v3/countries
 Location: /api/v3/countries 
ProtocolVersion: HTTP/1.1
- ContentType:  application/json
 MediaType:  application/json
api-key:  XXXXXXXXXXXXXXXXXXXXXX
Accept:  application/json
Host:  api-sandbox.grnconnect.com
Connection:  Keep-Alive
HeaderEnd: CRLF

唯一不同的是PayloadLen

如何解决?

我也试过Restharp Code。但仍然是400错误

var client = new RestClient("http://api- 
sandbox.grnconnect.com/api/v3/countries");
var request = new RestRequest(Method.GET);
request.AddHeader("api-key", "XXXXXXXXXXXXXX");
request.AddHeader("accept", "application/json");
request.AddHeader("content-type", "application/json");
IRestResponse response = client.Execute(request);

1 个答案:

答案 0 :(得分:0)

使用MediaType标头值

设置httpclient
x <- 121
sub("(?<=[1-9]|1[0-2])(?=\\d{2})", "-", x, perl=TRUE)

[1] "1-21"