博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
获取网页HTML代码[转]
阅读量:5010 次
发布时间:2019-06-12

本文共 3194 字,大约阅读时间需要 10 分钟。

public bool getweb(string strURL,out string buf)
  {
   buf="";
   try
   {
    //Uri url=new Uri(strURL,false);
    HttpWebRequest request;
    request = (HttpWebRequest)WebRequest.Create(strURL);
    request.Method="POST"; //Post请求方式
    request.ContentType="text/html;charset=gb2312"; //内容类型
    string paraUrlCoded = System.Web.HttpUtility.UrlEncode(""); //参数经过URL编码
    byte[] payload;
    payload = System.Text.Encoding.GetEncoding("GB2312").GetBytes(paraUrlCoded); //将URL编码后的字符串转化为字节
    request.ContentLength = payload.Length; //设置请求的ContentLength
    Stream writer = request.GetRequestStream(); //获得请求流
    writer.Write(payload,0,payload.Length); //将请求参数写入流
    writer.Close(); //关闭请求流
    HttpWebResponse response;
    response = (HttpWebResponse)request.GetResponse(); //获得响应流
    Stream s;
    s = response.GetResponseStream();
    StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));
    string HTML = "";
    string sLine ="";
    int i = 0;
    while (sLine!=null)
    {
     i++;
     sLine = objReader.ReadLine();
     if (sLine!=null)
      HTML += sLine;
    }
    //HTML = HTML.Replace("&lt;","<");
    //HTML = HTML.Replace("&gt;",">");
    buf=HTML;
    return true;
   }
   catch (Exception x)
   {   
    buf=x.Message.ToString();
    return false;    
   }
  }
 
带Cookie:
CookieContainer cc = new CookieContainer();
public bool getweb(string strURL,out string buf)
  {
   buf="";
   try
   {
    HttpWebRequest request;
    request = (HttpWebRequest)WebRequest.Create(strURL);
    request.Method="POST"; //Post请求方式
    request.ContentType="text/html;charset=gb2312"; //内容类型
    string paraUrlCoded = System.Web.HttpUtility.UrlEncode(""); //参数经过URL编码
    byte[] payload;
    payload = System.Text.Encoding.GetEncoding("GB2312").GetBytes(paraUrlCoded); //将URL编码后的字符串转化为字节
    request.ContentLength = payload.Length; //设置请求的ContentLength
    Stream writer = request.GetRequestStream(); //获得请求流
    writer.Write(payload,0,payload.Length); //将请求参数写入流
    writer.Close(); //关闭请求流
    HttpWebResponse response;
    response = (HttpWebResponse)request.GetResponse(); //获得响应流
    Stream s;
    s = response.GetResponseStream();
    StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));
    string HTML = "";
    string sLine ="";
    int i = 0;
    while (sLine!=null)
    {
     i++;
     sLine = objReader.ReadLine();
     if (sLine!=null)
      HTML += sLine;
    } 
    buf=HTML;
    return true;
   }
   catch (Exception x)
   {   
    buf=x.Message.ToString();
    return false;    
   }
  }
  public bool getweb(string strURL,out string buf,string postData)
  {
   buf="";
   try
   {   
    
    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] data = encoding.GetBytes(postData);
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = data.Length;
    Stream newStream = request.GetRequestStream();
    newStream.Write(data, 0, data.Length);
    newStream.Close();
                   
    request.CookieContainer = cc;
                   
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    cc.Add(response.Cookies);
    Stream stream = response.GetResponseStream();
    string sHtml = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();
    buf=sHtml;
    return true;
   }
   catch (Exception x)
   {   
    buf=x.Message.ToString();
    return false;    
   }
  }
转自http://www.cnblogs.com/jacktu/archive/2007/07/30/836365.html

转载于:https://www.cnblogs.com/shanqian/archive/2007/08/08/848234.html

你可能感兴趣的文章
CTFMON。exe
查看>>
spark
查看>>
[Angular] Angular Global Keyboard Handling With EventManager
查看>>
[Python] Execute a Python Script
查看>>
[Angular 2] Using Promise to Http
查看>>
[Grunt] grunt.template
查看>>
一、基础篇--1.1Java基础-Object类中常见的方法,为什么wait notify会放在Object里边...
查看>>
UVa 10079 - Pizza Cutting
查看>>
Ubuntu最小化桌面快捷键Super+D不生效解决
查看>>
Cookie&Session会话跟踪技术
查看>>
UNIX环境高级编程 第17章 高级进程间通信
查看>>
ES的Zen发现机制
查看>>
【hibernate】1、Hibernate的一个注解 @Transient
查看>>
flex入门----基础知识
查看>>
HihoCoder 1877 - Approximate Matching
查看>>
【转】C++多继承的细节
查看>>
物联网实验日志-2016-6-8
查看>>
高德地图、腾讯地图、谷歌中国区地图与百度地图坐标系
查看>>
Elastic Search 语法总结
查看>>
py自动化之环境配置
查看>>