zoukankan      html  css  js  c++  java
  • c#发送Http请求

    .net 有两种方式取得页面源码

     1、HttpWebRequest和HttpWebResponse 

              HttpWebRequest request = (HttpWebRequest)WebRequest.Create(redirectUrl);
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    using (StreamReader readStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                    {
                        while (!readStream.EndOfStream)
                        {
                            ViewBag.Stream += readStream.ReadLine();
                        }
                    }

    2、WebClient 

             WebClient client = new WebClient();
             client.Encoding = Encoding.UTF8;
    string htmlStr = client.DownloadString(redirectUrl); ViewBag.Stream = htmlStr;

    区别:

    第一种,复杂,灵活性高。可以伪装一下进行采集或者发送登录信息。

    第二种,简单,好用。遇到反采集、需要登录的话就麻烦了。

    WebClient是WebRequest的再包装,简化使用,代码量少,但灵活不足.

  • 相关阅读:
    1月19日
    1月18日
    1月17日
    读后感(1)
    UIAlertView
    plist
    jQuery validation
    HTML <a href >标签的target属性
    HTML DOM
    .与..的区别
  • 原文地址:https://www.cnblogs.com/xbblogs/p/5920784.html
Copyright © 2011-2022 走看看