Click here to Skip to main content
16,005,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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...

C#
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"] = "http://tempuri.org/Register";

            myRequestData = string.Format(@"<?xml version=""1.0"" encoding=""utf-8""?> <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> <soap:Body> <authenticate xmlns=""http://tempuri.org/""> <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);

            //HttpWebRequestExtensions.GetRequestStream(objRequest);
            //HttpWebRequestExtensions.GetResponse(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);
            postStream.Close();

            // Start the asynchronous operation to get the response
            objHttpWebRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), objHttpWebRequest);
        }

        private static void GetResponseCallback(IAsyncResult asyncResult)
        {
            try
            {
                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
                objStreamResponse.Close();
                objStreamReader.Close();
                objHttpWebResponse.Close();
            }
            catch (WebException e)
            {
                HttpStatusCode code = ((HttpWebResponse)(e).Response).StatusCode;

                Debug.WriteLine("Errorr: " + code.ToString());
            }
        }
Posted

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900