1

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.

4
  • 1
    show your c# code that you have (even not working) Commented Feb 6, 2011 at 19:29
  • maybe it was a typo here and not in your actual code but fyi in a php array it should be "a"=>"1" not "a"->"1" Commented Feb 6, 2011 at 19:42
  • have you checked in Fiddler.. are both of them sending same data.. if not what are the differences Commented Feb 6, 2011 at 20:39
  • I am trying to do it in C#. I tried in php. Php worked as i expected but still C# not working. We should post php array. localhost/index.php splits and uses it. Commented Feb 6, 2011 at 20:45

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.