Read an xml file

Asked

Viewed 586 times

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 line error:

WebResponse res = request.GetResponse();

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

  • It looks like you’re not getting the data correctly. This pagina is with full address, protocol, everything?

  • @bigown, added the page URL!

  • I have no experience with this, but is the fact that being HTTPS is not causing problems?

  • @bigown, I have no idea. rsrs

1 answer

1

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

request.Method = "GET";

One more thing: GET requests have no body (request body), then you can remove the code that defines the ContentType and that 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());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.