-1

I need to create the following PHP POST in C#

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

I cannot figure out how to create the params array. I have tried with WebClient.UploadString() with a json string to no avail.

How do I construct the above in C# ?

I try

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

The above works but needs further parameters.

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

P1 is not recognised.

I've tried with UploadValues() but cannot store the params in a NamedValueCollection

The API is https://secure-test.be2bill.com/front/service/rest/process

Sam Leach
  • 12,746
  • 9
  • 45
  • 73

2 Answers2

2

Like explained here: http://www.codingvision.net/networking/c-sending-data-using-get-or-post/

it should work like this:

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 maybe you want to use post method... look at the link

in your example for

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

it should be:

string urlAddress = "http://www.yoursite.tld/somepage.php?method=foo&params[P1]=bar1&params[P2]=bar2&params[P3]=bar3";  
steven
  • 4,868
  • 2
  • 28
  • 58
0

I assume you need use the POST method to post data. Many times the error is that you did not set correct request headers.

Here is a solution that should work (first posted by Robin Van Persi in 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);
}

If this does not solve your problem, try more solutions in the answers in link above.

Community
  • 1
  • 1
Legoless
  • 10,942
  • 7
  • 48
  • 68