2010-03-19 64 views
5

我不知道。我正在嘗試從我爲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 ..是否導致此問題? 謝謝。

+0

請發佈完整的例外,並告訴我們發生異常的地方。另外,在SomeCallback中,您需要將您的IDisposable實例放入使用塊中。 HttpWebResponse,StreamReader甚至Stream都需要使用''blocks',否則你會泄漏資源。 – 2010-03-19 20:13:18

+0

添加了我的問題的例外..我修改了使用「使用」塊,這很好。問題仍然存在。 – djbyter 2010-03-23 14:16:05

+0

不知道這是否是請求它的正確位置,但是您能否將Windows-Phone-7標記添加到此文章中? – CACuzcatlan 2010-12-07 23:00:34

回答

8

我敢打賭,這個問題是因爲你設置了'ContentType'標題,但沒有在你的請求中提供內容。請注意,協議異常在嘗試創建請求時被拋出。

您可能的意思是將'Accept'標頭設置爲「text/xml」而不是內容類型標頭。

+0

這是問題所在。謝謝保羅。 – djbyter 2013-01-29 17:39:36

0

顯然,桌面上這種錯誤最常見的原因是Web服務器需要SSL而不是TLS身份驗證。查看關於this article explaining troubleshooting options for various .NET 2 HTTP errors的所有評論。它看起來在CodePlex上的MIT許可的Chargify.NET項目已經是using this work-around(「SecurityProtocolType.Ssl3」)。

another article circa 2008嘗試的其他選項正在回退到HTTP/1.0或禁用Keep-Alive。

務必嘗試this answer,刪除ContentType初始化。

15

這不是你的問題,但它可能會幫助其他人。我碰到這個,因爲我在GET請求上調用BeginGetRequestStream()。當然,這不是必須的,我應該只是調用BeginGetResponse()。愚蠢的錯誤,但如果錯誤信息更有幫助,我會更快地發現它。

1

「ContentType的」標頭「GET」導致它需要的ContentType編碼成AUTH頭雖然異常,如果出現在請求 在Azure Table中REST API - 使用空字符串GET的頭的「的ContentType」

相關問題