我不知道。我正在嘗試從我爲Windows Phone構建的應用上的REST服務中獲取XML。爲什麼在嘗試使用HttpWebRequest時收到ProtocolViolationException?
HttpWebResponse response = request.EndGetResponse(ar) as HttpWebResponse;
我有以下設置(忽略壞的URL,這只是一個例子。)
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實例放入使用塊中。 HttpWebResponse,StreamReader甚至Stream都需要使用''blocks',否則你會泄漏資源。 – 2010-03-19 20:13:18
添加了我的問題的例外..我修改了使用「使用」塊,這很好。問題仍然存在。 – djbyter 2010-03-23 14:16:05
不知道這是否是請求它的正確位置,但是您能否將Windows-Phone-7標記添加到此文章中? – CACuzcatlan 2010-12-07 23:00:34