zoukankan      html  css  js  c++  java
  • GetJson

      public static string GetJson(string url)
        {
            string res = "";
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
            req.Method = "GET";
            using (WebResponse wr = req.GetResponse())
            {
                HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                res = reader.ReadToEnd();
            }
            return res;
        }

    HttpWebRequest.Create(url):返回WEBRequest类型。

    HttpWebRequest类与HttpRequest类的区别。

      HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所有信息。而HttpWebRequest用于客户端,拼接请求的HTTP报文并发送等。

      HttpWebRequest这个类非常强大,强大的地方在于它封装了几乎HTTP请求报文里需要用到的东西,以致于能够能够发送任意的HTTP请求并获得服务器响应(Response)信息。采集信息常用到这个类。在学习这个类之前,首先有必要了解下HTTP方面的知识。

    三步走:

    1.HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);

    2.  HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();

    3.StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);

  • 相关阅读:
    Badboy录制脚本时,提示脚本错误解决方法
    Python数据类型_列表
    Wannafly Primary School Students训练记录
    Deadline队伍训练实录
    2018 German Collegiate Programming Contest (GCPC 18)
    2016ccpc杭州
    2016 ICPC 大连
    2017CCPC Final (哈尔滨)
    2017CCPC 长春
    2017 CCPC 杭州
  • 原文地址:https://www.cnblogs.com/zhubenxi/p/5329036.html
Copyright © 2011-2022 走看看