views:

313

answers:

4

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.

A: 

Get a System.Net Trace log of your application, and look at the log file. It might show the reason for the protocolViolationException.

feroze
This is a Windows (7) Phone app, and there aren't configuration files for me to add in anything for System.Net tracing .. not sure how to trace in this situation.
djbyter
I am not familiar with the .net cf, but I think it should support the config files needed. Try putting an application config file with the settings for enabling logging and see if you can get a trace.
feroze
If that doesnt work, see if you can get a wireshark trace. Since you cant run wireshark on your phone, you should see if you can run wireshark on the same subnet where your phone has it's IP - and for a repro, run your app over the phones WIFI. Then post the wireshark trace on pastebin.
feroze
A: 

Hi,

Having the exact same issue, did you ever figure it out?

Ronan

rsheridan
A: 

No, never did figure it out. Feroze suggested I trace, but I can't figure out how to trace that app due to the configuration setup.

-_- Still looking for an answer ..

Kori
Most probably you should be able to repro this by using the same code from a desktop application. If you can repro, just get a system.net tracelog as I indicated before.
feroze
A: 

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.