SKGeek
SKGeek

Reputation: 81

C# ASP.Net : How to call a web service in .NET 4.0 C# without using the WSDL or proxy classes

I have to send some XML DATA to web service via POST Command , I have no information about the webserice or anything at all , i only know that i need to send a XMLstream to it , i know the server IP adress and the port , the XML tree of the parameters. and that all , I am a bet lost on how to consumme this web service and get the response . I've never worked with Web services so please understand if my code have some major errors and kindly guide me :

public  void Execute()
    {
        HttpWebRequest request = CreateWebRequest();
        XmlDocument soapEnvelopeXml = new XmlDocument();
        soapEnvelopeXml.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
        <soap:Envelope              xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""  xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">

         <soap:Body> 

                    <mergeprofiles xmlns=""http://127.0.0.1:90"">
                    <Profile>
                    <Member>278615</Member>
                    <ExtRef>003D000001StY68IAF</ExtRef>zs 
                    <Name>LESCA</Name>
                    <ChangedBy>user</ChangedBy>
                    <FirstName>Paolo</FirstName>
                    <Street1>via magnanina 3947</Street1>
                     <Street2></Street2>
                    <Street3></Street3>
                    <City>VIGEVANO</City>
                    <ZIP>27029</ZIP>
                    <EMail>[email protected]</EMail>
                    <Phone></Phone>
                    <MobilePhone>+39331231233</MobilePhone>
                    <Fax></Fax>
                    <ISOLanguage>en</ISOLanguage>
                    <Salut1>Mr</Salut1>
                    <Homepage></Homepage>
                    <VATNo1>0</VATNo1>
                    </Profile>
                    <EndOfMessage>@@@</EndOfMessage>
                    </mergeprofiles>

</soap:Body>

</soap:Envelope>");


using (Stream stream = request.GetRequestStream())
        {
            soapEnvelopeXml.Save(stream);
        }

        using (WebResponse response = request.GetResponse())
        {
            using (StreamReader rd = new StreamReader(response.GetResponseStream()))
            {
                string soapResult = rd.ReadToEnd();
                Console.WriteLine(soapResult);
            }
        }
    }
    /// <summary>
    /// Create a soap webrequest to [Url]
    /// </summary>
    /// <returns></returns>
    public HttpWebRequest CreateWebRequest()
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http://127.0.0.1:90/");
        webRequest.Headers.Add(@"SOAP:Action");
        webRequest.ContentType = "text/xml;charset=\"utf-8\"";
        webRequest.Accept = "text/xml";
        webRequest.Method = "POST";
        return webRequest;
    }

The problem is when ever I call the method Execute in page_load, it runs and runs and never stops. The web service is working when I call using other methods, I don't know how or why is that happening, can you please help ?

Upvotes: 0

Views: 3455

Answers (1)

welegan
welegan

Reputation: 3043

Try following some of the steps from this article:

http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client

WebAPI is the latest round of .NET's service frameworks, and it differs quite a lot from the code-generated SOAP classes. Of particular use to you will probably be the following method:

response = await client.PostAsync("your url", new StreamContent(yourStream));

If you are using < .NET 4.5, you may not be able to use this syntax. In that case, try using this answer instead:

.NET: Simplest way to send POST with data and read response

Upvotes: 1

Related Questions