【发布时间】:2021-01-03 08:03:38
【问题描述】:
我正在尝试通过 httpwebrequest 调用 webservice。已成功添加 XML 文档和标题,但仍然出现错误 Internal Server Error 500 。从调试模式复制的相同 xml 文档在邮递员上运行良好。我的代码是
public HttpWebRequest CreateWebRequest(string url, string action)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction",action);
//webRequest.Headers.Add("ContentType","text/xml;charset=\"utf-8\"");
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
// "charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
public string CallWebService()
{
try
{
var _url = "https://xxxxxxxxxx/siebel/app/eai/enu?SWEExtSource=WebService&SWEExtCmd=Execute&WSSOAP=1";
var _action = @"""document/http://yyyyyy/:CreateFollowup""";
XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
ServicePointManager
.ServerCertificateValidationCallback +=
(sender, cert, chain, sslPolicyErrors) => true;
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
//InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
Invoke(new Action(() =>
{
listBox_Log.Items.Add("saving soapenvelope.");
}));
soapEnvelopeXml.Save(webRequest.GetRequestStream());
// begin async call to web request.
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
// suspend this thread until call is complete. You might want to
// do something usefull here like update your UI.
asyncResult.AsyncWaitHandle.WaitOne();
// get the response from the completed web request.
string soapResult = null;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
Invoke(new Action(() =>
{
listBox_Log.Items.Add("fetching response");
}));
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
Invoke(new Action(() =>
{
listBox_Log.Items.Add("collecting response");
}));
soapResult = rd.ReadToEnd();
}
Console.Write(soapResult);
}
return soapResult;
}
catch(Exception ex)
{
throw ex;
}
}
public XmlDocument CreateSoapEnvelope()
{
XmlDocument soapEnvelop = new XmlDocument();
soapEnvelop.LoadXml(@"xml code");
return soapEnvelop;
}
我也尝试绕过 ssl 证书,因为 webservice 服务器 ssl 证书已过期或不受信任。请帮忙,因为我被卡住了。我也尝试过 WSDL 文件,但也没有成功。
【问题讨论】:
-
如果你展示了你是如何使用 Postman 创建请求的,那么我们比较和对比会更容易
-
还有你所说的“尝试过的 WSDL 文件”到底是什么意思,这个描述太模糊了,任何人都无法知道你是否正确地做到了。
-
这大约是我回答这个问题的 1000 次。c# 中的默认标头与 http 的其他工具不同。所以解决问题的最佳方法是使用像 wireshark 或 fiddler 这样的嗅探器和比较 Postman 和 c# 中的第一个请求。还要检查正在使用的 TLS 的 500 错误版本。五年前,由于安全漏洞,业界决定消除 TLS 1.0/1.1。微软在 6 月推出了一项安全更新,在服务器上禁用了 TLS 1.0/1.1。客户端必须确保正在使用 TLS 1.2 或 1.3。如果未指定,则使用操作系统的默认版本。
-
@ADyson 我已经为你添加了邮递员图片。这是在这里使用相同的标题和 xml 正文。对于 WSDL 文件,我使用 add service reference -> add web reference 添加了它。然后使用它的功能。但收到错误 客户端发现响应内容类型为“”,但预期为“文本/xml”。请求失败,响应为空
-
@jdweng 请你更具体或更清楚,因为我是消费网络服务的初学者。这是我第一次。早些时候我收到您提到的错误 底层连接已关闭:无法为 SSL/TLS 安全通道建立信任关系。 ---> System.Security.Authentication.AuthenticationException:根据验证程序,远程证书无效。。但后来我从互联网添加了证书绕过代码,然后它显示新错误 Internal Server Error 500。可能你说的对,但我没听懂。
标签: c# asp.net web-services postman webrequest