/// <summary> /// 封装的Http参数化请求 改变Cookie /// </summary> /// <param name="Httpinfo"></param> /// <returns>返回的源代码</returns> public static string HttpWork(ref HttpInfo Httpinfo) { Encoding encoding = Encoding.UTF8; String retString = null; HttpWebResponse response = null; MemoryStream _stream = new MemoryStream(); var request = Httpinfo.CreatRequest(); if (request == null && Httpinfo.CheckUrl) { retString = "Url校验未通过"; } try { if (request != null) { if (Httpinfo.UseTaskTimeOut && !String.IsNullOrEmpty(Httpinfo.Ip)) { var a = request.BeginGetResponse(null, null); var waitResult = a.AsyncWaitHandle.WaitOne(Httpinfo.Timeout + Httpinfo.ReadWriteTimeout); if (waitResult && a.IsCompleted) { response = request.EndGetResponse(a) as HttpWebResponse; } else { response = null; } } else { response = request.GetResponse() as HttpWebResponse; } if (response == null) { retString = String.Empty; } else { //GZIIP处理 if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase)) { //开始读取流并设置编码方式 //new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240); //.net4.0以下写法 _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress)); } else { //开始读取流并设置编码方式 //response.GetResponseStream().CopyTo(_stream, 10240); //.net4.0以下写法 _stream = GetMemoryStream(response.GetResponseStream()); } //获取Byte byte[] RawResponse = _stream.ToArray(); _stream.Close(); if (Httpinfo.Encoding != null) { encoding = Httpinfo.Encoding; } else { //从这里开始我们要无视编码了 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 (string.IsNullOrEmpty(response.CharacterSet)) { try { encoding = Encoding.GetEncoding(charter.Trim()); } catch { encoding = Encoding.UTF8; } } else { if (string.IsNullOrEmpty(response.CharacterSet)) { try { encoding = Encoding.GetEncoding(charter.Trim()); } catch { encoding = Encoding.UTF8; } } else if (response.CharacterSet.ToLower().Contains("iso-8859-1")) { encoding = Encoding.UTF8; } else { encoding = Encoding.GetEncoding(response.CharacterSet); } } } //得到返回的HTML retString = encoding.GetString(RawResponse); if (String.IsNullOrEmpty(Httpinfo.Cookie.BaseCookieStr)) { Httpinfo.Cookie.AddCookie(response.Headers[HttpResponseHeader.SetCookie]); } else { Httpinfo.Cookie.BaseCookieStr = response.Headers[HttpResponseHeader.SetCookie]; } } } } catch (System.Net.WebException e) { if (Httpinfo.IgnoreWebException) { response = (HttpWebResponse)e.Response; Httpinfo.Cookie.AddCookie(response.Headers[HttpResponseHeader.SetCookie]); using (StreamReader sr = new StreamReader(response.GetResponseStream(), encoding)) { retString = sr.ReadToEnd(); } } else { return(e.Message); } } finally { if (request != null) { request.Abort(); request = null; } if (response != null) { response.Close(); response = null; } if (_stream != null) { _stream.Close(); _stream.Dispose(); _stream = null; } } return(retString); }
/// <summary> /// 构造HttpInfo方式下载文件 下载到指定目录 /// </summary> /// <param name="DownLoadUrl">下载地址</param> /// <param name="SavePath">文件目录</param> /// <param name="FileName">保存文件名</param> /// <returns></returns> public static bool DownLoadFile(HttpInfo info, string FloderPath, String FileName = "") { Regex filename_reg = new Regex("(“)|(\\r)|(\\n)|( )|(\\t)|\\.|\"|/"); #region 配置文件路径 //去除非法字符和空格 FileName = filename_reg.Replace(FileName, String.Empty); //文件后缀名 从文件地址后缀中获取 String filetype = Path.GetExtension(info.RequestUrl); //如果文件名中没有后缀名 if (!String.IsNullOrEmpty(Path.GetExtension(FileName))) { //拼接后缀名 FileName = FileName + filetype; } //获得下载地址中匹配到文件名 String filename = Path.GetFileName(info.RequestUrl); //形成最终保存路径 如果参数中的文件名是空的 则使用自动匹配到的文件路径 反之使用输入的文件名 String FinalPath = Path.Combine(FloderPath, filename); #endregion try { var Myrq = info.CreatRequest(); if (Myrq != null) { using (System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse()) { #region 载 long totalBytes = myrp.ContentLength; object[] args = { (int)totalBytes, 0 }; System.IO.Stream st = myrp.GetResponseStream(); System.IO.Stream so = new System.IO.FileStream(FinalPath, System.IO.FileMode.Create); try { long totalDownloadedByte = 0; byte[] by = new byte[1024]; int osize = st.Read(by, 0, (int)by.Length); while (osize > 0) { int taskosiz = osize; totalDownloadedByte = osize + totalDownloadedByte; so.Write(by, 0, osize); osize = st.Read(by, 0, (int)by.Length); } } catch { } finally { so.Dispose(); so.Close(); st.Dispose(); st.Close(); } #endregion } } } catch (System.Exception) { return(false); } return(true); }
/// <summary> /// 快速请求 基于HttpInfo /// </summary> /// <param name="Url">请求地址</param> /// <returns></returns> public static String FastGetMethod_HttpInfo(String Url) { HttpInfo info = new HttpInfo(Url); return(HttpMethod.HttpWork(info)); }
/// <summary> /// Response异步回调函数 /// </summary> /// <param name="ar"></param> private static void EndResopnseCallBack(IAsyncResult ar) { //解析获得响应内容 object[] ojarr = (object[])ar.AsyncState; var request = ojarr[0] as HttpWebRequest; HttpInfo Httpinfo = ojarr[3] as HttpInfo; MemoryStream _stream = null; Encoding encoding = Encoding.UTF8; String html = String.Empty; HttpWebResponse response = null; try { using (response = request.EndGetResponse(ar) as HttpWebResponse) { if (ExecDateDiff(DateTime.Now, Convert.ToDateTime(ojarr[1])) <= Httpinfo.Timeout) { //GZIIP处理 if (response.ContentEncoding != null && response.ContentEncoding.ToLower().Contains("gzip")) { //开始读取流并设置编码方式 //new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240); //.net4.0以下写法 _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress)); } else { //开始读取流并设置编码方式 //response.GetResponseStream().CopyTo(_stream, 10240); //.net4.0以下写法 _stream = GetMemoryStream(response.GetResponseStream()); } //获取Byte byte[] RawResponse = _stream.ToArray(); _stream.Close(); if (Httpinfo.Encoding != null) { encoding = Httpinfo.Encoding; } else { //从这里开始我们要无视编码了 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(response.CharacterSet)) { try { encoding = Encoding.GetEncoding(charter.Trim()); } catch { encoding = Encoding.UTF8; } } else if (response.CharacterSet.ToLower().Contains("iso-8859-1")) { encoding = Encoding.UTF8; } else { encoding = Encoding.GetEncoding(response.CharacterSet); } } } //得到返回的HTML html = encoding.GetString(RawResponse); } } } catch (System.Net.WebException e) { if (Httpinfo.IgnoreWebException) { response = (HttpWebResponse)e.Response; using (StreamReader sr = new StreamReader(response.GetResponseStream(), encoding)) { html = sr.ReadToEnd(); } } else { html = e.Message; } } finally { var list = ojarr.ToList().GetRange(2, ojarr.Length - 2); if (request != null) { request.Abort(); request = null; } EndResopnseMethod(html, list.ToArray()); } }
/// <summary> /// 封装的Http参数化请求 /// </summary> /// <param name="Httpinfo">携带请求参数的HttpInfo参数</param> /// <returns>返回的源代码</returns> public static string HttpWork(HttpInfo Httpinfo) { //默认的编码为UTF-8 Encoding encoding = Encoding.UTF8; //待返回的网页源代码 String retString = String.Empty; HttpWebRequest request = null; HttpWebResponse response = null; MemoryStream _stream = new MemoryStream(); try { request = Httpinfo.CreatRequest(); if (request != null) { if (Httpinfo.UseTaskTimeOut && !String.IsNullOrEmpty(Httpinfo.Ip)) { var a = request.BeginGetResponse(null, null); var waitResult = a.AsyncWaitHandle.WaitOne(Httpinfo.Timeout + Httpinfo.WriteTimeout); if (waitResult && a.IsCompleted) { response = request.EndGetResponse(a) as HttpWebResponse; } else { response = null; } } else { response = request.GetResponse() as HttpWebResponse; } if (response == null) { retString = String.Empty; } else { //GZIIP处理 if (response.ContentEncoding != null && response.ContentEncoding.ToLower().Contains("gzip")) { //开始读取流并设置编码方式 //new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240); //.net4.0以下写法 _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress)); } else { //开始读取流并设置编码方式 //response.GetResponseStream().CopyTo(_stream, 10240); //.net4.0以下写法 _stream = GetMemoryStream(response.GetResponseStream()); } //获取Byte byte[] RawResponse = _stream.ToArray(); _stream.Close(); if (Httpinfo.Encoding != null) { encoding = Httpinfo.Encoding; } else { //从这里开始我们要无视编码了 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(response.CharacterSet)) { try { encoding = Encoding.GetEncoding(charter.Trim()); } catch { encoding = Encoding.UTF8; } } else if (response.CharacterSet.ToLower().Contains("iso-8859-1")) { encoding = Encoding.UTF8; } else { encoding = Encoding.GetEncoding(response.CharacterSet); } } } //得到返回的HTML retString = encoding.GetString(RawResponse); } } } catch (System.Net.WebException e) { //Console.WriteLine(e.Message); return(e.Message); } finally { if (request != null) { request.Abort(); request = null; } if (response != null) { response.Close(); response = null; } if (_stream != null) { _stream.Close(); _stream.Dispose(); _stream = null; } } return(retString); }
/// <summary> /// 封装的Http参数化请求 /// </summary> /// <param name="Httpinfo">携带请求参数的HttpInfo参数</param> /// <returns></returns> public static string HttpWork(HttpInfo Httpinfo) { //默认的编码为UTF-8 Encoding encoding = Encoding.UTF8; //待返回的网页源代码 String retString = String.Empty; try { var request = Httpinfo.CreatRequest(); if (request != null) { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { MemoryStream _stream = new MemoryStream(); //GZIIP处理 if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase)) { //开始读取流并设置编码方式 //new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240); //.net4.0以下写法 _stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress)); } else { //开始读取流并设置编码方式 //response.GetResponseStream().CopyTo(_stream, 10240); //.net4.0以下写法 _stream = GetMemoryStream(response.GetResponseStream()); } //获取Byte byte[] RawResponse = _stream.ToArray(); _stream.Close(); if (Httpinfo.Encoding != null) { encoding = Httpinfo.Encoding; } else { //从这里开始我们要无视编码了 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(response.CharacterSet)) { try { encoding = Encoding.GetEncoding(charter.Trim()); } catch { encoding = Encoding.UTF8; } } else if (response.CharacterSet.ToLower().Contains("iso-8859-1")) { encoding = Encoding.UTF8; } else { encoding = Encoding.GetEncoding(response.CharacterSet); } } } //得到返回的HTML retString = encoding.GetString(RawResponse); } } } catch (System.Net.WebException e) { //Console.WriteLine(e.Message); return(e.Message); } return(retString); }