无法对此处地图定位端点进行异步发布调用

当通过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;
  

状态码:415,原因短语:“不支持的媒体类型”,版本:1.1,   内容:System.Net.Http.HttpConnection + HttpConnectionResponseContent,   标头:{日期:2019年11月8日星期五13:38:37 GMT服务器:nginx-clojure   内容类型:application / json内容长度:114}

WebRequest

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;
    }
}
xiangxiangdetang 回答:无法对此处地图定位端点进行异步发布调用

我看到两个区别:

  1. 您正在Accept代码中设置HttpClient标头,而您不在WebRequest代码中。这定义了接受的数据类型。如果此API调用未返回JSON,则可能只是在说“那么我无话可说”。您可以尝试只删除整行。
  2. 您的Content-Type代码中的HttpClient将是application/json; charset=utf-8,而您将application/json代码中的WebRequest设置为charset。我不知道为什么Content-Type会令人窒息,但是如果更改#1不起作用,则可以尝试直接设置var content = new StringContent(""); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 并查看它是否有所不同:
class Webpage_validity:
  """Class to check if a website is indeed reachable
   for further scrapping
  """
  def __init__(self): #ensures  inheritability 
    pass

  @staticmethod
  def assessing_response_status(url_to_check,num_retries = 3,retry = 1):
    #check url using requests and return url checked if good

class Dynamic_response(Webdriver_preferences,Webpage_validity):
  """Class to generate dynamic content of a page by scrolling down
  """
  def __init__(self):
    Webdriver_preferences.__init__(self) #browser preferences
    Webpage_validity.__init__(self) 

  def scrolling_down(self,url_to_render):
    #code to scroll down to load dynamic content using url checked before
    #return response

class Scrapping(Dynamic_response):

  def __init__(self):
    super().__init__()

  def grab_url_links(self):
    #use the response from scrolling down,scrap url of href and return a list of urls for further use

  def get_elements(self):
    #by inheritage,re-use scrolling down (and hence,webpage validity to check status) 
    #to open all url from list of grabed url from "grab_url_links" and scrap more stuff

本文链接:https://www.f2er.com/3137560.html

大家都在问