示例#1
0
        //PostAsync
        internal static HttpRequestDetail SendHttpWebRequest(Uri endpoint, string method, string dataToSend,
                                                             NameValueCollection headerData = null, int?connectionTimeout = null)
        {
            var webRequest = CreateHttpRequest(endpoint, connectionTimeout);

            webRequest.Method = method;
            webRequest.AddHeader(headerData).AddBody(dataToSend);

            var requestDetail = new HttpRequestDetail
            {
                Url        = webRequest.RequestUri.AbsoluteUri,
                Method     = webRequest.Method,
                Header     = headerData,
                RawRequest = dataToSend
            };

            try
            {
                using var webResponse = webRequest.GetResponse() as HttpWebResponse;
                var streamReader = new StreamReader(webResponse.GetResponseStream());
                requestDetail.RawResponse        = streamReader.ReadToEnd();
                requestDetail.ResponseStatusCode = webResponse.StatusCode;
            }
            catch (WebException ex)
            {
                if (ex.Response == null)
                {
                    throw;
                }

                using var webResponse = (HttpWebResponse)ex.Response;
                var test = new StreamReader(webResponse.GetResponseStream());
                requestDetail.RawResponse        = test.ReadToEnd();
                requestDetail.ResponseStatusCode = webResponse.StatusCode;
            }


            return(requestDetail);
        }
 public HttpException(HttpRequestDetail detail)
 {
     this.RequestDetail = detail;
 }