Sending a HttpWebRequest with PFX Certificate C#

Daniel picture Daniel · Nov 20, 2015 · Viewed 8.3k times · Source

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();

Answer

Daniel picture Daniel · Nov 21, 2015

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);
            }
        }