Read an xml file

1

I'm trying to read an XML file.

string pagina = "https://ws.pagseguro.uol.com.br/v3/transactions/notifications/5B93AB-E9FA04FA04D8-24449BAF8A80-E32467?email=diegozanardo@yahoo.com.br&token=DFA3837517594466BCC87D8F397BF15F";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(pagina));
request.Method = "POST";
request.ContentType = "application/xml";
request.Accept = "application/xml";

StreamWriter writer = new StreamWriter(request.GetRequestStream());


WebResponse res = request.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string returnvalue = sr.ReadToEnd();
XmlDocument xm = new XmlDocument();
xm.LoadXml(returnvalue);
XmlNodeList el = xm.GetElementsByTagName("paymentLink");
Response.Redirect(el[0].InnerText);

But it generates the following error in the line:

WebResponse res = request.GetResponse();
  

Additional information: The remote server returned an error: (405)   Method Not Allowed.

    
asked by anonymous 13.10.2014 / 15:41

1 answer

1

The response indicates that the method you are using ( POST ) is not supported by the service. Try changing the line that defines the HttpWebRequest method to use GET that should work:

request.Method = "GET";

One more thing: GET requests do not have request body ), so you can remove the code that sets ContentType and returns the request stream :

string pagina = "https://ws.pagseguro.uol.com.br/v3/transactions/notifications/5B93AB-E9FA04FA04D8-24449BAF8A80-E32467?email=diegozanardo@yahoo.com.br&token=DFA3837517594466BCC87D8F397BF15F";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(pagina));
request.Method = "GET";
request.Accept = "application/xml";

WebResponse res = request.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
    
13.10.2014 / 18:20