2011-02-06 22 views
1

我正在嘗試構建一個C#表單應用程序與我的php網頁進行交互。它使HTTP文件發佈到PHP頁面,但PHP頁面期望一個數組。Http php中的C#數組#

如果我在PHP,而不是C#這樣做好像:(它的工作原理)

<?php 
$postArray=array("a"=>"1","b"=>"2","c"=>3); 
.... 
curl_setopt($curl,CURLOPT_POSTFIELDS,$postArray); 
... 
?> 

C#代碼:(不工作)

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)) 

所以,我怎樣才能發佈此PHP數組在C#中?我嘗試了哈希表和字典,但有些東西是錯誤的。請幫幫我。謝謝。

+1

顯示你的c#代碼,你有(甚至不工作) – Andrey 2011-02-06 19:29:46

回答

1

出於某種原因,我不知道這是否會有所幫助,但PHP將數組作爲單獨的字段。

例如,如果您有數組$some_array = array('a' => 1, 'b' => 2),並且您將其作爲GET發佈,則該URL將包含?some_array[a]=1&some_array[b]=2

這應該在POST請求中工作,只有它在請求的主體中,而不是URL。