客户端将SOAP请求发送到Web服务

时间:2017-11-09 09:58:49

标签: c# web-services xamarin soap

我正在尝试向我的网络服务发送SOAP请求,并从这个问题发送 - Client to send SOAP request and received response

我得到了这段代码:

using System.Xml;
using System.Net;
using System.IO;

public static void CallWebService()
{
var _url = "http://xxxxxxxxx/Service1.asmx";
var _action = "http://xxxxxxxx/Service1.asmx?op=HelloWorld";

XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
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();
    }
    Console.Write(soapResult);        
}
}

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()
{
    XmlDocument soapEnvelopeDocument = new XmlDocument();
soapEnvelopeDocument.LoadXml(@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/1999/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/1999/XMLSchema""><SOAP-ENV:Body><HelloWorld xmlns=""http://tempuri.org/"" SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""><int1 xsi:type=""xsd:integer"">12</int1><int2 xsi:type=""xsd:integer"">32</int2></HelloWorld></SOAP-ENV:Body></SOAP-ENV:Envelope>");
return soapEnvelopeDocument;
}

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

但它对我没有用,它说没有GetRequestStream()和Headers的定义你在HttpWebRequest下添加SOAPAction,有没有人可以帮我解决问题?

编辑: 错误发生在

行上
using (Stream stream = webRequest.GetRequestStream())

它给出了错误

  

错误CS1061&#39; HttpWebRequest&#39;不包含&#39; GetRequestStream&#39;的定义没有扩展方法&#39; GetRequestStream&#39;接受类型&#39; HttpWebRequest&#39;的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)

行上的另一个错误

 webRequest.Headers.Add("SOAPAction", action);

它给出了错误

  

错误CS1929&#39; WebHeaderCollection&#39;不包含&#39;添加&#39;的定义和最好的扩展方法重载&#39; SettersExtensions.Add(IList,BindableProperty,object)&#39;需要一个类型为“IList”的接收器。

1 个答案:

答案 0 :(得分:0)

用于添加标题:

webRequest.Headers["HeaderToken"] = "HeaderValue";

对于GetRequestStream使用:

using (var stream = await Task.Factory.FromAsync<Stream>(webRequest.BeginGetRequestStream, webRequest.EndGetRequestStream, null))
                    {
                        reqBody.Save(stream);
                    }