【问题标题】:Why am I getting ProtocolViolationException when trying to use HttpWebRequest?为什么在尝试使用 HttpWebRequest 时出现 ProtocolViolationException?
【发布时间】:2010-03-19 20:04:55
【问题描述】:

我不知道。我正在尝试从我为 Windows Phone 构建的应用程序上的 REST 服务获取 XML。我总是在以下行遇到异常:

HttpWebResponse response = request.EndGetResponse(ar) as HttpWebResponse;

我有以下设置(忽略错误的网址,这只是一个示例..)

HttpWebRequest request = WebRequest.Create("https://domain.chargify.com/customers.xml") as HttpWebRequest;
NetworkCredential credentials = new NetworkCredential("appkeyhere", "password");
request.Credentials = credentials;
request.Method = "GET";
request.ContentType = "text/xml";
request.BeginGetResponse(new AsyncCallback(SomeCallback), request);
...
private void SomeCallback(IAsyncResult ar) {
    HttpWebRequest request = ar.AsyncState as HttpWebRequest;
    HttpWebResponse response = request.EndGetResponse(ar) as HttpWebResponse;
    StreamReader reader = new StreamReader(response.GetResponseStream());
    XElement xmlResult = XElement.Parse(reader.ReadToEnd());
    ...
}

例外情况如下:

System.Net.ProtocolViolationException was unhandled
Message=ProtocolViolationException
  StackTrace:
       at System.Net.Browser.ClientHttpWebRequest.PrepareAndSendRequest(String method, Uri requestUri, Stream requestBodyStream, WebHeaderCollection headerCollection, CookieContainer cookieContainer)
       at System.Net.Browser.ClientHttpWebRequest.BeginGetResponseImplementation()
       at System.Net.Browser.ClientHttpWebRequest.InternalBeginGetResponse(AsyncCallback callback, Object state)
       at System.Net.Browser.AsyncHelper.BeginOnUI(BeginMethod beginMethod, AsyncCallback callback, Object state)
       at System.Net.Browser.ClientHttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state)
       at ChargifyWPA.MainPage.button1_Click(Object sender, RoutedEventArgs e)
       at System.Windows.Controls.Primitives.ButtonBase.OnClick()
       at System.Windows.Controls.Button.OnClick()
       at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
       at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

有人有什么想法吗?它是https,而不是http ..是导致问题的原因吗? 谢谢。

【问题讨论】:

  • 请发布完整的异常,并告诉我们异常发生在哪里。此外,在 SomeCallback 中,您需要将 IDisposable 实例置于 using 块中。 HttpWebResponse、StreamReader 甚至 Stream 都需要在 using 块中,否则会泄漏资源。
  • 为我的问题添加了例外..我已经修改为使用“使用”块,这很好。问题仍然存在。
  • 不确定这是否是请求它的正确位置,但能否请您将 Windows-Phone-7 标签添加到此帖子?

标签: windows-phone-7 httpwebrequest


【解决方案1】:

这不是你的问题,但它可能会帮助其他人。我遇到了这个问题,因为我在 GET 请求上调用 BeginGetRequestStream()。当然,这不是必需的,我应该只是调用 BeginGetResponse()。愚蠢的错误,但如果错误消息更有帮助,我会更快地发现它。

【讨论】:

    【解决方案2】:

    我敢打赌,这个问题是因为您设置了“ContentType”标头,但没有在您的请求中提供内容。请注意,在尝试创建请求时会引发协议异常。

    您可能的意思是将“接受”标头设置为“text/xml”而不是内容类型标头。

    【讨论】:

    • 这就是问题所在。谢谢保罗。
    【解决方案3】:

    如果请求中存在带有“GET”的“Content-Type”标头,则会导致异常 在 Azure Table REST API 中,需要将 ContentType 编码为 auth 标头 - 使用空字符串作为 GET 标头的“ContentType”

    【讨论】:

      【解决方案4】:

      显然,如今桌面上出现此错误的最常见原因是需要 SSL 而不是 TLS 身份验证的 Web 服务器。查看this article explaining troubleshooting options for various .NET 2 HTTP errors 上的所有 cmets。看来您在 CodePlex 上获得 MIT 许可的 Chargify.NET 项目已经是 using this work-around ("SecurityProtocolType.Ssl3")。

      从another article circa 2008 尝试的其他选项是回退到 HTTP/1.0 或禁用 Keep-Alive。

      一定要试试this answer,去掉 ContentType 初始化。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-14
        • 2015-01-15
        • 1970-01-01
        • 1970-01-01
        • 2021-08-22
        • 2021-12-24
        • 2013-03-07
        相关资源
        最近更新 更多