C# HTTP

来源:互联网 发布:完全变态 知乎 编辑:程序博客网 时间:2024/06/08 11:17
using System;using System.IO;using System.Net;public class Program{    public static string getHttp(string url)    {        string text="";        byte[] receiveBytes = new byte[byte.MaxValue];        int bytes;        try        {            HttpWebRequest httpWebRequest = WebRequest.Create(url) as HttpWebRequest;            HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;            Stream responseStream = httpWebResponse.GetResponseStream();            while (true)            {                bytes = responseStream.Read(receiveBytes, 0, receiveBytes.Length);                if(bytes<=0)                    break;                text += System.Text.Encoding.UTF8.GetString(receiveBytes, 0, bytes);            }        }        catch (Exception e)        {            Console.WriteLine(e);        }        return text;    }   public static void Main(string[]args)   {        System.Console.Write(getHttp("http://www.baidu.com"));   }}
0 0
原创粉丝点击