/// <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);
        }
Exemple #3
0
        /// <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 async Task <WebResponse> 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 = await WebRequestPolicy.GetPoliciesAsync(Option).ExecuteAndCaptureAsync(() => request.GetResponseAsync());

            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)
        {
            var request = (HttpWebRequest)WebRequest.Create(targetUri);

            request.Method           = HttpMethod.Get.Method;
            request.Accept           = "application/json";
            request.ReadWriteTimeout = Option.TimeoutSeconds;

            if (httpRequestHeaderParam != null)
            {
                request = httpRequestHeaderParam.SetHeader(request);
            }

            if (fields != null && fields.Count != 0)
            {
                var body = JsonSerializer.Serialize(fields);

                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>
 /// <returns></returns>
 public HttpWebResponse Get(Uri targetUri, HttpRequestHeaderParam httpRequestHeaderParam)
 {
     return(this.Get(targetUri, httpRequestHeaderParam, null));
 }
 /// <summary>
 /// Request a resource asynchronously.
 /// </summary>
 /// <param name="targetUri"></param>
 /// <param name="httpRequestHeaderParam"></param>
 /// <returns></returns>
 public HttpWebResponse Delete(Uri targetUri, HttpRequestHeaderParam httpRequestHeaderParam)
 {
     return(Delete(targetUri, httpRequestHeaderParam, null));
 }
 /// <summary>
 /// Patch
 /// </summary>
 /// <param name="targetUri"></param>
 /// <param name="requestBody"></param>
 /// <param name="httpRequestHeaderParam"></param>
 /// <returns></returns>
 public HttpWebResponse Patch(Uri targetUri, object requestBody, HttpRequestHeaderParam httpRequestHeaderParam)
 {
     return(Patch(targetUri, requestBody, httpRequestHeaderParam, true));
 }
 /// <summary>
 /// Request a resource asynchronously.
 /// </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)
 {
     return(Put(targetUri, requestBody, httpRequestHeaderParam, bodyJsonSerialize));
 }
 /// <summary>
 /// Request a resource asynchronously.
 /// </summary>
 /// <param name="targetUri"></param>
 /// <param name="httpRequestHeaderParam"></param>
 /// <returns></returns>
 public async Task <WebResponse> Get(Uri targetUri, HttpRequestHeaderParam httpRequestHeaderParam)
 {
     return(await Get(targetUri, httpRequestHeaderParam, null));
 }
 /// <summary>
 /// Request a resource asynchronously.
 /// </summary>
 /// <param name="targetUri"></param>
 /// <param name="requestBody"></param>
 /// <param name="httpRequestHeaderParam"></param>
 /// <returns></returns>
 public async Task <WebResponse> Patch(Uri targetUri, object requestBody, HttpRequestHeaderParam httpRequestHeaderParam)
 {
     return(await Patch(targetUri, requestBody, httpRequestHeaderParam, true));
 }
 /// <summary>
 /// Request a resource asynchronously.
 /// </summary>
 /// <param name="targetUri"></param>
 /// <param name="requestBody"></param>
 /// <param name="httpRequestHeaderParam"></param>
 /// <param name="bodyJsonSerialize"></param>
 /// <returns></returns>
 public async Task <WebResponse> Put(Uri targetUri, object requestBody, HttpRequestHeaderParam httpRequestHeaderParam, bool bodyJsonSerialize)
 {
     return(await Put(targetUri, requestBody, httpRequestHeaderParam, bodyJsonSerialize));
 }