简体   繁体   English

SOAP服务请求

[英]SOAP Service Request

I have this code that tries to send a request to a soap server, I'm new to soap so i am not sure if the terms i am using are correct or not please correct me I am wrong. 我有这段代码试图将请求发送到Soap服务器,我是Soap的新手,所以我不确定我使用的术语是否正确,请纠正我,我错了。

Basically i am accessing a web service method named getUserDomain via soap request 基本上我是通过soap请求访问一个名为getUserDomain的Web服务方法

Here is the code: 这是代码:

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);
    }
}

This code i have seen somewhere in stack overflow before as an answer but i couldn't get it to work... The error I'm getting is threw exception System.net.webexception. 我之前在堆栈溢出的某处看到了此代码作为答案,但我无法使其正常工作...我得到的错误是抛出异常System.net.webexception。 the remote server returned an error :(500) internal server 远程服务器返回错误:(500)内部服务器

Follow these steps: 跟着这些步骤:

1) click on Project->Add Service Reference 2) click on the "Advanced" button on the bottom of "Add Service Reference" Window 3) click on "Add Web Reference" button on the bottom of the "Service Reference Settings" window 4) type your URL in the URL bar to access the service. 1)单击“项目”->“添加服务引用” 2)单击“添加服务引用”窗口底部的“高级”按钮3)单击“服务引用设置”窗口底部的“添加Web引用”按钮4)在网址栏中输入您的网址以访问服务。

No need for you to code all of that stuff. 无需您编写所有这些东西。 Once the service is added to your project, you can declare an instance of it and use it as needed. 将服务添加到您的项目后,您可以声明它的实例并根据需要使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM