0


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;
Jenifer_L
  • 3
  • 2
  • Basically, `WebClient.UploadData`, `UploadValues` etc: http://msdn.microsoft.com/en-us/library/tdbbwh0a(v=vs.110).aspx – Marc Gravell Oct 03 '14 at 08:38
  • 1
    possible duplicate of [HTTP request with post](http://stackoverflow.com/questions/4015324/http-request-with-post) – Robert Oct 03 '14 at 08:40

1 Answers1

2

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'];
CapitanFindus
  • 1,498
  • 15
  • 26
  • Tnx, I'm not so good at C# it throw and error in NameValueCollection "The type or namespace name 'NameValueCollection' could not be found". How can I fix it I searched in reference but there is no such NameValueCollection there. – Jenifer_L Oct 03 '14 at 09:17
  • add using system.collections.specialized to your project – CapitanFindus Oct 03 '14 at 09:19
  • Hi again! Sorry for this but I have another error :( I got error for this line: dataToSend['fieldname']=uniqueId; it said "too many characters in character literal" and in this line for using(WebClient wc=new WebClient()) said: "The type or namespace name 'WebClient' could not be found" How can I fix it? – Jenifer_L Oct 03 '14 at 16:33
  • You have to use double brackets, like dataToSend["fieldname"] and for the error related to WebClient you should add using System.Net at the begin of your project... You should read .Net guides on microsoft site to see the full documentation, and, just saying, this question has been asked about 1 million times. Try to avoid duplicates, there's a search bar at page top, and, at last, if I helped you, please check the answer as right – CapitanFindus Oct 03 '14 at 23:42