2013-06-25 81 views
-1

我需要在C#創建以下PHP POSTC#中使用REST API之後的參數作爲數組

$params = array( 
'method' => 'method1', 
'params' => array 
    ( 
     'P1' => FOO,  
     'P2' => $Bar, 
     'P3' => $Foo, 
    ) 
); 

我無法弄清楚如何創建params陣列。我用WebClient.UploadString()與json字符串嘗試無濟於事。

如何在C#中構建上述代碼?

我嘗試

using (WebClient client = new WebClient()) 
    { 
     return client.UploadString(EndPoint, "?method=payment"); 
    } 

上述作品,但還需要進一步的參數。

using (WebClient client = new WebClient()) 
    {    
     return client.UploadString(EndPoint, "?method=foo&P1=bar"); 
    } 

P1無法識別。

我試着UploadValues()但不能PARAMS存儲在NamedValueCollection

的API是https://secure-test.be2bill.com/front/service/rest/process

+0

你在打什麼API?如果可能,閱讀文檔將會很酷。 –

+0

https://secure-test.be2bill.com/front/service/rest/process –

+0

什麼是downvotes? –

回答

2

喜歡這裏解釋:http://www.codingvision.net/networking/c-sending-data-using-get-or-post/

它應該像這樣工作:

string urlAddress = "http://www.yoursite.tld/somepage.php?method=foo&P1=bar1&P2=bar2&P3=bar3"; 

using (WebClient client = new WebClient()) 
{ 
     string response = client.DownloadString(urlAddress); 
} 

ob也許你想使用post方法...看看鏈接

在你的榜樣

$php_get_vars = array( 
'method' => 'foo', 
'params' => array 
    ( 
     'P1' => 'bar1',  
     'P2' => 'bar2', 
     'P3' => 'bar3', 
    ) 
); 

它應該是:

string urlAddress = "http://www.yoursite.tld/somepage.php?method=foo&params[P1]=bar1&params[P2]=bar2&params[P3]=bar3"; 
+0

數組內的參數'P1'等無法識別。 –

+0

嗯,它不是在示例中的數組,它應該是PHP中的_ _GET ['P1'] – steven

+0

method = foo&params [P1] = bar1&params [P2] = bar2&params [P3] = bar3 –

0

我假設你需要使用POST方法來發布數據。很多時候,錯誤是您沒有設置正確的請求標頭。

這裏是一個解決方案,它應該工作(第一帖由羅賓·PERSI在How to post data to specific URL using WebClient in C#):

string URI = "http://www.domain.com/restservice.php"; 
string params = "method=foo&P1=" + value1 + "&P2=" + value2 + "&P3=" + value3; 

using (WebClient wc = new WebClient()) 
{ 
    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; 
    string HtmlResult = wc.UploadString(URI, params); 
} 

如果這沒有解決您的問題,嘗試在上面的鏈接的答案更多的解決方案。

+0

數組中的參數'P1'等不被識別。 –