2013-06-25 45 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'等不被识别。 –