无法使用 c# 使用 web 服务,但在邮递员上工作
Unable to consume web services using c# but working on postman
我正在尝试通过 httpwebrequest 调用网络服务。已成功添加 XML 文档和 headers 但仍然出现错误 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 证书,因为 web 服务服务器 ssl 证书已过期或不受信任。请帮忙,因为我被困住了。我也尝试过 WSDL 文件,但也没有成功。
编辑
我在这里添加图片我是如何尝试 webservice in postman
已解决
经过非常艰苦的寻找之后,我不知何故知道如果 webservice 运行 在 postman 上成功,它还提供该语言的代码(就在保存按钮下方)。所以我使用了该代码(基于 RestClient)。
我的错误是没有在 xml doc. 中使用 \r\n 和适当的空格 所以 xml 是问题所在.
//THis code used for ssl bypassing
ServicePointManager
.ServerCertificateValidationCallback +=
(sender, cert, chain, sslPolicyErrors) => true;
var client = new RestSharp.RestClient(webservice_url)
{
Timeout = -1
};
var request = new RestSharp.RestRequest(Method.POST);
request.AddHeader("Content-Type", "text/xml");
request.AddHeader("SOAPAction", "action");
request.AddParameter("text/xml", "<soapenv:Envelope xmlns:soapenv=\"url1" xmlns:hhm=\"url2">\r\n\t<soapenv:Header>\r\n\t\t<UsernameToken>username</UsernameToken>\r\n\t\t<PasswordText>password</PasswordText>\r\n\t\t<SessionType>None</SessionType>\r\n\t</soapenv:Header>\r\n <soapenv:Body>\r\n <hhm:CreateFollowup_Input>\r\n <hhm:FollowupAction>testing</hhm:FollowupAction>\r\n <hhm:CloseOutSubReason></hhm:CloseOutSubReason>\r\n <hhm:FolloUpStatus>Open</hhm:FolloUpStatus>\r\n <hhm:Object_spcId>1-3B39WBFE</hhm:Object_spcId>\r\n <hhm:Model></hhm:Model>\r\n <hhm:CloseReason></hhm:CloseReason>\r\n <hhm:FollowUpDate>08/20/2020</hhm:FollowUpDate>\r\n <hhm:ExpectedPurchaseDate></hhm:ExpectedPurchaseDate>\r\n <hhm:FollowUpQuestions></hhm:FollowUpQuestions>\r\n <hhm:FollowUpDone></hhm:FollowUpDone>\r\n <hhm:DSEName>10086S20</hhm:DSEName>\r\n <hhm:MakeBought></hhm:MakeBought>\r\n </hhm:CreateFollowup_Input>\r\n </soapenv:Body>\r\n</soapenv:Envelope>", ParameterType.RequestBody);
RestSharp.IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
return response.Content.ToString();
我正在尝试通过 httpwebrequest 调用网络服务。已成功添加 XML 文档和 headers 但仍然出现错误 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 证书,因为 web 服务服务器 ssl 证书已过期或不受信任。请帮忙,因为我被困住了。我也尝试过 WSDL 文件,但也没有成功。
编辑
我在这里添加图片我是如何尝试 webservice in postman
已解决
经过非常艰苦的寻找之后,我不知何故知道如果 webservice 运行 在 postman 上成功,它还提供该语言的代码(就在保存按钮下方)。所以我使用了该代码(基于 RestClient)。 我的错误是没有在 xml doc. 中使用 \r\n 和适当的空格 所以 xml 是问题所在.
//THis code used for ssl bypassing
ServicePointManager
.ServerCertificateValidationCallback +=
(sender, cert, chain, sslPolicyErrors) => true;
var client = new RestSharp.RestClient(webservice_url)
{
Timeout = -1
};
var request = new RestSharp.RestRequest(Method.POST);
request.AddHeader("Content-Type", "text/xml");
request.AddHeader("SOAPAction", "action");
request.AddParameter("text/xml", "<soapenv:Envelope xmlns:soapenv=\"url1" xmlns:hhm=\"url2">\r\n\t<soapenv:Header>\r\n\t\t<UsernameToken>username</UsernameToken>\r\n\t\t<PasswordText>password</PasswordText>\r\n\t\t<SessionType>None</SessionType>\r\n\t</soapenv:Header>\r\n <soapenv:Body>\r\n <hhm:CreateFollowup_Input>\r\n <hhm:FollowupAction>testing</hhm:FollowupAction>\r\n <hhm:CloseOutSubReason></hhm:CloseOutSubReason>\r\n <hhm:FolloUpStatus>Open</hhm:FolloUpStatus>\r\n <hhm:Object_spcId>1-3B39WBFE</hhm:Object_spcId>\r\n <hhm:Model></hhm:Model>\r\n <hhm:CloseReason></hhm:CloseReason>\r\n <hhm:FollowUpDate>08/20/2020</hhm:FollowUpDate>\r\n <hhm:ExpectedPurchaseDate></hhm:ExpectedPurchaseDate>\r\n <hhm:FollowUpQuestions></hhm:FollowUpQuestions>\r\n <hhm:FollowUpDone></hhm:FollowUpDone>\r\n <hhm:DSEName>10086S20</hhm:DSEName>\r\n <hhm:MakeBought></hhm:MakeBought>\r\n </hhm:CreateFollowup_Input>\r\n </soapenv:Body>\r\n</soapenv:Envelope>", ParameterType.RequestBody);
RestSharp.IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
return response.Content.ToString();