使用GET执行WebRequest时的ProtocolViolationException

时间:2013-05-13 16:37:58

标签: c# .net windows-phone-7 http-get

我正在尝试从Windows Phone应用的公共API中收集数据。

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);
}

但是我继续在回调方法的最后一行获得ProtocolViolationException,消息为A request with this method cannot have a request body.。我读到这是因为我正在尝试发送数据,这显然是禁止GET协议的,但我不知道我在做什么,即如何避免它。

1 个答案:

答案 0 :(得分:8)

可能是ContentType让它认为有一个请求体,因此是例外。

您可能希望设置Accept-Encoding