[RESOLVED] POST XML data to external PHP script
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] POST XML data to external PHP script

Hybrid View

  1. #1
    Join Date
    Dec 2012
    Posts
    3

    [RESOLVED] POST XML data to external PHP script

    I am using the below 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 that no XML data is received by the PHP script. I tried changing my encoding to UTF8 but it did not change anything. I am using .NET 4.0

    Any hints or tips will be greatly appreciated.


    Code:
    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();
            }
            
        }
    }

  2. #2
    Join Date
    Jul 2012
    Posts
    90

    Re: POST XML data to external PHP script

    I would think that you should be using UTF8 encoding and ContentType of "text/xml".

  3. #3
    Join Date
    Dec 2012
    Posts
    3

    Re: POST XML data to external PHP script

    I have tried text/xml and UTF8 encoding. I used Fiddler Web Debugger and it seems that I am passing the right XML data so I am unsure if there is a problem with the PHP script on the server. Attached is the Fiddler screenshot.

    Name:  fiddlerwebdebugger.JPG
Views: 257
Size:  79.9 KB

  4. #4
    Join Date
    Dec 2012
    Posts
    3

    Re: POST XML data to external PHP script

    I figured it out, it wasn't a problem in my coding at all. I had to simply package the xml data inside a variable known as xml and pass it to the server for the PHP script to accept it.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center