【问题标题】:Sending a HttpWebRequest with PFX Certificate C#使用 PFX 证书 C# 发送 HttpWebRequest
【发布时间】:2015-11-20 22:40:33
【问题描述】:

我一直在尝试发送 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();

【问题讨论】:

    标签: c# soap webrequest pfx envelope


    【解决方案1】:

    我不知道如何,但这段代码运行良好。

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

    【讨论】:

      【解决方案2】:

      看起来您发送到服务器的 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();
      

      【讨论】:

      • 我尝试使用此代码,但仍然收到相同的错误消息。
      猜你喜欢
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 2022-07-11
      • 2012-11-10
      相关资源
      最近更新 更多