1

Having stumbled upon a problem when doing this, I first searched SO to try and find if others were having similar problems, and found this question: POST data to a PHP page from C# WinForm

However, when I tried the code example given in the answer to this question, it does not work. The PHP script that I am making a request to responds with a message indicating that the POST variable that has to be set is not set. Here's my C# code:

HttpWebRequest POSTreq =
    (HttpWebRequest)WebRequest.Create("http://82.41.255.140/api/post-ptr");

string POSTdata = "action=" + HttpUtility.UrlEncode("date");
byte[] data = Encoding.ASCII.GetBytes(POSTdata);

POSTreq.Method = "POST";
POSTreq.ContentType = "application/x-www-form-urlencoded";
POSTreq.ContentLength = data.LongLength;

POSTreq.GetRequestStream().Write(data, 0, data.Length);

HttpWebResponse POSTres = (HttpWebResponse)POSTreq.GetResponse();
Console.WriteLine("HTTP Status Code {0}", POSTres.StatusCode);
Console.WriteLine("Response Method: {0}", POSTres.Method);
Console.WriteLine("Response Text: {0}",
    new StreamReader(POSTres.GetResponseStream()).ReadToEnd());
Console.ReadLine();

And this is the code inside the PHP script:

<?php    
    $A = strtolower($_POST["action"]);

    if ($A == "date")
    {
        echo date("c");
    }
    else if ($A == "ip")
    {
        echo $_SERVER["REMOTE_ADDR"];
    }
    else if ($A == null || $A == "")
    {
        echo "bad_request:no_argument:POST_action";
    }
    else
    {
        echo "bad_request:invalid_argument:POST_action";
    }

    exit();
?>

When I make the POST request from my C# program, I see the following screen, indicating that the variable action has not been set. Am I missing the obvious in my code?

screen

Thanks to those who reply.

Community
  • 1
  • 1
LMS
  • 4,117
  • 5
  • 27
  • 37

3 Answers3

4

You might need to flush the stream. I usually do it like this:

string POSTdata = "action=" + HttpUtility.UrlEncode("date");
byte[] data = Encoding.ASCII.GetBytes(POSTdata);

POSTreq.Method = "POST";
POSTreq.ContentType = "application/x-www-form-urlencoded";
POSTreq.ContentLength = data.LongLength;

using (Stream stream = POSTreq.GetRequestStream()) {
    stream.Write(data, 0, data.Length);
    stream.Flush();
}
Lloyd
  • 29,197
  • 4
  • 84
  • 98
  • This does not solve it, for whatever reason. `Stream POSTstr = POSTreq.GetRequestStream(); POSTstr.Write(data, 0, data.Length); POSTstr.Flush(); POSTstr.Close();` still produces the "no argument" response. – LMS Sep 27 '12 at 16:32
2

You're not closing the request stream. See this example in the MSDN docs, which is very close to your code.

EDIT

Your PHP null check is incorrect also. See this article.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • @Lloyd - sorry, we were typing at the same time. Your answer is also correct. – Matt Johnson-Pint Sep 27 '12 at 16:29
  • That shouldn't have to do with why the data isn't being received. – Cole Tobin Sep 27 '12 at 16:31
  • Adding `POSTstr.Close()` (request stream) still does not solve the issue; I still get the "no argument" response. – LMS Sep 27 '12 at 16:31
  • 1
    Use fiddler (www.fiddler2.com) to check what is actually being sent. Then you'll know whether to focus on the .Net side or on the PHP side. – Matt Johnson-Pint Sep 27 '12 at 16:40
  • Sorry for the long time without a reply. I had to go suddenly. Anyway, I used Fiddler, and it appears to me to be on the PHP side. I compared a request to an Imgur server with the request to my server, and the request to my server appears to be in-order. http://i.imgur.com/NjHhG.png – LMS Sep 28 '12 at 14:13
0

I've found the problem.

It turns out I'd left off the trailing / in the request, and the request was getting 301'd, which for whatever reason removed the post variables.

So http://82.41.255.140/api/post-ptr should have been http://82.41.255.140/api/post-ptr/.

Thanks to everyone who answered, I've +1'd you all.

LMS
  • 4,117
  • 5
  • 27
  • 37