SOAP服务请求

时间:2014-06-11 03:16:24

标签: c# soap request

我有这个代码试图向肥皂服务器发送请求,我是肥皂新手,所以我不确定我使用的条款是否正确请纠正我,我错了。

基本上我通过soap请求访问名为getUserDomain的Web服务方法

以下是代码:

public void CallWebService()
{
    var _url = "https://....com/QcXmlWebService/QcXmlWebService.asmx?wsdl";
    var _action = "https://....com/QcXmlWebService/QcXmlWebService.asmx?op=GetUserDomains";

    XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
    HttpWebRequest webRequest = CreateWebRequest(_url, _action);
    InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
    webRequest.BeginGetResponse(null, null);
    // 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();
        }
        Console.Write(soapResult);
    }
}

private 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 XmlDocument CreateSoapEnvelope()
{
    XmlDocument soapEnvelop = new XmlDocument();
     string oRequest = "";
    oRequest = @"<soap:Envelope xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"" xmlns:qcx=""http://smething.com/QCXML"">";
    oRequest = oRequest + "<soap:Header/>";
    oRequest = oRequest + "<soap:Body>";
    oRequest = oRequest + "<qcx:GetUserDomains>";
    oRequest = oRequest + "<qcx:inputXml><![CDATA[";
    oRequest = oRequest + "<GetUserDomains>";
    oRequest = oRequest + "<login>";
    oRequest = oRequest + "<domain_name>MBB_BTS</domain_name>";
    oRequest = oRequest + "<project_name>WCDMA_BTS_IV</project_name>";
    oRequest = oRequest + "<user_name>user</user_name>";
    oRequest = oRequest + "<password>pass</password>";
    oRequest = oRequest + "</login>";
    oRequest = oRequest + "</GetUserDomains>";
    oRequest = oRequest + " ]]>";
    oRequest = oRequest + "</qcx:inputXml>";
    oRequest = oRequest + "</qcx:GetUserDomains>";
    oRequest = oRequest + "</soap:Body>";
    oRequest = oRequest + "</soap:Envelope>";
    soapEnvelop.LoadXml(oRequest);
    return soapEnvelop;
}

private void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
    using (Stream stream = webRequest.GetRequestStream())
    {
        soapEnvelopeXml.Save(stream);
    }
}

这段代码我已经在堆栈溢出的某处看到了作为答案,但我无法让它工作......我得到的错误是抛出异常System.net.webexception。远程服务器返回错误:(500)内部服务器

1 个答案:

答案 0 :(得分:1)

请按照以下步骤操作:

1)点击Project-&gt; Add Service Reference 2)点击&#34;高级&#34; &#34;添加服务参考&#34;底部的按钮窗口 3)点击&#34;添加网络参考&#34; &#34;服务参考设置&#34;底部的按钮;窗口 4)在URL栏中键入您的URL以访问该服务。

您无需编写所有这些内容的代码。将服务添加到项目后,您可以声明它的实例并根据需要使用它。