C# HttpWebRequest,如何发送应用程序/JSON内容类型的POST数据? HttpWebRequest请求=(HttpWebRequest)HttpWebRequest.Create(url); request.Method=“POST”; request.ContentType=“application/x-www-form-urlencoded;charset=utf-8”;

C# HttpWebRequest,如何发送应用程序/JSON内容类型的POST数据? HttpWebRequest请求=(HttpWebRequest)HttpWebRequest.Create(url); request.Method=“POST”; request.ContentType=“application/x-www-form-urlencoded;charset=utf-8”;,c#,httpwebrequest,yahoo-messenger,C#,Httpwebrequest,Yahoo Messenger,从Yahoo返回的POST数据(我使用Fiddler检查): {“error”:{“code”:-1003,“detail”:“Unsupported Content Type error”,“description”:“Unsupported Content Type error”},“code”:-1003} 我正在编写需要application/json的Yahoo Messanger客户端;内容类型为charset=utf-8,设置时: HttpWebRequest request =

从Yahoo返回的POST数据(我使用Fiddler检查):

{“error”:{“code”:-1003,“detail”:“Unsupported Content Type error”,“description”:“Unsupported Content Type error”},“code”:-1003}

我正在编写需要application/json的Yahoo Messanger客户端;内容类型为charset=utf-8,设置时:

HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded; charset=utf-8"; request.ContentType=“application/json;charset=utf-8”; 未发送POST数据,从Yahoo返回:

{“error”:{“code”:-1005,“detail”:“无效参数错误”,“description”:“无效参数错误”},“code”:-1005}

更新

我试图通过POST方法发送这两个值:presenceState&status

如支持的内容类型中所述,是应用程序/json
在我的代码中,如果我将内容类型设置为应用程序/json,HttpWebRequest不会通过POST发送这两个值。

根据您的错误,第一个失败,因为请求的内容类型与Yahoo期望的内容类型不匹配。这是有道理的,你的第二个例子正朝着正确的方向发展,但根据你的帖子,你似乎得到了回应。有了fiddler,你应该能够通过网站将你的帖子与适当的请求进行比较。这可能有助于查明哪里出了问题


但是不管怎样,我们需要更多地了解你在做什么,因为没有显示你文章的内容供我们查看。

我的错误可能与你的错误相同。通过将
presenceState
的类型更改为int类型而不是字符串类型,可以解决此问题

request.ContentType = "application/json; charset=utf-8";
我希望你能解决这个问题。

看看下面的例子

ClassLogon objLogon = new ClassLogon
  {
    presenceState = ***0***,
    presenceMessage = "I am logn"
  };
其中
CreateData

byte[] data = CreateData(value);
var requst = (HttpWebRequest) WebRequest.Create(uri);
requst.Method = "POST";
requst.ContentType = "application/json";
requst.ContentLength = data.Length;
using (Stream stream = requst.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}
公共静态字节[]创建(T值)
{
var serializer=newdatacontractjsonserializer(typeof(T));
使用(var stream=new MemoryStream())
{
serializer.WriteObject(流、值);
返回流ToArray();
}
}

我也在为同样的问题而挣扎。 如文档()中所述,POST请求中需要有一个空的主体({})

使用javascript和jQuery,我在帖子正文中发送了一个简单的空对象字符串,这样就可以了:

public static byte[] Create<T>(T value)
{
    var serializer = new DataContractJsonSerializer(typeof (T));
    using (var stream = new MemoryStream())
    {
        serializer.WriteObject(stream, value);
        return stream.ToArray();
    }
}

希望这能有所帮助。

我参加聚会已经迟到了,但我最终还是来这里想弄清楚我到底把什么搞砸了,所以也许这会对其他人有所帮助

如果在c#代码中设置了内容类型,然后添加了一些其他标题,如:

    $.ajax({
        type: 'POST',
        url: 'http://developer.messenger.yahooapis.com/v1/session',
        data: JSON.stringify({ }),
        processData: false,
        beforeSend: function(xhr) {
            xhr.setRequestHeader('Authorization', OAuth.getAuthorizationHeader("yahooapis.com", message.parameters));
            xhr.setRequestHeader('Content-Type','application/json; charset=UTF-8');
            xhr.setRequestHeader('X-Yahoo-Msgr-User-Agent','YahooMessenger/1.0 (yourapp; 1.0.0.1)')
        }});

实际上,当您向.ContentType写入时,它会在请求的WebHeaderCollection中设置内容类型。然后你继续覆盖它们。在添加一组自定义标题之前,这样设置的任何标题都可能发生这种情况。如果这是您正在做的,只需先设置自定义头,然后写入.whicher头。

您真的确定请求不会从客户端发出,而是在服务器上失败吗?使用Fiddler,从我的第一个代码yahoo服务器返回此JSON结果:{“error”:{“code”:-1003,“detail”:“Unsupported Content Type error”,“描述”:“不支持的内容类型错误”},“代码”:-1003}。和响应代码400(错误请求)。感谢您的重播,我编辑了我的问题,并添加了我正在做的更多信息。感谢您的重播,您是否使用HttpWebRequest以及您使用的设置?您可以共享您的代码吗?
httpWebRequest.ContentType = @"application/json; charset=utf-8";        
WebHeaderCollection headers = new WebHeaderCollection();
headers.Add("Authorization", "Bearer "+oAuthBearerToken);
httpWebRequest.Headers = headers;