【问题标题】:ProtocolViolationException when doing a WebRequest with GET使用 GET 执行 WebRequest 时出现 ProtocolViolationException
【发布时间】:2013-05-13 16:37:57
【问题描述】:

我正在尝试从 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 协议所禁止的,但我看不到我在做什么,即如何避免它。

【问题讨论】:

  • 尝试注释您设置ContentType 的行。 Afaik,json 请求总是有一个 BODY,get 不是这种情况。

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


【解决方案1】:

可能是ContentType 让它认为有请求正文,因此是例外。

您可能想设置Accept-Encoding

【讨论】:

  • 工作优雅。我认为我不需要 Accept-Encoding,因为规范说它始终是 JSON。
猜你喜欢
  • 2014-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多