使用 PFX 证书 C# 发送 HttpWebRequest
Sending a HttpWebRequest with PFX Certificate C#
我一直在尝试发送 Web 请求,但我在 req.GetResponse();
上遇到此错误 "The remote server returned an error: (500) Internal Server Error."
我真不知道是不是少了什么东西,还是哪里出了问题。
谁能帮我解决这个问题?
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();
看起来您发送到服务器的 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();
我不知道怎么做,但这段代码运行得很好。
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);
}
}
我一直在尝试发送 Web 请求,但我在 req.GetResponse();
上遇到此错误 "The remote server returned an error: (500) Internal Server Error."我真不知道是不是少了什么东西,还是哪里出了问题。
谁能帮我解决这个问题?
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();
看起来您发送到服务器的 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();
我不知道怎么做,但这段代码运行得很好。
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);
}
}