scottm scottm - 2 years ago 178
PHP Question

How can I send an array as a parameter of a query string in c#?

I am trying to use a home grown web API to retrieve some data. The documentation is all written in PHP. The example I'm looking at is this:

$params = array(
'id' => 1
,'data' => array(
,'email' => ''

$url = "" . http_build_query( $params );

I'm using the C# WebClient class, but I can't figure out how to serialize the data parameter:

WebClient wc = new WebClient();
wc.QueryString["id"] = "1";
wc.QueryString["data"] = // I have no idea.

string json = wc.DownloadString(apiUrl);

I've tried a few variations:

wc.QueryString["data"] = "";
wc.QueryString["data"] = Uri.EscapeDataString("data[email]");
wc.QueryString["data"] = Uri.EscapeDataString("email[0]");
wc.QueryString["data"] = Uri.EscapeDataString("");

Of course, I don't have PHP setup anywhere to see what the http_build_query() is actually returning.

Answer Source

I finally figured it out. I guess posting this question rebooted my brain.

This is what worked:

wc.QueryString["data[email]"] = "";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download