C#获取界面内容

来源:互联网 发布:java execfile 编辑:程序博客网 时间:2024/06/05 14:16

1、用WebClient获取界面内容

static void Main(string[] args)        {            WebClient MyWebClient = new WebClient();            MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据            Byte[] pageData = MyWebClient.DownloadData("http://www.baidu.com"); //从指定网站下载数据            //string pageHtml = Encoding.Default.GetString(pageData);  //网站页面采用的是GB2312                      string html = Encoding.UTF8.GetString(pageData); //网站页面采用的是UTF-8            Console.WriteLine(html);//在控制台输入获取的内容            using (StreamWriter sw = new StreamWriter("c:\\ouput.html"))//将获取的内容写入文本            {            sw.Write(html);            }            Console.ReadLine(); //让控制台不一闪而过              }
2、HttpWebRequest获取界面内容

static void Main(string[] args)        {            string uri = "http://www.baidu.com";            HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(uri);            Req.Method = "GET";// 浏览器和服务器交互            Req.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1;)";// 浏览器的类型,IE或者Firefox            Req.AllowAutoRedirect = true;// 是否允许自动重定向(自动跳转)            Req.MaximumAutomaticRedirections = 3;// 自动跳转的次数            Req.Timeout = 50000;// 超时时间50000=50秒            Req.KeepAlive = true;//  是否建立TCP持久连接            HttpWebResponse response = (HttpWebResponse)Req.GetResponse();            Stream stream = response.GetResponseStream();            Encoding myEncoding = Encoding.GetEncoding("UTF-8");            StreamReader streamReader = new StreamReader(stream, myEncoding);            string html = streamReader.ReadToEnd();            Console.Write(html);            using (StreamWriter sw = new StreamWriter("c:\\ouput.html"))//将获取的内容写入文本            {            sw.Write(html);            }            Console.ReadLine(); //让控制台不一闪而过              }

3、获得CookieContainer里的cookie

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]);            request.CookieContainer = new CookieContainer();            HttpWebResponse response = (HttpWebResponse) request.GetResponse();            // Print the properties of each cookie.            foreach (Cookie cook in response.Cookies)            {                Console.WriteLine("Cookie:");                Console.WriteLine("{0} = {1}", cook.Name, cook.Value);}

4、忽略https证书报错

System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>                {                    return true;                };

5、打开IE

            InternetExplorer IE = new InternetExplorer();            IE.Visible = true;            object nil = new object();            string CnblogUrl = "http://www.baidu.com";            IE.Navigate(CnblogUrl, ref nil, ref nil, ref nil, ref nil);            Thread.Sleep(3000);

6、自动在界面输入某个值,查询

static void Main()        {            InternetExplorer IE = new InternetExplorer();            IE.Visible = true;            object nil = new object();            string Url = "http://www.baidu.com";            IE.Navigate(Url, ref nil, ref nil, ref nil, ref nil);            Thread.Sleep(3000);            HTMLDocument doc = (HTMLDocument)IE.Document;            // 百度主页上搜索框            HTMLInputElement SearchTextBox = (HTMLInputElement)doc.getElementById("kw");            SearchTextBox.value = "yeyang";            // 查找按钮            HTMLInputElement SearchButton = (HTMLInputElement)doc.getElementById("su");            SearchButton.click();            // 关闭IE            Thread.Sleep(3000);            IE.Quit();        }



------------------------------------------------------------------------------------------------------------------

注意:目前HttpWebRequest  已过时



0 0