我使用DotNetOpenAuth并试图张贴JSON对象到服务器。
服务器抛出WebContentType =原始错误。
授权:OAuth的
oauth_token="V9vVXD51ehUU6WmY%2FQ41qta0RfY%3D",oauth_consumer_key="CHiawRiAb299cOas",oauth_nonce="BBTM4csg",oauth_signature_method="HMAC-SHA1",oauth_signature="KZJZvT630f2KenB9l9tqSLI%2FfHA%3D",oauth_version="1.0",oauth_timestamp="1306870331"
内容类型:应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8
用户代理:DotNetOpenAuth / 3.4.6.10357
我想我真的寻找如何做到这一点的例子吗?或URL以一个样本?
目前,我想要
WebRequest的HTT prequest =消费。
prepareAuthorizedRequest(端点,AccessToken,对位);
HTT prequest.ContentType =应用/ JSON;
WebResponse类WebResponse类= HTT prequest.GetResponse();
解决方案
最初由伊恩回答:
好吧,我设法解决这个问题。我发现这个问题是我是从 ConsumerBase
调用 prepareAuthorizedRequest
。内容类型,则不能在此之后兑现,这将始终标记为 X WWW的形式,urlen codeD
。要解决这一点,我叫 prepareAuthorizedRequest
正常,然后创建一个新的HttpWebRequest和整个OAuth的授权头复制。
HttpWebRequest的HTT prequest =消费。
prepareAuthorizedRequest(端点,AccessToken);
HttpWebRequest的HttpWebRequest的=(HttpWebRequest的)
WebRequest.Create(ServiceBase的+/编辑/ 6363241);
的foreach(在HTT prequest.Headers.AllKeys串headerKey)
httpWebRequest.Headers.Add(headerKey,HTT prequest.Headers [headerKey]);
httpWebRequest.ContentType =应用/ JSON;
httpWebRequest.Method =POST;
使用(流requestStream = httpWebRequest.GetRequestStream())
{
字节[]字节= Encoding.UTF8.GetBytes(test22);
requestStream.Write(字节,0,bytes.Length);
}
WebResponse类WebResponse类= httpWebRequest.GetResponse();
字符串responseContent =新的StreamReader(webResponse.GetResponseStream())。
ReadToEnd();
I'm using DotNetOpenAuth and attempting to post a JSON object to a server.
The server is throwing a WebContentType = Raw error.
Authorization: OAuth
oauth_token="V9vVXD51ehUU6WmY%2FQ41qta0RfY%3D",oauth_consumer_key="CHiawRiAb299cOas",oauth_nonce="BBTM4csg",oauth_signature_method="HMAC-SHA1",oauth_signature="KZJZvT630f2KenB9l9tqSLI%2FfHA%3D",oauth_version="1.0",oauth_timestamp="1306870331"
Content-Type: application/x-www-form-urlencoded; charset=utf-8
User-Agent: DotNetOpenAuth/3.4.6.10357
I guess I'm really looking for an example on how to do this? Or a Url to a sample?
I'm currently trying
WebRequest httpRequest = consumer.
PrepareAuthorizedRequest(endpoint, AccessToken, para);
httpRequest.ContentType = "application/json";
WebResponse webResponse = httpRequest.GetResponse();
解决方案
Originally answered by iain:
Okay, I have managed to solve this. The problem that I have found is I was invoking PrepareAuthorizedRequest
from the ConsumerBase
. The content type is then not honoured after this and it is always marked as x-www-form-urlencoded
. To work around this I called PrepareAuthorizedRequest
as normal then created a new HttpWebRequest and copied across the OAuth Authorization header.
HttpWebRequest httpRequest = consumer.
PrepareAuthorizedRequest(endpoint, AccessToken);
HttpWebRequest httpWebRequest = (HttpWebRequest)
WebRequest.Create(serviceBase + "/Edit/6363241");
foreach (string headerKey in httpRequest.Headers.AllKeys)
httpWebRequest.Headers.Add(headerKey, httpRequest.Headers[headerKey]);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (Stream requestStream = httpWebRequest.GetRequestStream())
{
byte[] bytes = Encoding.UTF8.GetBytes(test22);
requestStream.Write(bytes, 0, bytes.Length);
}
WebResponse webResponse = httpWebRequest.GetResponse();
string responseContent = new StreamReader(webResponse.GetResponseStream()).
ReadToEnd();