2

What I'm trying to do is have my PHP page display a string that I've created through a function in my C# application, via System.Net.WebClient.

That's really it. In it' s simplest form, I have:

WebClient client = new WebClient();  
string URL = "http://wwww.blah.com/page.php";
string TestData = "wooooo! test!!";

byte[] SendData = client.UploadString(URL, "POST", TestData);

So, I'm not even sure if that's the right way to do it.. and I'm not sure how to actually OBTAIN that string and display it on the PHP page. something like print_r(SendData) ??

ANY help would be greatly appreciated!

John Saunders
  • 160,644
  • 26
  • 247
  • 397
kogh
  • 995
  • 4
  • 17
  • 30
  • if you have control over the php page you can send the TestData as querystring parameter in the url... then in the php page you will consume it. – Davide Piras Feb 17 '11 at 23:48
  • possible duplicate of [Fake a form submission with C# WebClient](http://stackoverflow.com/questions/726710/fake-a-form-submission-with-c-webclient) – NotMe Feb 17 '11 at 23:50
  • this makes very little sense. Where is this C# application running? You want it to send a string to one PHP page, and then you want to display the string - on the same page? On a different page? Display it to which browser? – John Saunders Feb 18 '11 at 01:14
  • I know it's ridiculous. but - yes; i want the C# application to send the string to the PHP page on a completely different server (that I have access to), where I can further manipulate that string later (extract stuff out of it, put that extracted data into a database, etc). – kogh Feb 18 '11 at 02:13
  • It's not ridiculous. It's called web services ;) – User Feb 18 '11 at 02:32

2 Answers2

6

There are two halves to posting. 1) The code that posts to a page and 2) the page that receives it.

For 1) Your C# looks ok. I personally would use:

string url = "http://wwww.blah.com/page.php";
string data = "wooooo! test!!";

using(WebClient client = new WebClient()) {
    client.UploadString(url, data);  
}

For 2) In your PHP page:

if ( $_SERVER['REQUEST_METHOD'] === 'POST' )
{
    $postData = file_get_contents('php://input');
    print $postData;
}

Read about reading post data in PHP here:

User
  • 62,498
  • 72
  • 186
  • 247
  • THANK you! I get the C# code, but in the PHP code - I still am unable to display the string on the page after receiving it.. probably a silly mistake on my end but its driving me mad :( (getting a blank page) – kogh Feb 18 '11 at 02:54
  • 1
    Then I would try sending the data via a query string first to see if you can get that working (e.g. use c# to send data to http://wwww.blah.com/page.php?message=wooooo!test!! and then use php print $_GET["fmessage"];). Once you have that working, I would come back to the POST issue. – User Feb 18 '11 at 22:21
4

Use This Codes To Send String From C# With Post Method

       try
       {
            string url = "";
            string str = "test";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            string Data = "message="+str;
            byte[] postBytes = Encoding.ASCII.GetBytes(Data);
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = postBytes.Length;
            Stream requestStream = req.GetRequestStream();
            requestStream.Write(postBytes, 0, postBytes.Length);
            requestStream.Close();

            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            Stream resStream = response.GetResponseStream();

            var sr = new StreamReader(response.GetResponseStream());
            string responseText = sr.ReadToEnd();


        }
        catch (WebException)
        {

            MessageBox.Show("Please Check Your Internet Connection");
        }

and php page

 <?php 
    if (isset($_POST['message']))
    {
        $msg = $_POST['message'];

        echo $msg;

    }

   ?>
Pouya Darabi
  • 2,246
  • 18
  • 23