当代码到达使用(WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
的行时,代码将引发500个html错误响应。发布到XML肥皂服务并且很难获得服务器响应
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);
}
}
}
所有我需要做的就是服务响应,这样我可以能够使用
为什么URL和行动具有相同的价值观?这是一个错字吗?什么是input.xml? 500被服务器抛出,所以通过查看客户端代码很难说出任何内容?你有这个Web服务的代码吗?也许你可以调试,或添加一些日志记录或检查现有的 –
输入没有什么..响应将被存储在该路径和文件夹中,并为网址和行动,我没有行动放在那里,它似乎去通过当我使用相同的路径的网址和行动。该Web服务没有WSDL文档,我所有的请求和响应的样本 – Tumelo