代码在使用(WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
到达该行时会引发500 html错误响应。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
public partial class RawService : System.Web.UI.Page
{
static string _url = "https://iserv.intecon.co.za/allpsws_test/allps.asmx";
static string _action = "https://iserv.intecon.co.za/allpsws_test/allps.asmx";
static string _inputPath = @"C:\intercon\input.xml";
static string _outputPath = @"C:\intercon\output.xml";
static string _soapEnvelope =
@"<soap:Envelope
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
<soap:Body></soap:Body></soap:Envelope>";
protected void Page_Load(object sender, EventArgs e)
{
}
private static HttpWebRequest CreateWebRequest(string url, string action)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", action);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
private static XmlDocument CreateSoapEnvelope(string content)
{
StringBuilder sb = new StringBuilder(_soapEnvelope);
sb.Insert(sb.ToString().IndexOf("</soap:Body>"), content);
// create an empty soap envelope
XmlDocument soapEnvelopeXml = new XmlDocument();
soapEnvelopeXml.LoadXml(sb.ToString());
return soapEnvelopeXml;
}
protected void Button1_Click(object sender, EventArgs e)
{
string content = File.ReadAllText(_inputPath);
XmlDocument soapEnvelopeXml = CreateSoapEnvelope(content);
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
// 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;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
File.WriteAllText(_outputPath, (soapResult));
}
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
}
}
我需要做的就是获取服务响应,以便我能够使用方法
答案 0 :(得分:0)
您的SOAP操作似乎有误。如果您查看此处的架构(https://iserv.intecon.co.za/allpsws_test/allps.asmx?op=Call),您可以看到它正在等待
SOAPAction: "http://intecon.co.za/webservices/allps/Call"
玩Fiddler(或类似的东西)并尝试确保您可以在代码之外使用该呼叫。
使用此标题:
Host: iserv.intecon.co.za
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://intecon.co.za/webservices/allps/Call"
Content-Length: 364
这个身体:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Call xmlns="http://intecon.co.za/webservices/allps/">
<xmlrequest></xmlrequest>
</Call>
</soap:Body>
</soap:Envelope>
我得到200 OK响应 - 虽然看起来我正在调用的系统存在问题,可能是因为我不知道我应该传递的XMLRequest应该是什么样的。
编辑:将一些代码放在一起作为例子:
static void Main( string[] args )
{
HttpWebRequest request = ( HttpWebRequest )WebRequest.Create( "https://iserv.intecon.co.za/allpsws_test/allps.asmx" );
request.Headers.Add( "SOAPAction", "http://intecon.co.za/webservices/allps/Call" );
request.ContentType = "text/xml; charset=utf-8";
request.Accept = "text/xml";
request.Method = "POST";
XmlDocument soapRequest = CreateEnvelope();
using ( Stream stream = request.GetRequestStream() )
{
soapRequest.Save( stream );
}
HttpWebResponse webResponse = ( HttpWebResponse )request.GetResponse();
StreamReader reader = new StreamReader( webResponse.GetResponseStream() );
string response = reader.ReadToEnd();
Console.WriteLine( response );
}
private static XmlDocument CreateEnvelope()
{
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml( @"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
<soap:Body>
<Call xmlns=""http://intecon.co.za/webservices/allps/"">
<xmlrequest></xmlrequest>
</Call>
</soap:Body>
</soap:Envelope>" );
return xmlDocument;
}