gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:25:49 25 4
gpt4 key购买 nike

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

关于c# - 使用 PFX 证书 C# 发送 HttpWebRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33837251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com