无法对 Here Maps 定位端点进行异步 Post 调用
Unable to make a Async Post call to Here Maps positioning endpoint
当通过 Postman 和 C# WebRequest 测试调用时,它可以工作,但我无法使用带有 PostAsync 或 PostJsonAsync 调用的 HttpClient 执行相同的操作。
错误:不支持的媒体类型,尽管需要并应用 application/json。
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var content = new StringContent(data, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("https://pos.api.here.com/positioning/v1/locate?app_id={id}&app_code={code}", content);
return response;
StatusCode: 415, ReasonPhrase: 'Unsupported Media Type', Version: 1.1,
Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent,
Headers:{ Date: Fri, 08 Nov 2019 13:38:37 GMT Server: nginx-clojure
Content-Type: application/json Content-Length: 114}
网络请求
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
if (!string.IsNullOrEmpty(data))
{
request.ContentType = "application/json";
request.Method = "POST";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(data);
streamWriter.Flush();
streamWriter.Close();
}
}
using (HttpWebResponse webresponse = request.GetResponse() as HttpWebResponse)
{
using (StreamReader reader = new StreamReader(webresponse.GetResponseStream()))
{
string response = reader.ReadToEnd();
return response;
}
}
我看到了两个不同之处:
- 您正在
HttpClient
代码中设置 Accept
header,而您不在 WebRequest
代码中。这定义了 you 接受的数据类型。如果这个 API 调用没有 return JSON,那么它可能只是说 "I have nothing to say to you then"。您可以尝试删除整行。
- 您的
HttpClient
代码中的 Content-Type
将是 application/json; charset=utf-8
,而您在 WebRequest
代码中将其设置为 application/json
。我不明白为什么 charset
会使它窒息,但如果更改 #1 不起作用,您可以尝试直接设置 Content-Type
并查看它是否有任何不同:
var content = new StringContent("");
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
当通过 Postman 和 C# WebRequest 测试调用时,它可以工作,但我无法使用带有 PostAsync 或 PostJsonAsync 调用的 HttpClient 执行相同的操作。
错误:不支持的媒体类型,尽管需要并应用 application/json。
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var content = new StringContent(data, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("https://pos.api.here.com/positioning/v1/locate?app_id={id}&app_code={code}", content);
return response;
StatusCode: 415, ReasonPhrase: 'Unsupported Media Type', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:{ Date: Fri, 08 Nov 2019 13:38:37 GMT Server: nginx-clojure Content-Type: application/json Content-Length: 114}
网络请求
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
if (!string.IsNullOrEmpty(data))
{
request.ContentType = "application/json";
request.Method = "POST";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(data);
streamWriter.Flush();
streamWriter.Close();
}
}
using (HttpWebResponse webresponse = request.GetResponse() as HttpWebResponse)
{
using (StreamReader reader = new StreamReader(webresponse.GetResponseStream()))
{
string response = reader.ReadToEnd();
return response;
}
}
我看到了两个不同之处:
- 您正在
HttpClient
代码中设置Accept
header,而您不在WebRequest
代码中。这定义了 you 接受的数据类型。如果这个 API 调用没有 return JSON,那么它可能只是说 "I have nothing to say to you then"。您可以尝试删除整行。 - 您的
HttpClient
代码中的Content-Type
将是application/json; charset=utf-8
,而您在WebRequest
代码中将其设置为application/json
。我不明白为什么charset
会使它窒息,但如果更改 #1 不起作用,您可以尝试直接设置Content-Type
并查看它是否有任何不同:
var content = new StringContent("");
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");