I've no idea. I'm trying to get XML from a REST service on an app I'm building for Windows Phone. I always get an exception at the following line:
HttpWebResponse response = request.EndGetResponse(ar) as HttpWebResponse;
I have the following setup 开发者_高级运维(ignore the bad url, it's just an example .. )
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());
...
}
The exception is as follows:
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)
Anyone have any ideas? It is https, not http .. is that causing the issue? Thanks.
This isn't your problem, but it might help somebody else. I ran into this because I was calling BeginGetRequestStream() on a GET request. Of course, that's not necessary and I should have just been calling BeginGetResponse(). Silly mistake, but I would have figured it out a lot faster if the error message had been more helpful.
My bet this problem is because you are setting the 'ContentType' header, but not providing content in your request. Note the protocol exception is thrown while trying to create the request.
You probably mean to set the 'Accept' header to "text/xml" rather than the content type header.
"Content-Type" header with "GET" causes the exception if present in request In Azure Table REST API it's required to encode ContentType into auth header though - use empty string for GET's header's "ContentType"
Apparently the most common cause of this error on the desktop these days is a web server that requires SSL rather than TLS authentication. See all the comments on this article explaining troubleshooting options for various .NET 2 HTTP errors. It appears your MIT-licensed Chargify.NET project on CodePlex is already using this work-around ("SecurityProtocolType.Ssl3").
Other options to try from another article circa 2008 are falling back to HTTP/1.0 or disabling Keep-Alive.
Be sure to try this answer, removing the ContentType initialization.
精彩评论