DotNetOpenAuth POST sample with JSON body
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_开发者_Go百科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();
I believe your mistake is that you're passing in para
as a third parameter to PreparedAuthorizedRequest
. If you are passing in extraParameters then you're telling DotNetOpenAuth that these extra parameters must be included in the message payload, which requires a specific content-type. If you just pass in null
as the third parameter I think you can avoid all this header copying and use your own custom content-type.
You need to set the Content-Length
header via the httpRequest.ContentLength
property prior to getting the request stream.
Also, while you already had it, I searched for hours trying figure out what I was missing, which was the HttpDeliveryMethods.AuthorizationHeaderRequest
in the endpoint
declaration.
string url = serviceBase + "/Booking/6363241";
HttpDeliveryMethods methods = HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest;
MessageReceivingEndpoint endpoint = new MessageReceivingEndpoint(url, methods);
WebRequest httpRequest = consumer.PrepareAuthorizedRequest(endpoint, AccessToken);
httpRequest.ContentType = "application/json";
byte[] bytes = Encoding.UTF8.GetBytes(test22);
httpRequest.ContentLength = bytes.Length;
using (Stream requestStream = httpRequest.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
string responseContent;
using (HttpWebResposne response = (HttpWebResponse)httpRequest.GetResponseStream())
using (StreamReader reader = new StreamReader(response))
{
responseContent = reader.ReadToEnd();
}
精彩评论