Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Windows Mobile Phone
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"] = "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 27-Nov-12 21:12pm

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

  Print Answers RSS
0 OriginalGriff 7,800
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 28 Nov 2012
Copyright © CodeProject, 1999-2014
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