Skip to main content
 首页 » 编程设计

c#之使用 PFX 证书 C# 发送 HttpWebRequest

2024年05月22日43dyllove98

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

请您参考如下方法:

我不知道怎么做,但这段代码运行得很好。

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