Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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
Http text/html vs application/x-www-form-urlencoded和FormUrlEncodedMediaTypeFormatter_Http_Rest_Oauth_Asp.net Web Api_Dotnet Httpclient - Fatal编程技术网

Http text/html vs application/x-www-form-urlencoded和FormUrlEncodedMediaTypeFormatter

Http text/html vs application/x-www-form-urlencoded和FormUrlEncodedMediaTypeFormatter,http,rest,oauth,asp.net-web-api,dotnet-httpclient,Http,Rest,Oauth,Asp.net Web Api,Dotnet Httpclient,对于POST请求,我得到了text/html格式的响应,响应正文包含以下信息: oauth_令牌=XXXXXXXXXXXXXXXXXXXXXXXXX&oauth_令牌=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX&oauth_回调_确认=true 我是通过System.Net.Http.HttpClient提出这个请求的,我认为我可以用FormUrlEncodedMediaTypeFormatter作为FormDataCollection读取响应,但结果是FormUrlEncode

对于POST请求,我得到了text/html格式的响应,响应正文包含以下信息:

oauth_令牌=XXXXXXXXXXXXXXXXXXXXXXXXX&oauth_令牌=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX&oauth_回调_确认=true

我是通过
System.Net.Http.HttpClient
提出这个请求的,我认为我可以用
FormUrlEncodedMediaTypeFormatter
作为
FormDataCollection
读取响应,但结果是
FormUrlEncodedMediaTypeFormatter
只支持应用程序/x-www-form-urlencoded格式违约因此,我用以下代码解决了这个问题:

using (OAuthHttpClient client = new OAuthHttpClient(creds)) {

    var response = await client.PostAsync(requestUri, new EmptyContent());
    var formatter = new FormUrlEncodedMediaTypeFormatter();
    formatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
    var result = await response.Content.ReadAsAsync<FormDataCollection>(new List<MediaTypeFormatter>() { formatter });

}
使用(OAuthHttpClient=new OAuthHttpClient(creds)){
var response=await client.PostAsync(requestUri,new EmptyContent());
var formatter=新FormUrlEncodedMediaTypeFormatter();
添加(新的MediaTypeHeaderValue(“text/html”);
var result=await response.Content.ReadAsAsync(新列表(){formatter});
}
这里的问题是:


响应提供者(在本例中是Twitter)是否错误地将此响应作为text/html发送,或者默认情况下应该
FormUrlEncodedMediaTypeFormatter
支持text/html类型?

您的问题缺少一些关键信息,即默认情况下requestUri应该返回什么,它是一个Web API服务还是一个外部服务等等。它似乎不是Web API,因为它返回“text/html”有点奇怪

但是FormUrlEncodedMediaTypeFormatter不支持从text/html返回格式的事实是绝对正确的。因为为什么会这样?“application/x-www-form-urlencoded”实际上是一个键值字典,text/html是一种富媒体类型

在WebAPI中,内容协商的工作方式

  • Mediatype映射(我假设在您的案例中没有)
  • 接受标题-查看您的请求时,您不会设置标题
  • 请求内容类型-同样,查看您的请求时,您没有将其设置为空
  • 格式化程序能否序列化给定类型

  • 因此,如果您按照向任何Web API操作显示的方式发出请求,它将返回text/xml(如果您没有手动调整conneg)。

    您的问题缺少一些关键信息,即默认情况下requestUri应该返回什么,是Web API服务还是外部服务等。它似乎不是Web API,因为它返回的结果有点奇怪“文本/html”

    但是FormUrlEncodedMediaTypeFormatter不支持从text/html返回格式的事实是绝对正确的。因为为什么会这样做?“application/x-www-form-urlencoded”实际上是一个键值字典,text/html是一种富媒体类型

    在WebAPI中,内容协商的工作方式

  • Mediatype映射(我假设在您的案例中没有)
  • 接受标题-查看您的请求时,您不会设置标题
  • 请求内容类型-同样,查看您的请求时,您没有将其设置为空
  • 格式化程序能否序列化给定类型

  • 因此,如果您按照向任何Web API操作显示的方式发出请求,它将返回text/xml(如果您没有手动调整conneg)。

    我同意Filip的说法,这是一个很好的解决错误内容类型标题的方法


    Henrik

    我同意Filip的观点,这是一个很好的解决不正确内容类型标题的方法



    Henrik

    你在帖子中的接受标题是什么?@AlexanderZeitler这是一个非常明智的评论。没有,我没有发送任何回复,但得到了回复。我会尽量明确,看看这次它给了我什么。顺便说一句,你不会得到
    应用程序/x-www-form-urlencoded
    ,因为
    FormUrlEncodedMediaTypeFormatter
    无法写入任何内容。通过查看@AlexanderZeitler,您可以看到它是只读的。我将它设置为
    application/json
    ,以查看它的行为,我得到了相同的响应。@AlexanderZeitler我不想让
    FormUrlEncodedMediaTypeFormatter
    写入任何内容。您在帖子中的Accept头是什么?@AlexanderZeitler,这是这是一个非常明智的评论。没有,我没有发送任何回复。我会尽量明确,看看这次回复给我什么。顺便说一句,你不会回复
    application/x-www-form-urlencoded
    ,因为
    FormUrlEncodedMediaTypeFormatter
    不能写任何东西。通过查看@AlexanderZeitler我将其设置为
    application/json
    以查看它的行为,我得到了相同的响应。@AlexanderZeitler我不想让
    FormUrlEncodedMediaTypeFormatter
    写任何东西。问题是关于
    FormUrlEncodedMediaTypeFormatter
    而不是关于conneg。响应来自Twitter OAuth服务器。因此,我包含了答案-但FormUrlEncodedMediaTypeFormatter不支持从text/html返回的格式这一事实是绝对正确的。因为为什么会这样?“application/x-www-form-urlencoded“实际上是一个键值字典,text/html是一种富媒体类型。我提到conneg是因为我不确定响应是来自Web API还是外部服务,我也这么认为。我认为响应提供者(Twitter)将此响应作为text/html.exatly发送是错误的。只是Twitter发送的响应格式为“application/x-www-form-urlencoded”,并错误地用“text/html”内容类型修饰。您的解决方法非常好。问题是关于
    FormUrlEncodedMediaTypeFormatter
    ,而不是关于conneg。响应来自TwitterOAuth服务器。因此我包含了答案——但FormUrlEncodedMediaTypeFormatter不支持从text/html返回格式这一事实是绝对正确的。因为为什么会这样?“application/x-www-form-urlencoded”实际上是一个关键问题