public object Img(httpconfig info) { var tm = 0; var t = ""; System.Net.ServicePointManager.DefaultConnectionLimit = 50; gt: HttpWebRequest request = null; HttpWebResponse response = null; try { request = WebRequest.Create(info.url) as HttpWebRequest; if (info.url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult; } request.ServicePoint.Expect100Continue = false; request.ServicePoint.UseNagleAlgorithm = false; request.ServicePoint.ConnectionLimit = 65500; request.AllowWriteStreamBuffering = false; request.Proxy = null; request.Proxy = null; if (info.Set_Cookie != null) { if (request != null) { request.Headers["Cookie"] = info.Set_Cookie.Replace("HttpOnly", ""); } } if (info.CookieContainer == null) { info.CookieContainer = new CookieContainer(); } if (info.cookieColletion != null || info.cookieoff == 1) { info.cookieColletion = info.cookieColletion; } var tms = new Stopwatch(); if (request != null) { request.AllowAutoRedirect = info.AllowAutoRedirect; if (info.HeadKey != null) { foreach (DictionaryEntry stw in info.HeadKey) { SetHeaderValue(request.Headers, stw.Key.ToString(), stw.Value.ToString()); // request.Headers.Set(stw.Key.ToString(), stw.Value.ToString()); } } if (!t.Contains("参数")) { SetHeaderValue(request.Headers, "Accept-Encoding", "gzip, deflate, sdch"); } // request.Method=info. if (info.Referer != null) { request.Referer = info.Referer; } request.Accept = info.Accept; request.UserAgent = info.UserAgent; request.ContentType = info.ContentType; tms.Start(); response = request.GetResponse() as HttpWebResponse; } if (info.cookieoff == 0) { if (response != null) { info.OutCookies = response.Headers["Set-Cookie"]; } } if (response != null) { var bm = response.GetResponseStream(); info.OutImag = new Bitmap(bm); } tms.Stop(); //if (HttpContext.Current != null) //{ // ("\r\n----------------------------------\r\nUrl:" + info.url + "\r\n平台名称:请求的时间\r\n" + // "耗时:" + tms.Elapsed.ToString()).FileWrite(HttpContext.Current.Request.MapPath("~/Log/YzmLog/") + // "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt"); // info.Imgstream = new MemoryStream(); // info.OutImag.Save(info.Imgstream, ImageFormat.Jpeg); // request?.Abort(); // response?.Close(); //} return(null); } catch (Exception ex) { request?.Abort(); response?.Close(); t = ex.Message; //("\r\n-----------------------------------------------\r\n" + // "Url:" + info.url + "\r\n错误信息:" + ex.ToString() + "\r\n").FileWrite( // HttpContext.Current.Request.MapPath("~/Log/YzmLog/") + "\\Err" + DateTime.Now.ToString("yyyy-MM-dd") + // ".txt"); if (ex.ToString().Contains("参数")) { tm++; if (tm > 5) { return(null); } goto gt; } return(null); } }
/// <summary> /// /// </summary> /// <returns></returns> public string Gethtml(httpconfig info) { ServicePointManager.Expect100Continue = false; HttpWebResponse response = null; var encoding = Encoding.GetEncoding(info.encodeType); // System.Net.ServicePointManager.DefaultConnectionLimit = 50; try { // 设置参数 var request = WebRequest.Create(info.url) as HttpWebRequest; if (request == null) { return("异常错误"); } request.Proxy = null; if (info.url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult; } if (info.Set_Cookie != null) { request.Headers["Cookie"] = info.Set_Cookie; } if (info.IsPort) { request.Proxy = new WebProxy() { Address = new Uri("http://" + info.Ip) }; } else { request.Proxy = null; } //if (info.CookieContainer != null) //{ // request.CookieContainer = info.CookieContainer; //} //if (info.cookieColletion != null) //{ // info.cookieColletion = info.cookieColletion; //} if (info.HeadKey != null) { foreach (DictionaryEntry stw in info.HeadKey) { SetHeaderValue(request.Headers, stw.Key.ToString(), stw.Value.ToString()); // request.Headers.Set(stw.Key.ToString(), stw.Value.ToString()); } } request.Timeout = info.OutTime != 0 ? info.OutTime : 80000; request.AllowAutoRedirect = info.AllowAutoRedirect; request.Method = info.Method; if (info.Origin != null) { request.Headers["Origin"] = info.Origin; } request.Referer = info.Referer; if (info.HeadKey != null) { if (info.HeadKey.Contains("Accept-Language")) { } else { request.Headers.Set("Accept-Language", "zh-CN,zh;q=0.8"); //很重要 } if (info.HeadKey.Contains("Accept-Encoding")) { request.Headers.Add(HttpRequestHeader.AcceptEncoding, info.HeadKey["Accept-Encoding"].ToString()); } } request.Accept = info.Accept; request.UserAgent = info.UserAgent; request.ContentType = info.ContentType; if (info.Method == "POST") { var data = encoding.GetBytes(info.postdata); request.ContentLength = data.Length; var outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); } //发送请求并获取相应回应数据 response = request.GetResponse() as HttpWebResponse; if (info.cookieoff == 0 & response.Headers["set-cookie"] != null & response.Headers["set-cookie"] != "") { info.OutCookies = response.Headers["set-cookie"]; } // request.GetResponse(); //直到request.GetResponse()程序才开始向目标网页发送Post请求 var instream = response.GetResponseStream(); var sr = new StreamReader(instream, encoding); if (!IsNullOrEmpty(response.Headers["Location"])) { info.Location = response.Headers["Location"]; } if (response.ContentEncoding.ToLower().Contains("gzip")) { using (System.IO.Compression.GZipStream stream = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress)) { using (StreamReader reader = new StreamReader(stream, encoding)) { return(reader.ReadToEnd()); } } } var hm = sr.ReadToEnd(); return(hm); } catch (Exception ex) { km++; if (km > 2) { return(ex + "|ErrContent"); } return(Gethtml(info)); } finally { // ReSharper disable once ConstantConditionalAccessQualifier response?.Close(); } }
/// <summary> /// 国际 /// </summary> /// <returns></returns> public HttpWebResponse GetdataHtml(httpconfig info) { System.Net.ServicePointManager.Expect100Continue = false; Encoding encoding = Encoding.GetEncoding(info.encodeType); try { // 设置参数 var request = WebRequest.Create(info.url) as HttpWebRequest; if (request == null) { return(null); } if (info.OutCookies != null & info.CookieContainer == null) { request.Headers["Cookie"] = info.OutCookies; } if (info.CookieContainer != null) { request.CookieContainer = info.CookieContainer; } if (info.cookieColletion != null) { info.cookieColletion = info.cookieColletion; } request.AllowAutoRedirect = info.AllowAutoRedirect; request.Method = info.Method; if (info.HeadKey != null) { foreach (DictionaryEntry stw in info.HeadKey) { SetHeaderValue(request.Headers, stw.Key.ToString(), stw.Value.ToString()); // request.Headers.Set(stw.Key.ToString(), stw.Value.ToString()); } } request.Headers.Set("Accept-Language", "zh-CN,zh;q=0.8");//很重要 request.Accept = info.Accept; request.Referer = info.Referer; request.UserAgent = info.UserAgent; request.ContentType = info.ContentType; request.Headers.Set("Accept-Encoding", "gzip,deflate,sdch"); if (info.Method == "POST") { byte[] data = encoding.GetBytes(info.postdata); request.ContentLength = data.Length; var outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); } //发送请求并获取相应回应数据 // response = (HttpWebResponse)request.GetResponse(); var response = request.GetResponse() as HttpWebResponse; if (info.cookieoff != 0) { return(response); } if (response != null) { info.OutCookies = response.Headers["Set-Cookie"]; } //string hj = response.Headers["set-cookie"]; ////直到request.GetResponse()程序才开始向目标网页发送Post请求 //instream = response.GetResponseStream(); // sr = new StreamReader(instream, encoding); return(response); } catch (Exception ex) { info.timcs++; return(info.timcs > 3 ? null : GetdataHtml(info)); } }
/// <summary> /// 返回数据流,主要用于下载xls文件 /// </summary> /// <returns></returns> public byte[] GetDataHtml(httpconfig info) { System.Net.ServicePointManager.Expect100Continue = false; HttpWebResponse response = null; Encoding encoding = Encoding.GetEncoding(info.encodeType); try { // 设置参数 var request = WebRequest.Create(info.url) as HttpWebRequest; if (request == null) { return(null); } request.Proxy = null; if (info.Set_Cookie != null) { request.Headers["Cookie"] = info.Set_Cookie; } //if (info.CookieContainer != null) //{ // request.CookieContainer = info.CookieContainer; //} //if (info.cookieColletion != null) //{ // info.cookieColletion = info.cookieColletion; //} if (info.OutTime > 0) { request.Timeout = info.OutTime; } if (info.HeadKey != null) { foreach (DictionaryEntry stw in info.HeadKey) { SetHeaderValue(request.Headers, stw.Key.ToString(), stw.Value.ToString()); // request.Headers.Set(stw.Key.ToString(), stw.Value.ToString()); } } request.AllowAutoRedirect = info.AllowAutoRedirect; request.Method = info.Method; if (info.Origin != null) { request.Headers["Origin"] = info.Origin; } request.Referer = info.Referer; request.Headers.Set("Accept-Language", "zh-CN,zh;q=0.8"); //很重要 request.Accept = info.Accept; request.UserAgent = info.UserAgent; request.ContentType = info.ContentType; if (info.Method == "POST") { byte[] data = encoding.GetBytes(info.postdata); request.ContentLength = data.Length; var outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); } //发送请求并获取相应回应数据 response = request.GetResponse() as HttpWebResponse; if (response != null && info.cookieoff == 0 & response?.Headers["set-cookie"] != null & response.Headers["set-cookie"] != "") { info.OutCookies = response.Headers["set-cookie"]; } info.Filename = response?.Headers["Content-Disposition"]; if (!IsNullOrEmpty(info.Filename)) { try { info.Filename = Path.GetExtension(info.Filename.Replace("\"", "")); } catch (Exception) { info.Filename = ""; } } var strd = new MemoryStream(); const int bufferSize = 512; var bytes = new byte[bufferSize]; using (var stream = response?.GetResponseStream()) { try { if (stream != null) { var length = stream.Read(bytes, 0, bufferSize); while (length > 0) { strd.Write(bytes, 0, length); length = stream.Read(bytes, 0, bufferSize); } } } catch (Exception ex) { return(null); } } return(strd.ToArray()); //string hj = response.Headers["set-cookie"]; ////直到request.GetResponse()程序才开始向目标网页发送Post请求 //instream = response.GetResponseStream(); //sr = new StreamReader(instream, encoding); //string hm = sr.ReadToEnd(); //return hm; } catch (Exception ex) { km++; if (km > 3) { return(null); } // response.Close(); return(GetDataHtml(info)); } finally { // ReSharper disable once ConstantConditionalAccessQualifier response?.Close(); } }