private HttpWebResponse RequestV3Sync(AbstractModel request, string actionName) { string httpRequestMethod = this.Profile.HttpProfile.ReqMethod; string canonicalQueryString = this.BuildCanonicalQueryString(request); string requestPayload = this.BuildRequestPayload(request); string contentType = this.BuildContentType(); Dictionary <string, string> headers = this.BuildHeaders(contentType, requestPayload, canonicalQueryString); headers.Add("X-TC-Action", actionName); string endpoint = headers["Host"]; HttpConnection conn = new HttpConnection( $"{this.Profile.HttpProfile.Protocol }{endpoint}", this.Profile.HttpProfile.Timeout, this.Profile.HttpProfile.WebProxy); try { if (this.Profile.HttpProfile.ReqMethod == HttpProfile.REQ_GET) { return(conn.GetRequestSync(this.Path, canonicalQueryString, headers)); } else { return(conn.PostRequestSync(this.Path, requestPayload, headers)); } } catch (Exception e) { throw new TencentCloudSDKException($"The request with exception: {e.Message}"); } }
private Dictionary <string, string> BuildParam(AbstractModel request, string actionName) { Dictionary <string, string> param = new Dictionary <string, string>(); request.ToMap(param, ""); // inplace change this.FormatRequestData(actionName, param); return(param); }
public async Task <string> InternalRequest(AbstractModel request, string actionName) { if ((this.Profile.HttpProfile.ReqMethod != HttpProfile.REQ_GET) && (this.Profile.HttpProfile.ReqMethod != HttpProfile.REQ_POST)) { throw new TencentCloudSDKException("Method only support (GET, POST)"); } IResponse response = null; if (ClientProfile.SIGN_SHA1.Equals(this.Profile.SignMethod) || ClientProfile.SIGN_SHA256.Equals(this.Profile.SignMethod)) { response = await RequestV1(request, actionName); } else { response = await RequestV3(request, actionName); } if ((int)response.Status != HTTP_RSP_OK) { throw new TencentCloudSDKException(response.Status + await response.Message.Content.ReadAsStringAsync()); } string strResp = null; try { strResp = await response.AsString(); } catch (ApiException ex) { string responseText = await ex.Response.AsString(); throw new TencentCloudSDKException($"The API responded with HTTP {ex.Response.Status}: {responseText}"); } JsonResponseModel <JsonResponseErrModel> errResp = null; try { errResp = JsonConvert.DeserializeObject <JsonResponseModel <JsonResponseErrModel> >(strResp); } catch (JsonSerializationException e) { throw new TencentCloudSDKException(e.Message); } if (errResp.Response.Error != null) { throw new TencentCloudSDKException($"code:{errResp.Response.Error.Code} message:{errResp.Response.Error.Message} ", errResp.Response.RequestId); } return(strResp); }
private string BuildRequestPayload(AbstractModel request) { string httpRequestMethod = this.Profile.HttpProfile.ReqMethod; if (HttpProfile.REQ_GET.Equals(httpRequestMethod)) { return(""); } return(JsonConvert.SerializeObject(request, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore })); }
private string BuildCanonicalQueryString(AbstractModel request) { string httpRequestMethod = this.Profile.HttpProfile.ReqMethod; if (!HttpProfile.REQ_GET.Equals(httpRequestMethod)) { return(""); } Dictionary <string, string> param = new Dictionary <string, string>(); request.ToMap(param, ""); StringBuilder urlBuilder = new StringBuilder(); foreach (KeyValuePair <string, string> kvp in param) { urlBuilder.Append($"{WebUtility.UrlEncode(kvp.Key)}={WebUtility.UrlEncode(kvp.Value)}&"); } return(urlBuilder.ToString().TrimEnd('&')); }
private HttpWebResponse RequestV1Sync(AbstractModel request, string actionName) { HttpWebResponse response = null; Dictionary <string, string> param = BuildParam(request, actionName); HttpConnection conn = this.BuildConnection(); try { if (this.Profile.HttpProfile.ReqMethod == HttpProfile.REQ_GET) { response = conn.GetRequestSync(this.Path, param); } else if (this.Profile.HttpProfile.ReqMethod == HttpProfile.REQ_POST) { response = conn.PostRequestSync(this.Path, param); } } catch (Exception ex) { throw new TencentCloudSDKException($"The request with exception: {ex.Message }"); } return(response); }
public string InternalRequestSync(AbstractModel request, string actionName) { if ((this.Profile.HttpProfile.ReqMethod != HttpProfile.REQ_GET) && (this.Profile.HttpProfile.ReqMethod != HttpProfile.REQ_POST)) { throw new TencentCloudSDKException("Method only support (GET, POST)"); } HttpWebResponse response = null; if (ClientProfile.SIGN_SHA1.Equals(this.Profile.SignMethod) || ClientProfile.SIGN_SHA256.Equals(this.Profile.SignMethod)) { response = RequestV1Sync(request, actionName); } else { response = RequestV3Sync(request, actionName); } HttpStatusCode statusCode = response.StatusCode; if (statusCode != HttpStatusCode.OK) { Encoding encoding = Encoding.UTF8; using (Stream stream = response.GetResponseStream()) { using (StreamReader sr = new StreamReader(response.GetResponseStream(), encoding)) { string content = sr.ReadToEnd().ToString(); throw new TencentCloudSDKException(statusCode.ToString() + content); } } } string strResp = null; try { Encoding encoding = Encoding.UTF8; using (Stream stream = response.GetResponseStream()) { using (StreamReader sr = new StreamReader(response.GetResponseStream(), encoding)) { strResp = sr.ReadToEnd().ToString(); } } } catch (Exception ex) { string responseText = ex.Message; throw new TencentCloudSDKException($"The API responded with HTTP {responseText}"); } JsonResponseModel <JsonResponseErrModel> errResp = null; try { errResp = JsonConvert.DeserializeObject <JsonResponseModel <JsonResponseErrModel> >(strResp); } catch (JsonSerializationException e) { throw new TencentCloudSDKException(e.Message); } if (errResp.Response.Error != null) { throw new TencentCloudSDKException($"code:{errResp.Response.Error.Code} message:{errResp.Response.Error.Message} ", errResp.Response.RequestId); } return(strResp); }