internal static string DoRequest(string path, string method, Dictionary <string, object> param = null) { if (string.IsNullOrEmpty(ApiKey)) { throw new PingppException("No API key provided. (HINT: set your API key using " + "\"Pingpp::setApiKey(<API-KEY>)\". You can generate API keys from " + "the Pingpp web interface. See https://pingxx.com/document/api for " + "details."); } try { HttpWebRequest req; HttpWebResponse res; method = method.ToUpper(); switch (method) { case "GET": case "DELETE": req = GetRequest(path, method, ""); using (res = req.GetResponse() as HttpWebResponse) { return(res == null ? null : ReadStream(res.GetResponseStream())); } case "POST": case "PUT": if (param == null) { throw new PingppException("Request params is empty"); } var body = JsonConvert.SerializeObject(param, Formatting.Indented); string sign; try { sign = RsaUtils.RsaSign(body, PrivateKey); } catch (System.Exception e) { throw new PingppException("Sign request error." + e.Message); } req = GetRequest(path, method, sign); using (var streamWriter = new StreamWriter(req.GetRequestStream())) { streamWriter.Write(body); streamWriter.Flush(); streamWriter.Close(); } using (res = req.GetResponse() as HttpWebResponse) { return(res == null ? null : ReadStream(res.GetResponseStream())); } default: return(null); } } catch (WebException e) { if (e.Response == null) { throw new WebException(e.Message); } var statusCode = ((HttpWebResponse)e.Response).StatusCode; var errors = Mapper <Error> .MapFromJson(ReadStream(e.Response.GetResponseStream()), "error"); throw new PingppException(errors, errors.ErrorType, errors.Message); } }
internal static async Task <string> DoRequest(string path, string method, Dictionary <string, object> param = null) { if (string.IsNullOrEmpty(ApiKey)) { throw new PingppException("No API key provided. (HINT: set your API key using " + "\"Pingpp::setApiKey(<API-KEY>)\". You can generate API keys from " + "the Pingpp web interface. See https://pingxx.com/document/api for " + "details."); } try { HttpContent httpContent = null; string sign = string.Empty; if (method.ToUpper() == "POST" || method.ToUpper() == "PUT") { if (param == null) { throw new PingppException("Request params is empty"); } string jsonBody = JsonConvert.SerializeObject(param, Formatting.Indented); try { sign = RsaUtils.RsaSign(jsonBody, PrivateKey); } catch (System.Exception e) { throw new PingppException("Sign request error." + e.Message); } httpContent = new StringContent(jsonBody); httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;charset=utf-8"); } HttpClient req; HttpResponseMessage res; method = method.ToUpper(); switch (method) { case "GET": req = GetRequest(""); using (res = await req.GetAsync(path)) { return(await res.Content.ReadAsStringAsync()); } case "DELETE": req = GetRequest(""); using (res = await req.DeleteAsync(path)) { return(await res.Content.ReadAsStringAsync()); } case "POST": req = GetRequest(sign); using (res = await req.PostAsync(path, httpContent)) { return(await res.Content.ReadAsStringAsync()); } case "PUT": req = GetRequest(sign); using (res = await req.PutAsync(path, httpContent)) { return(await res.Content.ReadAsStringAsync()); } default: return(null); } } catch (WebException e) { if (e.Response == null) { throw new WebException(e.Message); } var statusCode = ((HttpWebResponse)e.Response).StatusCode; var errors = Mapper <Error> .MapFromJson(ReadStream(e.Response.GetResponseStream()), "error"); throw new PingppException(errors, statusCode, errors.ErrorType, errors.Message); } }
internal static string DoRequest(string path, string method, Dictionary <string, object> param = null, bool isValidateUri = true) { if (string.IsNullOrEmpty(ApiKey)) { throw new PingppException("No API key provided. (HINT: set your API key using " + "\"Pingpp::setApiKey(<API-KEY>)\". You can generate API keys from " + "the Pingpp web interface. See https://pingxx.com/document/api for " + "details."); } try { HttpWebRequest req; HttpWebResponse res; method = method.ToUpper(); string body = "", sign = ""; string timestamp = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString(); if ((method.Equals("POST") || method.Equals("PUT")) && param != null) { body = JsonConvert.SerializeObject(param, Formatting.Indented); } // Sign the request try { if (PrivateKey != null) { var uri = isValidateUri ? path : ""; sign = RsaUtils.RsaSign(body + uri + timestamp, PrivateKey); } } catch (System.Exception e) { throw new PingppException("Sign request error." + e.Message); } req = GetRequest(path, method, timestamp, sign); if (method.Equals("POST") || method.Equals("PUT")) { using (var streamWriter = new StreamWriter(req.GetRequestStream())) { streamWriter.Write(body); streamWriter.Flush(); streamWriter.Close(); } } using (res = req.GetResponse() as HttpWebResponse) { return(res == null ? null : ReadStream(res.GetResponseStream())); } } catch (WebException e) { if (e.Response == null) { throw new WebException(e.Message); } var statusCode = ((HttpWebResponse)e.Response).StatusCode; if ((int)statusCode == 502 && BadGateWayMatch && MaxRetry < MaxNetworkRetries) { MaxRetry++; DoRequest(path, method, param, isValidateUri); } var errors = Mapper <Error> .MapFromJson(ReadStream(e.Response.GetResponseStream()), "error"); throw new PingppException(errors, statusCode, errors.ErrorType, errors.Message); } }