C#网页信息采集方法汇总

 更新时间:2014年10月29日 09:31:49   投稿:shichen2014  
这篇文章主要介绍了C#网页信息采集方法,实例汇总了三种常用的方法,是非常实用的技巧,需要的朋友可以参考下

本文实例总结了三种常用的C#网页信息采集方法。分享给大家供大家参考。具体实现方法如下:

一、通过HttpWebResponse 来获取

复制代码 代码如下:
public static string CheckTeamSiteUrl(string url) 

        string response = ""; 
        HttpWebResponse httpResponse = null; 
 
        //assert: user have access to URL  
        try 
        { 
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url); 
            httpRequest.Headers.Set("Pragma", "no-cache"); 
 
                // request.Headers.Set("KeepAlive", "true"); 
 
                httpRequest.CookieContainer = new CookieContainer(); 
 
 
 
                httpRequest.Referer = url; 
 
                httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; 
 
               
 
            httpRequest.Credentials = System.Net.CredentialCache.DefaultCredentials; 
            httpResponse = (HttpWebResponse)httpRequest.GetResponse(); 
             
        } 
        catch (Exception ex) 
        { 
            throw new ApplicationException("HTTP 403 Access denied, URL: " + url, ex); 
        } 
 
        //if here, the URL is correct and the user has access  
        try 
        { 
            string strEncod = httpResponse.ContentType; 
            StreamReader stream; 
            if (strEncod.ToLower().IndexOf("utf") != -1) 
            { 
                stream = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.UTF8); 
            } 
            else 
            { 
                stream = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.Default); 
            } 
            
            char[] buff = new char[4000]; 
            stream.ReadBlock(buff,0,4000); 
            response = new string(buff); 
            stream.Close(); 
            httpResponse.Close(); 
        } 
        catch (Exception ex) 
        { 
            throw new ApplicationException("HTTP 404 Page not found, URL: " + url, ex); 
        } 
        return response; 
}

 
二、通过 WebResponse 来获取

复制代码 代码如下:
public static string getPage(String url) 
{
        WebResponse result = null; 
        string resultstring = ""; 
        try 
        { 
            WebRequest req = WebRequest.Create(url); 
            req.Timeout = 30000; 
            result = req.GetResponse(); 
            Stream ReceiveStream = result.GetResponseStream(); 
 
            //read the stream into a string 
            //StreamReader sr = new StreamReader(ReceiveStream, System.Text.Encoding.UTF8); 
            string strEncod = result.ContentType; 
            StreamReader sr; 
            if (strEncod.ToLower().IndexOf("utf") != -1) 
            { 
                sr = new StreamReader(ReceiveStream, System.Text.Encoding.UTF8); 
            } 
            else 
            { 
                sr = new StreamReader(ReceiveStream, System.Text.Encoding.Default); 
            } 
            resultstring = sr.ReadToEnd(); 
            js.alert(resultstring); 
            //Console.WriteLine(resultstring); 
        } 
        catch 
        { 
            throw new Exception(); 
        } 
        finally 
        { 
            if (result != null) 
            { 
                result.Close(); 
            } 
        } 
        return resultstring; 
}

 
三、通过WebClient来获取

复制代码 代码如下:
public string get(int length) 

        try 
        { 
            getEncodeing(); 
            WebClient wb = new WebClient(); 
            Stream response = wb.OpenRead(url); 
            StreamReader reader = new StreamReader(response, this.encoding, true, 256000); 
            char[] a = new char[length]; 
            int i  = reader.Read(a,0,length); 
            reader.Close(); 
            return new string(a); 
        } 
        catch (Exception e) 
        { 
            return e.Message; 
            //return null; 
        } 

private void getEncodeing() 
{
        switch (this.encode) 
        { 
            case "UTF-8": encoding = Encoding.UTF8; break; 
            case "GB2312": encoding = Encoding.GetEncoding("GB2312"); break; 
            case "ASCII": encoding = Encoding.ASCII; break; 
            default: encoding = Encoding.GetEncoding(encode); break; 
        } 
}

希望本文所述对大家的C#程序设计有所帮助。

相关文章

  • Unity实现高效的音效管理类的示例代码

    Unity实现高效的音效管理类的示例代码

    这篇文章主要为大家详细介绍了如何通过Unity实现高效的音效管理类,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的可以了解一下
    2023-03-03
  • C#实现WinForm全屏置顶的示例代码

    C#实现WinForm全屏置顶的示例代码

    我们在运行一些 Windows 应用程序的时候,需要将其运行在窗体置顶的模式,并且进入全屏状态,本文将介绍如何使用 C# 来实现 WinForm 的全屏置顶的基本功能,感兴趣的可以了解下
    2024-12-12
  • c# 实现语音合成

    c# 实现语音合成

    这篇文章主要介绍了c# 实现语音合成的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
    2020-12-12
  • C#使用log4net结合sqlite数据库实现记录日志

    C#使用log4net结合sqlite数据库实现记录日志

    因为结构化的数据库存储的日志信息,可以写专门的软件读取历史日志信息,通过各种条件筛选,可操作性极大增强,有这方面需求的开发人员可以考虑,本文给大家介绍了C#使用log4net结合sqlite数据库记录日志,需要的朋友可以参考下
    2024-10-10
  • unity 实现摄像机绕某点旋转一周

    unity 实现摄像机绕某点旋转一周

    这篇文章主要介绍了unity 实现摄像机绕某点旋转一周,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • C#查询SqlServer数据库并返回单个值的方法

    C#查询SqlServer数据库并返回单个值的方法

    这篇文章主要介绍了C#查询SqlServer数据库并返回单个值的方法,涉及C#操作SQLServer数据库查询的相关技巧,需要的朋友可以参考下
    2015-06-06
  • C#实现简单订单管理程序

    C#实现简单订单管理程序

    这篇文章主要为大家详细介绍了C#实现简单订单管理程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • 详解C#如何使用读写锁控制多线程写入

    详解C#如何使用读写锁控制多线程写入

    这篇文章主要为大家详细介绍了C#如何使用读写锁控制多线程写入,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-01-01
  • C#清除WebBrowser中Cookie缓存的方法

    C#清除WebBrowser中Cookie缓存的方法

    这篇文章主要介绍了C#清除WebBrowser中Cookie缓存的方法,涉及C#针对WebBrowser控件的操作技巧,非常简单实用,需要的朋友可以参考下
    2016-05-05
  • C# 设计模式系列教程-外观模式

    C# 设计模式系列教程-外观模式

    外观模式松散了客户端与子系统的耦合关系,让子系统内部的模块能更容易扩展和维护。
    2016-06-06

最新评论