Reputation: 11
I am trying to build a C# form application interacts with my php web page. It makes HTTP file post to php page but php page expects an array.
If i do it in php instead of C# it seems like: (It works)
<?php
$postArray=array("a"=>"1","b"=>"2","c"=>3);
....
curl_setopt($curl,CURLOPT_POSTFIELDS,$postArray);
...
?>
C# code: (not working)
string boundary = "-----------------------------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create("localhost/index.php");
webrequest.CookieContainer = cookies;
webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
webrequest.Method = "POST";
webrequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; tr; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
webrequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
NameValueCollection n1 = new NameValueCollection();
n1.Add("a", "1");
n1.Add("b", "2");
n1.Add("c", "3");
using (var requestStream = webrequest.GetRequestStream())
using (var writer = new StreamWriter(requestStream))
{
writer.Write("POST_DATA=" + n1);
}
using (var responseStream = webrequest.GetResponse().GetResponseStream())
using (var reader = new StreamReader(responseStream))
So how can i post this php array in C#? I tried hashtable and dictionary but something is wrong in it. Please help me. Thanks.
Upvotes: 1
Views: 2088
Reputation: 13289
For some reason I'm not sure if this will help, but PHP takes in arrays as individual fields.
For instance, if you have the array $some_array = array('a' => 1, 'b' => 2)
, and you posted it as a GET, the URL would contain ?some_array[a]=1&some_array[b]=2
.
This should work the same in a POST request, only then it's in the body of the request, as opposed to the URL.
Upvotes: 1