2

I have been trying to send a Web Request, but im facing this error "The remote server returned an error: (500) Internal Server Error." on req.GetResponse();

I don't really know if something is missing or if is something wrong.

Can anyone can help me with this?

string soap = "<?xml version='1.0'?> " +
    "soapenv:Envelope xmlns:ns='http://www.buzonfiscal.com/ns/xsd/bf/bfcorp/32' xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> " + 
    "<soapenv:Header/> " +
    "<soapenv:Body> " +
    "<ns:RequestCancelaCFDi uuid='" + this.txtUUID.Text + "' rfcReceptor='" + this.txtReceptor.Text + "' rfcEmisor='" + this.txtEmisor.Text + "'/> " +
    "</soapenv:Body> " +
    "</soapenv:Envelope> ";

        X509Certificate2 cert = new X509Certificate2(@"C:\test.pfx", "password");

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://WebRequest.com/bfcorpcfdi32ws");

        req.ContentType = "text/xml";
        req.Method = "POST";
        req.ClientCertificates.Add(cert);

       // MessageBox.Show(soap);

        using (Stream stm = req.GetRequestStream())
        {
            using (StreamWriter stmw = new StreamWriter(stm))
            {
                stmw.Write(soap);
                stmw.Close();
            }
        }

        WebResponse response = req.GetResponse();
        Stream responseStream = response.GetResponseStream();

        response = req.GetResponse();
        StreamReader sr = new StreamReader(response.GetResponseStream());
        string result = sr.ReadToEnd();
        sr.Close();
Daniel
  • 66
  • 1
  • 1
  • 5

2 Answers2

3

i dont know how but this code worked perfectly.

string soap = "<?xml version='1.0'?> " +
                        "<soapenv:Envelope xmlns:ns='http://www.buzonfiscal.com/ns/xsd/bf/bfcorp/32' xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> " +
                        "<soapenv:Header/> " +
                        "<soapenv:Body> " +
                        "<ns:RequestCancelaCFDi uuid='" + this.txtUUID.Text + "' rfcReceptor='" + this.txtReceptor.Text + "' rfcEmisor='" + this.txtEmisor.Text + "'/> " +
                        "</soapenv:Body> " +
                        "</soapenv:Envelope> ";

        X509Certificate2 cert = new X509Certificate2(@"C:\test.pfx", "password");

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://WebRequest.com/bfcorpcfdi32ws");

        req.ContentType = "text/xml";
        req.Method = "POST";
        req.ClientCertificates.Add(cert);

        MessageBox.Show(soap);

        using (Stream stm = req.GetRequestStream())
        {
            using (StreamWriter stmw = new StreamWriter(stm))
            {
                stmw.Write(soap);
                stmw.Close();
            }
        }
        try
        {
            WebResponse response = req.GetResponse();
            Stream responseStream = response.GetResponseStream();

            response = req.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream());
            string result = sr.ReadToEnd();
            sr.Close();

        }
        catch (Exception ex)
        {
            if (ex is WebException)
            {
                WebException we = ex as WebException;
                WebResponse webResponse = we.Response;
                throw new Exception(ex.Message);
            }
        }
Daniel
  • 66
  • 1
  • 1
  • 5
1

It looks like you might have an error in the XML you are sending to the server. Your first line should look like this:

string soap = "<?xml version='1.0'?> " +
"<soapenv:Envelope xmlns:ns='http://www.buzonfiscal.com/ns/xsd/bf/bfcorp/32' xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> " + 
"<soapenv:Header/> " +
"<soapenv:Body> " +
"<ns:RequestCancelaCFDi uuid='" + this.txtUUID.Text + "' rfcReceptor='" + this.txtReceptor.Text + "' rfcEmisor='" + this.txtEmisor.Text + "'/> " +
"</soapenv:Body> " +
"</soapenv:Envelope> ";

You should also be careful and escape the values you are setting. While a little bit more verbose, using XDocument, XElement and XAttribute may help you guarantee you have a valid document.

XNamespace soapenv = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace ns = "http://www.buzonfiscal.com/ns/xsd/bf/bfcorp/32";
var doc = new XDocument(
    new XElement(soapenv + "Envelope",
        new XAttribute(XNamespace.Xmlns + "soapenv", soapenv),
        new XAttribute(XNamespace.Xmlns + "ns", ns),
        new XElement(soapenv + "Header"),
        new XElement(ns + "RequestCancelaCFDi",
            new XAttribute("uuid", this.txtUUID.Text),
            new XAttribute("rfcReceptor", this.txtReceptor.Text),
            new XAttribute("rfcEmisor", this.txtEmisor.Text)
            )
        )
    );

var builder = new StringBuilder();
using (var writer = new StringWriter(builder))
{
    doc.Save(writer);
}

string soap = builder.ToString();
JesseYoung
  • 105
  • 6