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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With