C#使用SOAP将对象发送到webservice

时间:2013-10-21 06:01:31

标签: c# web-services soap httpwebrequest

我有一个班级:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="MyClass", Namespace="http://model.common.party.ent.gfdi.be")]
[System.SerializableAttribute()]
public class MyClass : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged 
{

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string firstname;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string lastname;
}

我创建了一个实例:

   var myClass = new MyClass() { lastname = "AAA", firstname = "BBB" };

我想将此实例发送到网络服务。

我想将此对象发送到Web服务。 Web服务收到的消息应如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:membership="http://service.common.party.ent.gfdi.be 
http://service.common.party.ent.gfdi.be">
   <soapenv:Header/>
   <soapenv:Body>
      <membership:find>
         <membership:in0>
           <membership:lastname>AAA</membership:lastname>
           <membership:firstname>BBB</membership:firstname>
         </membership:in0>
      </membership:find>
   </soapenv:Body>
</soapenv:Envelope>

我试过了:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(myurl);
webRequest.Headers["Authorization"] = "Basic " + "XXXXXXXXXXXXXX=";
webRequest.ContentType = "text/xml; charset=UTF-8";
webRequest.Method = "POST";

XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

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

asyncResult.AsyncWaitHandle.WaitOne();

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

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

private static XmlDocument CreateSoapEnvelope()
{
    XmlDocument soapEnvelop = new XmlDocument();
    soapEnvelop.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 soapEnvelop;
}

有什么想法吗?

谢谢,

1 个答案:

答案 0 :(得分:0)

阅读你的评论我会做以下事情:

  • 我会请求网络服务网址的用户名/密码,并通过网络浏览器(视觉工作室外)进行身份验证。

  • 然后在项目中,使用添加服务引用 - &gt;高级 - &gt;添加webreference。选项并引入它不应该请求身份验证的URL,因为IExplorer已经过身份验证。

  • 如果您设法获取WSDL服务描述解析并创建WS类,那么您就完成了,因为您可以在使用以下内容时使用Web服务为webService提供基本身份验证:

    WebServiceClass wsClass = new WebServiceClass();
    wsClass .Credentials = new System.Net.NetworkCredential("user", "password");