0

I have a php service that generates XML. How can I parse the XML in C#? I tried using something like this:

WebRequest request = WebRequest.Create("http://devstage.jokeroo.com/rest.php");
request.Method = "GET";
request.ContentType = "text/html";
IAsyncResult result = request.BeginGetResponse(RequestCallback, request);

private void RequestCallback(IAsyncResult ar)
    {
        var request = ar.AsyncState as WebRequest;
        Stream reader = request.EndGetResponse(ar).GetResponseStream();
        //use this reader to read the content
    }

But it keeps throwing this exception:

An exception of type 'System.Net.ProtocolViolationException' occurred in System.Windows.ni.dll but was not handled in user code

Any suggestions?

Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
Alek Arsovski
  • 339
  • 3
  • 18

1 Answers1

2

Get rid of this line:

request.ContentType = "text/html";

You're making a GET request so there is no request body; therefore, setting the content type (for a non-existent and non-supported HTTP request body) is what's causing your error.

Jim O'Neil
  • 23,344
  • 7
  • 42
  • 67