Click here to Skip to main content
13,044,371 members (124,874 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to post Xml data to .asmx service.

I can't add the service as a service reference to my project. So I have to post data as a Xml format.

I try with this code.

Please tell me what is wrong with my code...

public void doSend()
            StringBuilder completeUrl = new StringBuilder(urlAddress);
            objRequest = (HttpWebRequest)WebRequest.Create(urlAddress.ToString());
            objRequest.ContentType = "application/x-www-form-urlencoded";
            objRequest.Method = "POST";
            objRequest.Headers["SOAPAction"] = "";
            myRequestData = string.Format(@"<?xml version=""1.0"" encoding=""utf-8""?> <soap:Envelope xmlns:xsi="""" xmlns:xsd="""" xmlns:soap=""""> <soap:Body> <authenticate xmlns=""""> <platformTuru>iPhone</platformTuru> <MusteriNo>NULL</MusteriNo> <request> <username>{0}</username> <getAdslPassword>1</getAdslPassword> <password>{1}</password> </request> </authenticate> </soap:Body> </soap:Envelope>", username.Text, password.Password);
            objRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), objRequest);
        private static void GetRequestStreamCallback(IAsyncResult asyncResult)
            HttpWebRequest objHttpWebRequest = (HttpWebRequest)asyncResult.AsyncState;
            // End the operation
            Stream postStream = objHttpWebRequest.EndGetRequestStream(asyncResult);
            Debug.WriteLine("streeaam: " + postStream.ToString());
            // Convert the string into a byte array.
            Debug.WriteLine("RequestData: " + myRequestData);
            byte[] byteArray = Encoding.UTF8.GetBytes(myRequestData);
            // Write to the request stream.
            postStream.Write(byteArray, 0, myRequestData.Length);
            // Start the asynchronous operation to get the response
            objHttpWebRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), objHttpWebRequest);
        private static void GetResponseCallback(IAsyncResult asyncResult)
                HttpWebRequest objHttpWebRequest = (HttpWebRequest)asyncResult.AsyncState;
                HttpWebResponse objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.EndGetResponse(asyncResult);
                Stream objStreamResponse = objHttpWebResponse.GetResponseStream();
                StreamReader objStreamReader = new StreamReader(objStreamResponse);
                string responseString = objStreamReader.ReadToEnd();            // Got response here
                Debug.WriteLine("Response: " + responseString);
                MessageBox.Show("RESPONSE :" + responseString);
                // Close the stream object
            catch (WebException e)
                HttpStatusCode code = ((HttpWebResponse)(e).Response).StatusCode;
                Debug.WriteLine("Errorr: " + code.ToString());
Posted 27-Nov-12 20:12pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 28 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100