HttpWebRequest请求
1 /// <summary> 2 /// 模拟网页操作,提交、获取订单页面数据 3 /// </summary> 4 public class HttpWebRequestExtension 5 { 6 private static string contentType = "application/x-www-form-urlencoded"; 7 private static string accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*"; 8 private static string userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Zune 4.7; BOIE9;ZHCN)"; 9 private static string referer = "https://kyfw.12306.cn/"; 10 /// <summary> 11 /// 提交订单数据 12 /// </summary> 13 /// <param name="url"></param> 14 /// <param name="cookie"></param> 15 /// <param name="param"></param> 16 /// <returns></returns> 17 public static string PostWebContent(string url, CookieContainer cookie, string param) 18 { 19 byte[] bs = Encoding.ASCII.GetBytes(param); 20 var httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); 21 httpWebRequest.CookieContainer = cookie; 22 httpWebRequest.ContentType = contentType; 23 httpWebRequest.Accept = accept; 24 httpWebRequest.UserAgent = userAgent; 25 httpWebRequest.Method = "POST"; 26 httpWebRequest.ContentLength = bs.Length; 27 using (Stream reqStream = httpWebRequest.GetRequestStream()) 28 { 29 reqStream.Write(bs, 0, bs.Length); 30 } 31 var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 32 Stream responseStream = httpWebResponse.GetResponseStream(); 33 StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8); 34 string html = streamReader.ReadToEnd(); 35 36 streamReader.Close(); 37 responseStream.Close(); 38 39 httpWebRequest.Abort(); 40 httpWebResponse.Close(); 41 42 return html; 43 } 44 45 /// <summary> 46 /// 获取页面数据 47 /// </summary> 48 /// <param name="url"></param> 49 /// <param name="cookie"></param> 50 /// <returns></returns> 51 public static string GetWebContent(string url, CookieContainer cookie) 52 { 53 var httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); 54 httpWebRequest.CookieContainer = cookie; 55 httpWebRequest.ContentType = contentType; 56 httpWebRequest.Referer = referer; 57 httpWebRequest.Accept = accept; 58 httpWebRequest.UserAgent = userAgent; 59 httpWebRequest.Method = "GET"; 60 httpWebRequest.ServicePoint.ConnectionLimit = int.MaxValue; 61 62 var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 63 Stream responseStream = httpWebResponse.GetResponseStream(); 64 StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8); 65 string html = streamReader.ReadToEnd(); 66 67 streamReader.Close(); 68 responseStream.Close(); 69 70 httpWebRequest.Abort(); 71 httpWebResponse.Close(); 72 73 return html; 74 } 75 76 /// <summary> 77 /// 获取网页验证码图片 78 /// </summary> 79 /// <param name="url"></param> 80 /// <param name="cookie"></param> 81 /// <returns></returns> 82 public static object GetWebImage(string url, CookieContainer cookie) 83 { 84 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 85 request.Referer = referer; 86 request.UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"; 87 request.Accept = "image/webp,*/*;q=0.8"; 88 request.CookieContainer = cookie; 89 request.ContentType = contentType; 90 request.KeepAlive = true; 91 request.UseDefaultCredentials = true; 92 // request.Proxy = null; 93 return request.GetResponse().GetResponseStream(); 94 } 95 }
作者:愤怒的TryCatch
请尊重别人的劳动成果,让分享成为一种美德,欢迎转载。另外,文章在表述和代码方面如有不妥之处,欢迎批评指正。留下你的脚印,欢迎评论!

浙公网安备 33010602011771号