gpt4 book ai didi

windows-phone-7 - 为什么在尝试使用 HttpWebRequest 时会收到 ProtocolViolationException?

转载 作者:行者123 更新时间:2023-12-04 16:21:46 25 4
gpt4 key购买 nike

我不知道。我正在尝试从我为 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());
...
}

异常(exception)情况如下:
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 .. 是导致问题的原因吗?
谢谢。

最佳答案

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

关于windows-phone-7 - 为什么在尝试使用 HttpWebRequest 时会收到 ProtocolViolationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2480258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com