locked
POST XML data to PHP script RRS feed

  • Question

  • using System;
    using System.IO;
    using System.Net;
    using System.Text;
    
    namespace Examples.System.Net
    {
        public class WebRequestPostExample
        {
            public static void Main()
            {
                // Create a request using a URL that can receive a post. 
                WebRequest request = WebRequest.Create("http://stage05.fujifilmimagine.com/phototellerphp/job_tracking.php");
                
                // Set the Method property of the request to POST.
                request.Method = WebRequestMethods.Http.Post;
    
                // Create POST data and convert it to a byte array.
                string postData = "<XML><JOB_NO>JOB~AAA012238</JOB_NO><JOB_STATUS_ID>21</JOB_STATUS_ID><STATUS_DESCRIPTION>Has been downloaded</STATUS_DESCRIPTION></XML>"; 
                byte[] byteArray = Encoding.ASCII.GetBytes(postData);
                
                // Set the ContentType property of the WebRequest.
                request.ContentType = "application/x-www-form-urlencoded";
                // Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length;
                // Get the request stream.
                Stream dataStream = request.GetRequestStream();
    
    
                // Write the data to the request stream and send it now
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.
                dataStream.Close();
    
    
                // Get the response.
                WebResponse response = request.GetResponse();
                // Display the status.
                Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.
                string responseFromServer = reader.ReadToEnd();
                // Display the content.
                Console.WriteLine(responseFromServer);
                // Clean up the streams.
                reader.Close();
                dataStream.Close();
                response.Close();
    
                Console.ReadLine();
            }
            
        }
    }

    I am using the above code which I got from MSDN to send an XML Data via HTTP Post to a server running a PHP script. I receive a response from the server but it does not recieve my XML data.

    The response I get is no XML data received. I tried changing my encoding to UTF8 but it did not change anything.

    Any hints or tips will be greatly appreciated. 

    • Moved by Lisa Zhu Wednesday, December 12, 2012 8:38 AM network related (From:Visual C# General)
    Tuesday, December 11, 2012 1:29 AM

All replies

  • Hi,

    have you tried to change the contenttype to: text/xml


    Please mark as answer if a post answers your question.

    Tuesday, December 11, 2012 7:29 AM
  • Hello,

    Yes I have tried text/xml and I have been using Fiddler Web Debugger and it seems that I am passing the right XML data so I am unsure if this is an issue with the server's PHP script.

    Thanks.

    Tuesday, December 11, 2012 8:50 PM
  • Hi papacharlie1989,

    From your description, I ‘d like to move this post to  the most related forum.

    There are more  experts in this aspect, so you will get  better support and  may have more luck getting answers.

    Thanks for your understanding.

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, December 12, 2012 8:38 AM
  • Is there a possibility that the server I am connecting to can't handle any encoding. Would this cause a problem?
    Wednesday, December 12, 2012 10:54 PM