c#通过GET/POST获取页面的代码
2010-08-12 16:54
513 查看
/// <summary> /// 提供web处理方法的类 /// </summary> public class WebTreatment { /// <summary> /// 通过GET方式获取页面的方法 /// </summary> /// <param name="urlString">请求的URL</param> /// <param name="encoding">页面编码</param> /// <returns></returns> public static string GetHtmlFromGet(string urlString, Encoding encoding) { //定义局部变量 HttpWebRequest httpWebRequest = null; HttpWebResponse httpWebRespones = null; Stream stream = null; string htmlString = string.Empty; //请求页面 try { httpWebRequest = WebRequest.Create(urlString) as HttpWebRequest; } //处理异常 catch (Exception ex) { throw new Exception("建立页面请求时发生错误!", ex); } httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; Maxthon 2.0)"; //获取服务器的返回信息 try { httpWebRespones = (HttpWebResponse)httpWebRequest.GetResponse(); stream = httpWebRespones.GetResponseStream(); } //处理异常 catch (Exception ex) { throw new Exception("接受服务器返回页面时发生错误!", ex); } StreamReader streamReader = new StreamReader(stream,encoding); //读取返回页面 try { htmlString = streamReader.ReadToEnd(); } //处理异常 catch (Exception ex) { throw new Exception("读取页面数据时发生错误!", ex); } //释放资源返回结果 streamReader.Close(); stream.Close(); return htmlString; } /// <summary> /// 提供通过POST方法获取页面的方法 /// </summary> /// <param name="urlString">请求的URL</param> /// <param name="encoding">页面使用的编码</param> /// <param name="postDataString">POST数据</param> /// <returns>获取的页面</returns> public static string GetHtmlFromPost(string urlString, Encoding encoding, string postDataString) { //定义局部变量 CookieContainer cookieContainer = new CookieContainer(); HttpWebRequest httpWebRequest = null; HttpWebResponse httpWebResponse = null; Stream inputStream = null; Stream outputStream = null; StreamReader streamReader = null; string htmlString = string.Empty; //转换POST数据 byte[] postDataByte = encoding.GetBytes(postDataString); //建立页面请求 try { httpWebRequest = WebRequest.Create(urlString) as HttpWebRequest; } //处理异常 catch (Exception ex) { throw new Exception("建立页面请求时发生错误!", ex); } //指定请求处理方式 httpWebRequest.Method = "POST"; httpWebRequest.KeepAlive = false; httpWebRequest.ContentType = "application/x-www-form-urlencoded"; httpWebRequest.CookieContainer = cookieContainer; httpWebRequest.ContentLength = postDataByte.Length; //向服务器传送数据 try { inputStream = httpWebRequest.GetRequestStream(); inputStream.Write(postDataByte, 0, postDataByte.Length); } //处理异常 catch (Exception ex) { throw new Exception("发送POST数据时发生错误!", ex); } finally { inputStream.Close(); } //接受服务器返回信息 try { httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse; outputStream = httpWebResponse.GetResponseStream(); streamReader = new StreamReader(outputStream, encoding); htmlString = streamReader.ReadToEnd(); } //处理异常 catch (Exception ex) { throw new Exception("接受服务器返回页面时发生错误!", ex); } finally { streamReader.Close(); } foreach (Cookie cookie in httpWebResponse.Cookies) { cookieContainer.Add(cookie); } return htmlString; } }
相关文章推荐
- c#通过GET/POST获取页面的代码
- c#通过GET/POST获取页面的代码
- C#后台post数据到指定页面并获取返回值
- SSH笔记 在JSP页面中通过Java获取系统时间并赋值给当前页面标签完整代码
- jQuery发起get/post请求_或_获取html页面数据
- 10行代码在C#中获取页面元素布局信息
- C#获取当前页面的URL示例代码
- C#和C实现通过CRC-16 (Modbus)获取CRC值并校验数据(代码)
- C# 通过GET、POST请求获取结果
- C#代码发送get/post请求的方法
- jsp中插入java代码,并在当前页面获取值(也可在另一js种通过input标签id获取值)
- js 通过post方式打开页面 利用html5本地存储获取数据
- python网络爬虫学习(一)通过GET和POST方式获取页面内容
- 史上最全的CSS hack方式一览 jQuery 图片轮播的代码分离 JQuery中的动画 C#中Trim()、TrimStart()、TrimEnd()的用法 marquee 标签的使用详情 js鼠标事件 js添加遮罩层 页面上通过地址栏传值时出现乱码的两种解决方法 ref和out的区别在c#中 总结
- 爬虫技术(六)-- 使用HtmlAgilityPack获取页面链接(附c#代码及插件下载)
- 爬虫技术 -- 进阶学习(九)使用HtmlAgilityPack获取页面链接(附c#代码及插件下载)
- C#通过POP3获取邮件的代码(正文和附件)
- c#模拟表单POST数据,并获取跳转之后的页面
- JSP 部属Tomcat jsp嵌套Java代码 通过JDBC获取数据库表通过Java在JSP页面输出