/// <summary> /// 通过HTTPGet方式获取远程页面数据. /// </summary> /// <param name="url">URL地址.</param> /// <param name="referer">引用地址.</param> /// <param name="errorMsg">输出错误消息.</param> /// <param name="encode">编码格式.</param> /// <returns> /// NetResponse /// </returns> public NetResponse Get(string url, string referer, out string errorMsg, string encode = "utf-8") { int failedTimes = _tryTimes; errorMsg = string.Empty; HttpStatusCode statusCode = HttpStatusCode.OK; string statusDes = ""; while (failedTimes-- > 0) { HttpWebRequest request = null; try { if (failedTimes < _tryTimes - 1) // 推迟处理,第一次请求不延迟 { Delay(); } // 获取网络请求对象 request = GetWebRequest(url, referer); request.Method = "GET"; if (_timeout > 0) { request.Timeout = _timeout; } using (WebResponse response = request.GetResponse()) { return(GetNetResponse((HttpWebResponse)response, Encoding.GetEncoding(encode))); } } catch (WebException ex) { errorMsg = ex.Message; if (ex.Status == WebExceptionStatus.ProtocolError) { HttpWebResponse webResponse = (HttpWebResponse)ex.Response; statusCode = webResponse.StatusCode; statusDes = webResponse.StatusDescription; } else { statusCode = HttpStatusCode.BadGateway; statusDes = ex.Message; } new ECFException("HTTP GET Error: " + ex.Message + "\nUrl: " + url); } finally { request = null; } } NetResponse Result = new NetResponse() { Cookie = "", Header = null, Content = errorMsg, StatusDescription = statusDes, StatusCode = statusCode }; return(Result); }
/// <summary> /// 获取网络响应. /// </summary> /// <param name="webResponse">Web响应.</param> /// <param name="encoding">The encoding.</param> /// <returns> /// NetResponse /// </returns> private NetResponse GetNetResponse(HttpWebResponse webResponse, Encoding encoding) { //返回参数 NetResponse netResponse = new NetResponse(); try { netResponse.StatusCode = webResponse.StatusCode; netResponse.StatusDescription = webResponse.StatusDescription; netResponse.Header = webResponse.Headers; // 设置Cookies if (webResponse.Cookies != null) { netResponse.CookieCollection = webResponse.Cookies; } if (webResponse.Headers["set-cookie"] != null) { netResponse.Cookie = webResponse.Headers["set-cookie"]; } // 内存数据流处理 MemoryStream _stream = new MemoryStream(); //GZIIP处理 if (webResponse.ContentEncoding != null && webResponse.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase)) { //开始读取流并设置编码方式 //new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240); //.net4.0以下写法 _stream = GetMemoryStream(new GZipStream(webResponse.GetResponseStream(), CompressionMode.Decompress)); } else { //开始读取流并设置编码方式 //response.GetResponseStream().CopyTo(_stream, 10240); //.net4.0以下写法 _stream = GetMemoryStream(webResponse.GetResponseStream()); } //获取Byte byte[] RawResponse = _stream.ToArray(); _stream.Close(); //是否返回Byte类型数据 netResponse.ResultByte = RawResponse; //从这里开始我们要无视编码了 if (encoding == null) { Match meta = Regex.Match(Encoding.Default.GetString(RawResponse), "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase); string charter = (meta.Groups.Count > 1) ? meta.Groups[2].Value.ToLower() : string.Empty; charter = charter.Replace("\"", "").Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk"); if (charter.Length > 2) { encoding = Encoding.GetEncoding(charter.Trim()); } else { if (string.IsNullOrEmpty(webResponse.CharacterSet)) { encoding = Encoding.UTF8; } else { encoding = Encoding.GetEncoding(webResponse.CharacterSet); } } } //得到返回的HTML netResponse.Content = encoding.GetString(RawResponse); } catch (WebException ex) { //这里是在发生异常时返回的错误信息 webResponse = (HttpWebResponse)ex.Response; netResponse.Content = ex.Message; if (webResponse != null) { netResponse.StatusCode = webResponse.StatusCode; netResponse.StatusDescription = webResponse.StatusDescription; } } catch (Exception ex) { netResponse.Content = ex.Message; } return(netResponse); }
/// <summary> /// POST方式发送数据到远程地址. /// </summary> /// <param name="url">远程地址.</param> /// <param name="content">内容.</param> /// <param name="referer">引用地址.</param> /// <param name="contentType">发送内容.</param> /// <param name="errorMsg">错误消息.</param> /// <param name="encode">编码方式.</param> /// <returns></returns> public NetResponse Post(string url, byte[] content, string referer, out string errorMsg, string contentType = DefaultContentType, string encode = "UTF-8") { int failedTimes = _tryTimes; errorMsg = string.Empty; HttpStatusCode statusCode = HttpStatusCode.OK; string statusDes = ""; while (failedTimes-- > 0) { HttpWebRequest request = null; try { if (failedTimes < _tryTimes - 1) // 推迟处理,第一次请求不延迟 { Delay(); } // 获取网络请求对象 request = GetWebRequest(url, referer); request.Method = "POST"; request.ContentType = contentType; if (_timeout > 0) { request.Timeout = _timeout; } Encoding encoding = Encoding.GetEncoding(encode); request.ContentLength = content.Length; using (Stream steam = request.GetRequestStream()) { steam.Write(content, 0, content.Length); } using (WebResponse response = request.GetResponse()) { return(GetNetResponse((HttpWebResponse)response, encoding)); } } catch (WebException ex) { errorMsg = ex.Message; if (ex.Status == WebExceptionStatus.ProtocolError) { HttpWebResponse webResponse = (HttpWebResponse)ex.Response; statusCode = webResponse.StatusCode; statusDes = webResponse.StatusDescription; } else { statusCode = HttpStatusCode.BadGateway; statusDes = ex.Message; } new ECFException("HTTP POST Error: " + ex.Message + "\nUrl: " + url + "\nPost:" + content); } finally { request = null; } } NetResponse Result = new NetResponse() { Cookie = "", Header = null, Content = errorMsg, StatusDescription = statusDes, StatusCode = statusCode }; return(Result); }