/// <summary> /// head method /// </summary> /// <param name="targetUri"></param> /// <param name="httpRequestHeaderParam"></param> /// <returns></returns> public HttpWebResponse Head(Uri targetUri, HttpRequestHeaderParam httpRequestHeaderParam) { var request = (HttpWebRequest)WebRequest.Create(targetUri); request.Method = HttpMethod.Head.Method; request.Accept = "application/json"; request.ReadWriteTimeout = Option.TimeoutSeconds; if (httpRequestHeaderParam != null) { request = httpRequestHeaderParam.SetHeader(request); } if (Signer != null) { Signer.SignRequest(request); } var res = WebRequestPolicy.GetPolicies(Option).ExecuteAndCapture(() => (HttpWebResponse)request.GetResponse()); if (res.Outcome == OutcomeType.Failure && (res.FinalException is WebException)) { throw res.FinalException; } return(res.Result); }
/// <summary> /// Put a request object to the endpoint represented by the web target and get the response. /// </summary> /// <param name="targetUri"></param> /// <param name="requestBody"></param> /// <param name="httpRequestHeaderParam"></param> /// <param name="bodyJsonSerialize"></param> /// <param name="sha256"></param> /// <returns></returns> public HttpWebResponse Put(Uri targetUri, object requestBody, HttpRequestHeaderParam httpRequestHeaderParam, bool bodyJsonSerialize, bool sha256) { var request = (HttpWebRequest)WebRequest.Create(targetUri); request.Method = HttpMethod.Put.Method; request.Accept = "application/json"; request.ContentType = "application/json"; request.ReadWriteTimeout = Option.TimeoutSeconds; if (httpRequestHeaderParam != null) { request = httpRequestHeaderParam.SetHeader(request); } if (requestBody != null) { request = SetBody(request, requestBody, bodyJsonSerialize, sha256); } if (Signer != null) { Signer.SignRequest(request); } var res = WebRequestPolicy.GetPolicies(Option).ExecuteAndCapture(() => (HttpWebResponse)request.GetResponse()); if (res.Outcome == OutcomeType.Failure && (res.FinalException is WebException)) { throw res.FinalException; } return(res.Result); }
/// <summary> /// Get /// </summary> /// <param name="request"></param> /// <returns></returns> public HttpWebResponse Get(HttpWebRequest request) { var res = WebRequestPolicy.GetPolicies(Option).ExecuteAndCapture(() => (HttpWebResponse)request.GetResponse()); if (res.Outcome == OutcomeType.Failure && (res.FinalException is WebException)) { throw res.FinalException; } return(res.Result); }
/// <summary> /// Put a request object to the endpoint represented by the web target and get the response. /// </summary> /// <param name="targetUri"></param> /// <param name="requestBody"></param> /// <param name="httpRequestHeaderParam"></param> /// <param name="bodyJsonSerialize"></param> /// <returns></returns> public HttpWebResponse Put(Uri targetUri, object requestBody, HttpRequestHeaderParam httpRequestHeaderParam, bool bodyJsonSerialize) { var request = (HttpWebRequest)WebRequest.Create(targetUri); if (httpRequestHeaderParam != null) { request = httpRequestHeaderParam.SetHeader(request); } byte[] arr = CreateBodyBytes(request, requestBody, bodyJsonSerialize); request.ContentLength = arr.Length; request.Headers["x-content-sha256"] = Convert.ToBase64String(SHA256.Create().ComputeHash(arr)); request.ContentType = "*/*"; request.KeepAlive = true; if (WebProxy != null) { request.Proxy = WebProxy; } request.Method = HttpMethod.Put.Method; if (Signer != null) { Signer.SignRequest(request); } if ((requestBody as Stream).Length > (10 * this.MiB)) { request.Timeout = int.MaxValue; } using (Stream dataStream = request.GetRequestStream()) { dataStream.Write(arr, 0, arr.Length); } var res = WebRequestPolicy.GetPolicies(Option).ExecuteAndCapture(() => (HttpWebResponse)request.GetResponse()); if (res.Outcome == OutcomeType.Failure && (res.FinalException is WebException)) { throw res.FinalException; } return(res.Result); }
/// <summary> /// Patch a request object to the endpoint represented by the web target and get the response. /// </summary> /// <param name="targetUri"></param> /// <param name="requestBody"></param> /// <param name="httpRequestHeaderParam"></param> /// <returns></returns> public HttpWebResponse Patch(Uri targetUri, object requestBody, HttpRequestHeaderParam httpRequestHeaderParam) { var request = (HttpWebRequest)WebRequest.Create(targetUri); request.Method = HttpMethod.Patch.Method; request.Accept = "application/json"; request.ContentType = "application/json"; request.ReadWriteTimeout = Option.TimeoutSeconds; if (httpRequestHeaderParam != null) { request = httpRequestHeaderParam.SetHeader(request); } if (requestBody != null) { var body = JsonSerializer.Serialize(requestBody); var bytes = Encoding.UTF8.GetBytes(body); request.Headers["x-content-sha256"] = Convert.ToBase64String(SHA256.Create().ComputeHash(bytes)); request.ContentLength = bytes.Length; using (var stream = request.GetRequestStream()) { stream.Write(bytes, 0, bytes.Length); } } if (Signer != null) { Signer.SignRequest(request); } var res = WebRequestPolicy.GetPolicies(Option).ExecuteAndCapture(() => (HttpWebResponse)request.GetResponse()); if (res.Outcome == OutcomeType.Failure && (res.FinalException is WebException)) { throw res.FinalException; } return(res.Result); }
/// <summary> /// Request a resource asynchronously. /// </summary> /// <param name="targetUri"></param> /// <param name="httpRequestHeaderParam"></param> /// <param name="fields"></param> /// <returns></returns> public HttpWebResponse Get(Uri targetUri, HttpRequestHeaderParam httpRequestHeaderParam, List <string> fields) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; var request = (HttpWebRequest)WebRequest.Create(targetUri); if (httpRequestHeaderParam != null) { request = httpRequestHeaderParam.SetHeader(request); } string body = ""; if (fields != null && fields.Count != 0) { body = JsonSerializer.Serialize(fields); } var bytes = Encoding.UTF8.GetBytes(body); if (bytes.Length > 0) { request.Headers["x-content-sha256"] = Convert.ToBase64String(SHA256.Create().ComputeHash(bytes)); request.ContentLength = bytes.Length; } request.Method = HttpMethod.Get.Method; request.Accept = "application/json"; request.ReadWriteTimeout = Option.TimeoutSeconds; if (WebProxy != null) { request.Proxy = WebProxy; } if (httpRequestHeaderParam != null) { request = httpRequestHeaderParam.SetHeader(request); } if (Signer != null) { Signer.SignRequest(request); } if (bytes.Length > 0) { using (var stream = request.GetRequestStream()) { stream.Write(bytes, 0, bytes.Length); } } var res = WebRequestPolicy.GetPolicies(Option).ExecuteAndCapture(() => (HttpWebResponse)request.GetResponse()); if (res.Outcome == OutcomeType.Failure && (res.FinalException is WebException)) { throw res.FinalException; } return(res.Result); }