Hello everybody!I want to ask you something. I want to send values from C# windows form application in php webpage.In other words send post values from C# to php.

The problem is that php script cannot read this values and preview this results which comes from C# app.Could you help me please?

Here is a C# code which i've found:

        public string Post(string url, string data) {


           string vystup = null;
           try
           {
               //Our postvars
               byte[] buffer = Encoding.ASCII.GetBytes(data);
               //Initialisation, we use localhost, change if appliable
               HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
               //Our method is post, otherwise the buffer (postvars) would be useless
               WebReq.Method = "POST";
               //We use form contentType, for the postvars.
               WebReq.ContentType = "application/x-www-form-urlencoded";
               //The length of the buffer (postvars) is used as contentlength.
               WebReq.ContentLength = buffer.Length;
               //We open a stream for writing the postvars
               Stream PostData = WebReq.GetRequestStream();
               //Now we write, and afterwards, we close. Closing is always important!
               PostData.Write(buffer, 0, buffer.Length);
               PostData.Close();
               //Get the response handle, we have no true response yet!
               HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
               //Let's show some information about the response
               Console.WriteLine(WebResp.StatusCode);
               Console.WriteLine(WebResp.Server);

               //Now, we read the response (the string), and output it.
               Stream Answer = WebResp.GetResponseStream();
               StreamReader _Answer = new StreamReader(Answer);
               vystup =  _Answer.ReadToEnd();

               //Congratulations, you just requested your first POST page, you
               //can now start logging into most login forms, with your application
               //Or other examples.
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
           return vystup.Trim()+"\n";

        }

And a very simple php script to display the post data!

<?
print_r( $_POST); 
?>

Thanks a lot!

Dani AI

Generated

Short checklist and focused fixes (for ; thanks to for the pointer):

PHP only fills $_POST for standard form encodings — specifically application/x-www-form-urlencoded or multipart/form-data. If your C# client sends JSON or a raw body, PHP will not populate $_POST; use php://input or json_decode() instead. Also prefer UTF‑8 over ASCII when encoding the body. See the PHP docs on $_POST and php://input for details: PHP $_POST manual and php://input docs. (php.net)

A compact, modern C# approach: use HttpClient with FormUrlEncodedContent so the request is sent as application/x-www-form-urlencoded (PHP will then populate $_POST):

using System.Net.Http;

var client = new HttpClient();
var values = new List<KeyValuePair<string,string>> {
  new("name","Alice"),
  new("age","30")
};
var content = new FormUrlEncodedContent(values);
var resp = await client.PostAsync("https://yourserver/receive.php", content);
var respBody = await resp.Content.ReadAsStringAsync();

HttpClient and FormUrlEncodedContent are documented here. (learn.microsoft.com)

On the PHP side, to catch non-form bodies or to debug raw input, read and inspect the request body:

$raw = file_get_contents('php://input');
parse_str($raw, $data);   // if the body is url-encoded
// or
$json = json_decode($raw, true); // if client sent JSON
var_dump($data ?? $json);

Troubleshooting tips: reproduce the POST with curl to confirm server behavior, capture the outgoing request with Fiddler (or another proxy) to inspect headers/body, and check PHP limits like max_input_vars or post_max_size in php.ini. References: curl examples and PHP max_input_vars. (curl.se)

Recommended Answers

All 2 Replies

Nop, nothing done yet me friend!Any other idea please???

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.