ProtocolViolationException when executing WebRequest with GET

I am trying to collect data from an open API for a Windows Phone application.

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 in the last line of the callback method with the message A request with this method cannot have a request body. . I read this because I am trying to send data that is obviously forbidden for the GET protocol, but I cannot see where I am doing it, i.e. How to avoid this.

+4
source share
1 answer

Probably ContentType makes us think that there is a request body, therefore an exception.

Instead, you want to set Accept-Encoding .

+8
source

Source: https://habr.com/ru/post/1480537/


All Articles