private APIBaseResponse _execute(string url, APIBaseMethod method, List <APIBaseParameter> headers, dynamic body, List <APIBaseParameter> pathParam = null) { var rst = new APIBaseResponse { ResponseStatus = -1, }; try { rst.ResponseString = string.Empty; var stringBody = string.Empty; // Make Url var uri = buildUrl(url, method, body, pathParam); // Make body if (body != null && method != APIBaseMethod.GET) { stringBody = json2String(body); //stringBody = stringBody.Replace("\\\\", "\\"); } return(_executeBase(uri, method, headers, stringBody)); } catch (Exception ex) { if (rst.Error == null) { rst.Error = ex; } } return(rst); }
private string buildUrl(string url, APIBaseMethod method, dynamic body, List <APIBaseParameter> pathParam = null) { var ret = (url + "").Trim(); var hasParam = ret.Contains("?"); var hasParamVal = ret.Contains("="); var tmp = string.Empty; if (pathParam != null) { foreach (var param in pathParam) { tmp += "&" + param.Key + "=" + (string.IsNullOrEmpty(param.StringValue) ? (param.Value + "") : param.StringValue); } } var vBody = body as List <APIBaseParameter>; if (method == APIBaseMethod.GET && vBody != null) { foreach (var param in vBody) { tmp += "&" + param.Key + "=" + (string.IsNullOrEmpty(param.StringValue) ? (param.Value + "") : param.StringValue); } } if (string.IsNullOrEmpty(tmp)) { return(ret); } if (hasParam) { if (hasParamVal) { ret += tmp; } else { ret += tmp.Substring(1); } } return(ret); }
private APIBaseResponse _executeBase(string url, APIBaseMethod method, List <APIBaseParameter> headers, string strBody) { var rst = new APIBaseResponse { ResponseStatus = -1, }; try { rst.ResponseString = string.Empty; try { if (EnabelSSL3) { try { ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12; } catch (Exception) { } } var uri = new Uri(url); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.ContentType = ContentType; request.Method = method + string.Empty; request.KeepAlive = false; //request.UseDefaultCredentials = true; // Add header if (headers != null) { foreach (var header in headers) { if (string.IsNullOrEmpty(header.Key)) { continue; } if (header.Key.ToLower() == "content-type") { continue; } if (header.Key.ToLower() == "host") { //request.Host = (string.IsNullOrEmpty(header.StringValue) ? (header.Value + "") : header.StringValue); //request.Headers.Add(header.Key, (string.IsNullOrEmpty(header.StringValue) ? (header.Value + "") : header.StringValue)); continue; } request.Headers.Add(header.Key, (string.IsNullOrEmpty(header.StringValue) ? (header.Value + "") : header.StringValue)); } } // Add cert if (Certificate != null) { request.ClientCertificates.Clear(); request.ClientCertificates.Add(Certificate); } else { if (uri.Scheme.ToLower() == "https") { ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => { return(true); }; } } if (method != APIBaseMethod.GET && !string.IsNullOrWhiteSpace(strBody)) { using (var streamWriter = new StreamWriter(request.GetRequestStream())) { streamWriter.Write(strBody); streamWriter.Flush(); streamWriter.Close(); } } var response = (WebResponse)request.GetResponse(); using (var streamReader = new StreamReader(response.GetResponseStream())) { rst.ResponseString = streamReader.ReadToEnd(); } rst.ResponseStatus = 200; } catch (WebException e) { rst.Error = e; if (e.Response != null) { using (WebResponse wresponse = e.Response) { HttpWebResponse httpResponse = (HttpWebResponse)wresponse; rst.ResponseStatus = (int)httpResponse.StatusCode; using (Stream data = wresponse.GetResponseStream()) using (var reader = new StreamReader(data)) { rst.ResponseString = reader.ReadToEnd(); } } } } } catch (Exception ex) { if (rst.Error == null) { rst.Error = ex; } } return(rst); }