c#获取url请求的返回值,C#调用Url

        /// <summary>
        /// 获取url的返回值
        /// </summary>
        /// <param name="url">eg:http://m.weather.com.cn/atad/101010100.html </param>
        public string GetInfo(string url)
        {
            string strBuff = "";
            Uri httpURL = new Uri(url);
            ///HttpWebRequest类继承于WebRequest,并没有自己的构造函数,需通过WebRequest的Creat方法 建立,并进行强制的类型转换 
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(httpURL);
            ///通过HttpWebRequest的GetResponse()方法建立HttpWebResponse,强制类型转换 
            HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
            ///GetResponseStream()方法获取HTTP响应的数据流,并尝试取得URL中所指定的网页内容 
            ///若成功取得网页的内容,则以System.IO.Stream形式返回,若失败则产生ProtoclViolationException错 误。在此正确的做法应将以下的代码放到一个try块中处理。这里简单处理 
            Stream respStream = httpResp.GetResponseStream();
            ///返回的内容是Stream形式的,所以可以利用StreamReader类获取GetResponseStream的内容,并以 
            //StreamReader类的Read方法依次读取网页源程序代码每一行的内容,直至行尾(读取的编码格式:UTF8) 
            StreamReader respStreamReader = new StreamReader(respStream, Encoding.UTF8);
            strBuff = respStreamReader.ReadToEnd();
            return strBuff;
        }

方法二

        /// <summary> 
        /// Get方式获取url地址输出内容 
        /// </summary> /// <param name="url">url</param> 
        /// <param name="encoding">返回内容编码方式,例如:Encoding.UTF8</param> 
         public static String SendRequest(String url,Encoding encoding) 
         { 
             HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); 
             webRequest.Method = "GET"; 
             HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); 
             StreamReader sr = new StreamReader(webResponse.GetResponseStream(), encoding); 
             return sr.ReadToEnd(); 
         }

 

posted on 2015-10-12 15:25  wuzx-blog  阅读(1686)  评论(0编辑  收藏  举报