MailChimp csharp Api calls

Code Example - MailChimp csharp Api calls

                
                        private string CallMailChimpApi(string method, string requestJson, string key)
    {
        var endpoint = String.Format("https://{0}.api.mailchimp.com/3.0/{1}", "<datacenter>", method);
        byte[] dataStream = Encoding.UTF8.GetBytes(requestJson);
        var responsetext = string.Empty;
        WebRequest request = HttpWebRequest.Create(endpoint);
        WebResponse response = null;
        try
        {
            request.ContentType = "application/json";
            SetBasicAuthHeader(request, "anystring", key);  // BASIC AUTH
            request.Method = "POST";
            request.ContentLength = dataStream.Length;
            Stream newstream = request.GetRequestStream();

            newstream.Write(dataStream, 0, dataStream.Length);
            newstream.Close();

            response = request.GetResponse();

            // get the result
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                JsonSerializer json = new JsonSerializer();
                JObject content = JObject.Parse(reader.ReadToEnd());

                responsetext = reader.ReadToEnd();
            }

            response.Close();
        }

        catch(WebException ex)
        {
            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                responsetext = sr.ReadToEnd();
            }
        }
        return responsetext;
    }