简体   繁体   中英

C# Send object to webservice with SOAP

I have a class :

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

I create an instance :

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

I'd like send this instance to the webservice.

I'd like send this object to a web service. The message received by the web service should look this :

<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>

I tried this :

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

Any idea ?

Thanks,

Reading your comments I would do the following:

  • I would request the user/password of the web service url and authenticate with internet explorer (outside visual studio).

  • Then in project , use the add service reference--> advanced --> add webreference. option and introduce the URL it should not request authentication since IExplorer is already authenticated.

  • If you manage to get the WSDL service description parse and create the WS Classes, then you are done because you can provide the basic authentication to the webService once you want to consume the webservice using:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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