原文:.net 获取https页面的信息 在iis7.5服务器上不管用
让我纠结了一天多的问题,给大家看下,有相同情况的可以不用浪费时间了,本人当时找了好半天都没找到什么有用的信息,项目在本地没有问题,但部署在服务器后,获取不到https页面的信息,加入下面的代码就可以了,因为iis7.5的安全协议比较高的原因。
我的获取页面需要cookie,不需要的可以去掉;
GET的方法:
1 /// <summary> 2 /// 获取URL访问的HTML内容 获取https 页面的 3 /// </summary> 4 /// <param name="Url">URL地址</param> 5 /// <returns>HTML内容</returns> 6 public static string GetWebContent(string Url, CookieContainer cookieContainer) 7 { 8 string strResult = ""; 9 try 10 { 11 ServicePointManager.Expect100Continue = true; 12 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; 13 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); 14 request.CookieContainer = cookieContainer; 15 request.Timeout = 30000; 16 request.Headers.Set("Pragma", "no-cache"); 17 18 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 19 Stream streamReceive = response.GetResponseStream(); 20 21 Encoding encoding = Encoding.GetEncoding("utf-8"); 22 StreamReader streamReader = new StreamReader(streamReceive, encoding); 23 strResult = streamReader.ReadToEnd(); 24 } 25 catch 26 { 27 28 } 29 return strResult; 30 }
View Code
POST的方法:
1 /// <summary> 2 /// post提交数据到https 3 /// </summary> 4 /// <param name="posturl"></param> 5 /// <param name="postdata"></param> 6 /// <param name="header"></param> 7 /// <param name="cookieContainer"></param> 8 /// <returns></returns> 9 public static string SetPostHtml(string posturl, string postdata, HttpHeader header, CookieContainer cookieContainer) 10 { 11 string restr = ""; 12 ServicePointManager.Expect100Continue = true; 13 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; 14 HttpWebRequest request = null; 15 HttpWebResponse response = null; 16 request = (HttpWebRequest)WebRequest.Create(posturl); 17 request.CookieContainer = cookieContainer; 18 request.Method = header.method; 19 request.Referer = header.Referer; 20 request.ContentType = header.contentType; 21 byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata); 22 request.ContentLength = postdatabyte.Length; 23 request.AllowAutoRedirect = false; 24 request.KeepAlive = true; 25 //提交请求 26 Stream stream; 27 stream = request.GetRequestStream(); 28 stream.Write(postdatabyte, 0, postdatabyte.Length); 29 stream.Close(); 30 //接收响应 31 response = (HttpWebResponse)request.GetResponse(); 32 using (StreamReader reader = new StreamReader(response.GetResponseStream())) 33 { 34 restr = reader.ReadToEnd().ToString(); 35 } 36 return restr; 37 }
View Code
希望对大家有帮助;
时间: 2024-10-01 19:40:42