【发布时间】:2016-02-03 17:36:57
【问题描述】:
当代码到达使用(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);
}
}
}
我需要做的就是获取服务响应,以便我能够使用这些方法
【问题讨论】:
-
为什么url和action的值一样?那是错字吗? input.xml 中有什么? 500是服务器抛出的,所以通过查看客户端代码很难说什么?你有那个网络服务的代码吗?也许您可以调试它或添加一些日志记录或检查现有的日志
-
输入没有任何内容..响应将存储在该路径和文件夹中,至于 url 和操作,我没有任何操作可以放在那里,当我使用相同的路径时它似乎会通过对于 url 和操作。 Web 服务没有 WSDL 文档,我所拥有的只是要处理的请求和响应的示例
标签: c# asp.net xml web-services soap