zoukankan      html  css  js  c++  java
  • C# 获取网页HTML代码

    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;    
       }
      }
  • 相关阅读:
    java 数据类型:集合接口Collection之List~ArrayList:remove移除;replaceAll改变原有值;sort排序;迭代器listIterator();
    java 数据类型:集合接口Collection之常用ArrayList;lambda表达式遍历;iterator遍历;forEachRemaining遍历;增强for遍历;removeIf批量操作集合元素(Predicate);
    java 常用类库:格式化NumberFormat;SimpleDataFormat类(转换Data()对象);DateTimeFormatter 转换LocalDateTime时间对象
    java 常用类库:时间类LocalDate;LocalTime;LocalDateTime;Calendar 类;Date ;
    java 常用类库:BigInteger大整数;BigDecimal大小数(解决double精度损失);
    java 常用类库:Math:常用min、max;floor;ceil;random;
    1345. Jump Game IV
    1298. Maximum Candies You Can Get from Boxes
    1293. Shortest Path in a Grid with Obstacles Elimination
    1263. Minimum Moves to Move a Box to Their Target Location
  • 原文地址:https://www.cnblogs.com/jacktu/p/836365.html
Copyright © 2011-2022 走看看