zoukankan      html  css  js  c++  java
  • 获取一个请求的URL内容

    using System.Net;

      1、


            // 创建一个请求的URL。  
            WebRequest request = WebRequest.Create("http://www.baidu.com/");
            // 如果所需的服务器,设置凭证。
            request.Credentials = CredentialCache.DefaultCredentials;
            // 得到响应。
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            // 显示状态。
            Console.WriteLine(response.StatusDescription);
            // 获取流包含服务器返回的内容。
            Stream dataStream = response.GetResponseStream();
            // 打开流使用StreamReader方便访问。
            StreamReader reader = new StreamReader(dataStream);
            // 读取内容。
            string responseFromServer = reader.ReadToEnd();
            // 显示内容。TextBox2显示
            TextBox2.Text = responseFromServer;
            // 清理流和响应。
            reader.Close();
            dataStream.Close();
            response.Close();

    2、

       public string GetHttpData(string Url)
        {
            string sRslt = null;
            WebResponse oWebRps = null;
            WebRequest oWebRqst = WebRequest.Create(Url);
            oWebRqst.Timeout = 50000;
            try
            {
                oWebRps = oWebRqst.GetResponse();
            }
            finally
            {
                if (oWebRps != null)
                {
                    StreamReader oStreamRd = new StreamReader(oWebRps.GetResponseStream(), Encoding.GetEncoding("utf-8"));
                    sRslt = oStreamRd.ReadToEnd();
                    oStreamRd.Close();
                    oWebRps.Close();
                }
            }
            return sRslt;
        }

  • 相关阅读:
    Git 使用记录
    AngularJS $http返回的数据类型
    angularJS 使用$http 出现 $http.get(...).success is not a function
    JS数组及其方法(slice,contact...)
    HTML5的localStorage和sessionStorage
    promise async await 结合 demo2
    promise async await 结合 demo1
    git 命令
    git
    new 优先级的题目
  • 原文地址:https://www.cnblogs.com/taikongbai/p/3539507.html
Copyright © 2011-2022 走看看