简体   繁体   中英

Send data with POST method to a PHP file using C#


I'm create an app that collect some data from user's computer
then I want to send them to a PHP file that receive data then php valid the code then it return a text and I want to show it in a text box in my application.
My questions are how can I post data in c# and collect a text that php return after data valid .
A text that php return is this format:

echo $validcode;

to send PHP data over C# you can use a NameValueCollection which is part of the System.Collections.Specialized

So, you should initialize it as following:

NameValueCollection dataToSend=new NameValueCollection();

Then, for each PHP field, you should do this:

dataToSend['fieldname']=data;
dataToSend['fieldname2']=data2;
using(WebClient wc=new WebClient())
{
   byte[] resp=wc.UploadValues(URL,dataToSend);
}

fieldname and fieldname2 will be the POST variables which u will use in your PHP, for example:

C#

dataToSend['fieldname']=data;
using(WebClient wc=new WebClient())
{
   byte[] resp=wc.UploadValues(URL,dataToSend);
}

And PHP:

echo $_POST['fieldname'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM