+ Reply to Thread
Results 1 to 2 of 2

Thread: Post data to a php script

  1. #1

    Post data to a php script

    I'd like to POST data to a php script , what is the correct way for that ?
    Last edited by csharpmaster; 10-31-2012 at 03:25 AM.

  2. #2
    SO , we got a php script that accept data through POST and to do that in c# , you must create a web request to the script with option "POST" and data to send.

    Here is the snippet :
                // create web request
                WebRequest request = WebRequest.Create("http://www.website.com/script.php");
                // request method is POST
                request.Method = "POST";
                // data to be sent [Parameter]
                string post_data = "id=123&data=xxxxx";
                byte[] array = Encoding.UTF8.GetBytes(post_data);
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = array.Length;
                // getting the response here
                Stream dataStream = request.GetRequestStream();
                // Write the data to the request stream.
                dataStream.Write(array, 0, array.Length);
                // Close the Stream object.
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream());
                string final = sr.ReadToEnd();
                // print the response

+ Reply to Thread

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