Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProtocolViolationException when doing a WebRequest with GET

I'm trying to gather data from a public API for a Windows Phone app.

private void GatherPosts()
{
    string url = baseURL + "?after=" + lastPostId + "&gifs=1";
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    request.ContentType = "text/json";
    request.Method = "GET";

    AsyncCallback callback = new AsyncCallback(PostRequestFinished);
    request.BeginGetResponse(callback, request);
}

private void PostRequestFinished(IAsyncResult result)
{
    HttpWebRequest request = (HttpWebRequest)result.AsyncState;
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
}

But I keep getting a ProtocolViolationException on the last line of the callback method with the message A request with this method cannot have a request body.. I read that it's because I'm trying to send data, which is obviously forbidden for the GET protocol, but I don't see where I'm doing it, i.e. how to avoid it.

like image 354
Cedric Reichenbach Avatar asked Feb 16 '23 13:02

Cedric Reichenbach


1 Answers

It's probably the ContentType that makes it think there is a request body, thus the exception.

You would want to set Accept-Encoding instead.

like image 171
canhazbits Avatar answered Feb 19 '23 04:02

canhazbits