使用 PFX 证书 C# 发送 HttpWebRequest [英] Sending a HttpWebRequest with PFX Certificate C#

查看:38
本文介绍了使用 PFX 证书 C# 发送 HttpWebRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试发送 Web 请求,但我遇到了此错误远程服务器返回错误:(500) 内部服务器错误."在 req.GetResponse();

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

推荐答案

我不知道如何,但这段代码运行得很好.

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

这篇关于使用 PFX 证书 C# 发送 HttpWebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆