使用PFX证书C#发送HttpWebRequest

时间:2015-11-20 22:40:33

标签: c# soap webrequest pfx envelope

我一直在尝试发送Web请求,但即时出现此错误“远程服务器返回错误:(500)内部服务器错误。”在req.GetResponse();

我真的不知道是否缺少某些东西或是否有问题。

任何人都可以帮我这个吗?

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

2 个答案:

答案 0 :(得分:2)

我不知道如何,但这段代码完美无缺。

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

答案 1 :(得分:1)

您可能在发送到服务器的XML中出错。 你的第一行应该是这样的:

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> ";

您还应该小心并逃避您正在设置的值。虽然稍微冗长一点,但使用XDocument,XElement和XAttribute可以帮助您保证您拥有有效的文档。

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