2015-11-03 53 views
1

当代码到达使用(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); 
     } 
    } 

} 

所有我需要做的就是服务响应,这样我可以能够使用

+0

为什么URL和行动具有相同的价值观?这是一个错字吗?什么是input.xml? 500被服务器抛出,所以通过查看客户端代码很难说出任何内容?你有这个Web服务的代码吗?也许你可以调试,或添加一些日志记录或检查现有的 –

+0

输入没有什么..响应将被存储在该路径和文件夹中,并为网址和行动,我没有行动放在那里,它似乎去通过当我使用相同的路径的网址和行动。该Web服务没有WSDL文档,我所有的请求和响应的样本 – Tumelo

回答

0

你的SOAP动作似乎是错误的方法。如果你看一下这里的架构(https://iserv.intecon.co.za/allpsws_test/allps.asmx?op=Call),你可以看到它的希望

SOAPAction: "http://intecon.co.za/webservices/allps/Call" 

与小提琴手(或类似的东西),一出戏,并尝试以确保您能获得通话的代码外工作。

使用此头:

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; 
} 
+0

您是否介意告诉我如何youi编码出来,如果你不介意请......分享整个代码和结构,以及要求应该是这样的: – Tumelo

+0

测试 不同厂家 pc_name allps - WS SomeSoft ThisProd 1.0.0.1 。这是为了验证整个会话并获取允许我通过信息发送的方法。 – Tumelo

+0

<呼叫 的xmlns = “http://intecon.co.za/webservices/allps/”> intellicellWS ******* 名称 SomeCompany SomeProduct 1.0.0。1 多数民众赞成什么输入XML有 – Tumelo