Convert a php array to something similar in C# and send it via cURL/HttpWebRequest -
hi new c# , first question posted here. accustomed php, need c# solution. trying build array similar following in c# later converted json , sent via curl or guess httpwebrequest in c#. best solution?
$member_data = array( "email" => $email, "fields" => array( "first_name" => $first_name, "last_name" => $last_name ), "group_ids" => $groups );
this curl converting httpwebrequest
$ch = curl_init(); curl_setopt($ch, curlopt_userpwd, $public_api_key . ":" . $private_api_key); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_post, count($member_data)); curl_setopt($ch, curlopt_postfields, json_encode($member_data)); curl_setopt($ch, curlopt_httpheader, array('content-type: application/json')); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_ssl_verifypeer, false); $head = curl_exec($ch); $http_code = curl_getinfo($ch, curlinfo_http_code); curl_close($ch);
thanks assistance!
i decided go httpclient , converted collected data directly json string instead of messing array conversion , able work.
public static void addsubscriber(string firstname, string lastname, string email) { var addmemberurl = baseurl + "/members/add"; int groupid = 1927094; string jsonstring = "{\"email\":\"" + email + "\",\"fields\":{\"first_name\":\"" + firstname + "\",\"last_name\":\"" + lastname + "\"},\"group_ids\":[" + groupid + "]}"; using (var httpclient = new httpclient()) { try { httpclient.defaultrequestheaders.authorization = new authenticationheadervalue("basic", convert.tobase64string( system.text.asciiencoding.ascii.getbytes( string.format("{0}:{1}", publicapikey, privateapikey)))); httpclient.defaultrequestheaders.accept.clear(); httpclient.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); httpclient.defaultrequestheaders.add("x-version", "1"); stringcontent stringcontent = new stringcontent(jsonstring); stringcontent.headers.contenttype = new mediatypeheadervalue("application/json"); var response = httpclient.postasync(addmemberurl, stringcontent); var statustext = response.result; system.diagnostics.debug.writeline(statustext); } catch (exception) { throw; } } }
Comments
Post a Comment