Beispiel #1
0
 /// <summary>
 /// 执行DELETE请求,获取返回的html
 /// </summary>
 /// <returns></returns>
 public string DeleteForString()
 {
     return(EasyHttpUtils.ReadAllAsString(ExecutForStream(Method.DELETE), _responseEncoding));
 }
Beispiel #2
0
        /// <summary>
        /// 根据指定方法执行请求,并返回原始Response
        /// </summary>
        /// <param name="method">http方法</param>
        /// <returns></returns>
        public HttpWebResponse Execute(Method method)
        {
            string url = string.Empty;

            //get方式直接拼接url
            if (method == Method.GET)
            {
                UrlToQuery(_url);
                url = this._url;
                if (_keyValues.Count > 0)
                {
                    //分解参数
                    url = url + "?" + EasyHttpUtils.NameValuesToQueryParamString(_keyValues);
                }
                _request = WebRequest.Create(url) as HttpWebRequest;

                EasyHttpUtils.CopyHttpHeader(_tempRequest, _defaultHeaderRequest, _request);

                _request.Method          = "GET";
                _request.CookieContainer = _cookieContainer;
                WriteHeader();
            }
            //post方式需要写入
            else if (method == Method.POST)
            {
                url      = _url;
                _request = _tempRequest;

                _request.CookieContainer = _cookieContainer;
                _request.Method          = "POST";
                EasyHttpUtils.CopyHttpHeader(_tempRequest, _defaultHeaderRequest, _request);
                WriteHeader();
                if (_isMultpart)
                {
                    EasyHttpUtils.WriteFileToRequest(_request, _keyValues);
                }
                else
                {
                    if (string.IsNullOrEmpty(_request.ContentType))
                    {
                        _request.ContentType = "application/x-www-form-urlencoded";
                    }
                    string querystring = EasyHttpUtils.NameValuesToQueryParamString(_keyValues);
                    //如果有自定义post内容,则写入自定义post数据,否则写入form
                    if (_customePostData != null)
                    {
                        querystring = _customePostData;
                    }
                    //写入到post
                    using (var stream = _request.GetRequestStream())
                    {
                        byte[] postData = _postEncoding.GetBytes(querystring);
                        stream.Write(postData, 0, postData.Length);
                        // Request.ContentLength = postData.Length;
                    }
                }
            }
            else if (method == Method.PUT)
            {
                UrlToQuery(_url);
                url = this._url;
                if (_keyValues.Count > 0)
                {
                    url = url + "?" + EasyHttpUtils.NameValuesToQueryParamString(_keyValues);
                }
                _request = WebRequest.Create(url) as HttpWebRequest;
                _request.CookieContainer = _cookieContainer;

                WriteHeader();
                EasyHttpUtils.CopyHttpHeader(_tempRequest, _defaultHeaderRequest, _request);
                _request.Method = "PUT";
            }
            else if (method == Method.DELETE)
            {
                UrlToQuery(_url);
                url = this._url;
                if (_keyValues.Count > 0)
                {
                    url = url + "?" + EasyHttpUtils.NameValuesToQueryParamString(_keyValues);
                }
                _request = WebRequest.Create(url) as HttpWebRequest;

                _request.CookieContainer = _cookieContainer;
                EasyHttpUtils.CopyHttpHeader(_tempRequest, _defaultHeaderRequest, _request);
                _request.Method = "DELETE";
                WriteHeader();
            }


            //Request.CookieContainer.Add(c);
            if (RequestInterceptor != null)
            {
                _response = RequestInterceptor.Invoke(_request);
            }
            else
            {
                _response = _request.GetResponse() as HttpWebResponse;
            }
            _cookieContainer.Add(_response.Cookies);

            if (_logLevel != EasyHttpLogLevel.None)
            {
                try
                {
                    LogRequet(url, method);
                    LogRespose(url, method);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                }
            }



            return(_response);
        }