简体   繁体   中英

GET request works in HttpWebRequest,Postman . but not in HttpClient or RestSharp

Following is my request

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

I tried it in postman , and it is working. Also, it works with following code using 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;

But When I try with httpclient it respond with Code 400 , "Unsupported media type". Following is the code.

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;
}

Also 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;
}

Analysed network traffic with Microsoft Network Monitor. both looks same

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

Only the difference is PayloadLen

How to resolve It?

I also tried with Restharp Code. but still same 400 error

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);

setting httpclient with MediaType header value

client.DefaultRequestHeaders.Accept
    .Add(new MediaTypeWithQualityHeaderValue("application/json"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM