Beispiel #1
0
        /// <summary>
        /// 获取响应信息
        /// </summary>
        /// <param name="resquestEntity"></param>
        /// <returns></returns>
        public static HttpResponseEntity SendValue(HttpResquestEntity resquestEntity)
        {
            HttpResponseEntity responseEntity = new HttpResponseEntity();
            string             responseStr    = "";
            HttpStatusCode     httpStatusCode;
            HttpWebRequest     request = CreateRequest(resquestEntity);

            try
            {
                using (HttpWebResponse wr = (HttpWebResponse)request.GetResponse())
                {
                    httpStatusCode = wr.StatusCode;
                    Stream       dataStream = wr.GetResponseStream();
                    StreamReader reader     = new StreamReader(dataStream, resquestEntity.URLEncoding);
                    responseStr = reader.ReadToEnd();
                    reader.Close();
                    dataStream.Close();
                }
            }
            catch (WebException ex)
            {
                httpStatusCode          = GetHttpStatusCode(ex);
                responseStr            += "";
                responseEntity.ErrorMsg = ex.Message;
            }
            catch (Exception e)
            {
                httpStatusCode          = HttpStatusCode.BadRequest;
                responseStr            += "";
                responseEntity.ErrorMsg = e.Message;
            }
            responseEntity.ResponseContext        = responseStr;
            responseEntity.ResponseHttpStatusCode = httpStatusCode;
            return(responseEntity);
        }
Beispiel #2
0
        /// <summary>
        /// 只发送不接收
        /// </summary>
        /// <param name="resquestEntity"></param>
        public static void SendOnly(HttpResquestEntity resquestEntity)
        {
            HttpWebRequest request = CreateRequest(resquestEntity);

            try
            {
                request.GetResponse().Close();//销毁关闭响应
            }
            catch (Exception ex)
            {
                if (!ex.Message.Contains("操作超时"))
                {
                    throw ex;
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// 只是获取响应码
        /// </summary>
        /// <param name="resquestEntity"></param>
        /// <returns></returns>
        public static HttpStatusCode SendStatus(HttpResquestEntity resquestEntity)
        {
            HttpWebRequest request = CreateRequest(resquestEntity);

            try
            {
                using (HttpWebResponse wr = (HttpWebResponse)request.GetResponse())
                {
                    //返回状态
                    return(wr.StatusCode);
                }
            }
            catch (WebException ex)
            {
                return(GetHttpStatusCode(ex));
            }
        }
Beispiel #4
0
        /// <summary>
        /// 发生get请求
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="postDataStr"></param>
        /// <returns></returns>
        public static string HttpGet(string Url, string postDataStr)
        {
            HttpResquestEntity requestEntity = new HttpResquestEntity()
            {
                Url        = Url,
                SendData   = postDataStr,
                MethodType = HttpMethodType.GET
            };

            HttpWebRequest  request          = CreateRequest(requestEntity);
            HttpWebResponse response         = (HttpWebResponse)request.GetResponse();
            Stream          myResponseStream = response.GetResponseStream();
            StreamReader    myStreamReader   = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string          retString        = myStreamReader.ReadToEnd();

            myStreamReader.Close();
            myResponseStream.Close();

            return(retString);
        }
Beispiel #5
0
        /// <summary>
        /// 创建请求对象
        /// </summary>
        /// <param name="resquestEntity"></param>
        /// <returns></returns>
        private static HttpWebRequest CreateRequest(HttpResquestEntity resquestEntity)
        {
            if (resquestEntity.Url.ToLower().StartsWith("https"))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);//验证服务器证书回调自动验证
                System.Net.ServicePointManager.SecurityProtocol         = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            }
            HttpWebRequest request;

            if (resquestEntity.MethodType == HttpMethodType.GET)
            {
                string getUrl = resquestEntity.Url + "?" + resquestEntity.SendData;
                request             = (HttpWebRequest)HttpWebRequest.Create(getUrl);
                request.Method      = "GET";
                request.ContentType = "text/html;charset=UTF-8";
                request.Timeout     = resquestEntity.Timeout;
            }
            else
            {
                request = (HttpWebRequest)HttpWebRequest.Create(resquestEntity.Url);

                if (!string.IsNullOrEmpty(resquestEntity.HeaderKey))
                {
                    request.Headers.Add(resquestEntity.HeaderKey, resquestEntity.HeaderValue);
                    request.Accept = "application/json";
                }

                request.ContentType = GetContentTypeName(resquestEntity.ContentType) + "charset=" + resquestEntity.URLEncoding.HeaderName;
                request.Method      = "POST";// EnumHelper.GetName<string>(resquestEntity.MethodType);

                byte[] bData = (resquestEntity.URLEncoding.GetBytes(resquestEntity.SendData));
                request.ContentLength = bData.Length;
                request.Timeout       = resquestEntity.Timeout;
                Stream writeStream = request.GetRequestStream();
                writeStream.Write(bData, 0, bData.Length);
                writeStream.Close();
            }
            return(request);
        }
Beispiel #6
0
        /// <summary>
        /// 发生post请求
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="postDataStr"></param>
        /// <returns></returns>
        public static async Task <string> DoHttpPostAsync(string Url, string postDataStr, HttpContentType contentType = HttpContentType.JSON, int timeOut = 300000)
        {
            HttpResquestEntity requestEntity = new HttpResquestEntity()
            {
                Url         = Url,
                SendData    = postDataStr,
                Timeout     = timeOut,
                ContentType = contentType
            };

            HttpWebRequest request = CreateRequest(requestEntity);

            //using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            using (var response = await request.GetResponseAsync())
            {
                Stream       myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader   = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string       retString        = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return(retString);
            }
        }