Reading the response.

24 views
Skip to first unread message

Márcio Laubstein

unread,
Jun 10, 2014, 4:07:31 PM6/10/14
to
Hello, I am using DNOA to communicate from an Outlook AddIn and a WebApi website.
I have the following code in my Outlook AddIn:

// snippet
var consumer = CreateConsumer();


HttpWebRequest req = consumer.PrepareAuthorizedRequest(new MessageReceivingEndpoint(WebApiAddress + ODataAddress + "/" + "TimesheetViews",
HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest), ActualAccessToken);
req
.ContentLength = stream.Length;

req
.ContentType = "application/json";
stream
.CopyTo(req.GetRequestStream());
consumer
.Channel.WebRequestHandler.GetResponse(req);



When the server returns a 500 internal error DNOA throws an ProtocolException and when I try to read the response like this:


var stringbuffer = new StringBuilder();
using (var stream = ((System.Net.WebException)ex.InnerException).Response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
String data = reader.ReadToEnd();
stringbuffer
.Append(data);
}
}


It was already consumed and closed by DNOA. I searched a lot of sites and I found out that I need to implement my own IDirectWebRequestHandler.

So I did my class as:

public class CustomWebRequestHandler : IDirectWebRequestHandler
{
//code
}


And changed the request code to:

var consumer = CreateConsumer();


HttpWebRequest req = consumer.PrepareAuthorizedRequest(new MessageReceivingEndpoint(WebApiAddress + ODataAddress + "/" + "TimesheetViews",
HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest), ActualAccessToken);
req
.ContentLength = stream.Length;

req
.ContentType = "application/json";
stream
.CopyTo(req.GetRequestStream());
consumer
.Channel.WebRequestHandler = new CustomWebRequestHandler(); // here i tell to use my implementation of IDirectWebRequestHandler

consumer
.Channel.WebRequestHandler.GetResponse(req);



Now I have 2 questions:

1 - When Andrew says to plug an IDirectWebRequestHandler implementation in DNOA is it as I am doing here? => consumer.Channel.WebRequestHandler = new CustomWebRequestHandler();

2 - I could not fully implement IDirectWebRequestHandler because one of its methods requires that I return an IncomingWebRequest so I ended up with the same problem as this thread: http://stackoverflow.com/questions/8217484/dotnetopenauth-implementing-idirectwebrequesthandler-class

I don't know what I am doing wrong, any help will be really appreciated.

Thanks!
Reply all
Reply to author
Forward
0 new messages