Soap Web请求失败

时间:2016-08-11 12:07:14

标签: c# asp.net web-services soap wsdl

我正在尝试使用asp.net C#中的SOAP请求来使用Web服务,但是它失败了

远程服务器返回错误:(500)内部服务器错误。

以下是我的网络服务请求代码:

public static HttpWebRequest CreateWebRequest()
{
string action = "http://localhost:8081/Service.asmx?op=getData";
string url = "http://localhost:8081/Service.asmx";
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.Headers.Add("SOAPAction", action);
    webRequest.ContentType = "application/soap+xml; charset=utf-8";
    webRequest.Accept = "text/xml";
    webRequest.Method = "POST";
    webRequest.Timeout = 30000;

    return webRequest;
}
public static XmlDocument ServiceCall()
{
    HttpWebRequest request = CreateWebRequest();

    XmlDocument soapEnvelopeXml = CreateSoapEnvelope("MyUser", "MyUser", "MyUser");

    using (Stream stream = request.GetRequestStream())
    {
        soapEnvelopeXml.Save(stream);
    }

    IAsyncResult asyncResult = request.BeginGetResponse(null, null);

    asyncResult.AsyncWaitHandle.WaitOne(20 * 1000);

    string soapResult;
    using (WebResponse webResponse = request.EndGetResponse(asyncResult))
    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
    {
        soapResult = rd.ReadToEnd();
    }


    XmlDocument resp = new XmlDocument();

    resp.LoadXml(soapResult);

    return resp;
}
private static XmlDocument CreateSoapEnvelope(string userName, string password, string DCSCommand)
{
    StringBuilder builder = new StringBuilder();
    builder.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    builder.Append("<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">");
    builder.Append("<soap12:Body>");
    builder.Append("<geteFliteData xmlns=\"http://test.org\">");
    builder.Append("<userName>" + userName + "</userName>");
    builder.Append("<password>" + password + "</password>");
    builder.Append("<DCSCommand>" + DCSCommand + "</DCSCommand>");
    builder.Append("</geteFliteData>");
    builder.Append("</soap12:Body>");
    builder.Append("</soap12:Envelope>");

    XmlDocument soapEnvelop = new XmlDocument();
    soapEnvelop.LoadXml(builder.ToString());
    return soapEnvelop;
}

注意:当我使用WSDL文件使用此Web服务时,它工作正常并返回预期的XML

0 个答案:

没有答案