需求:.net后台获取制定URL的TITLE信息目前情况:对需要登录的网站,无法获取原因: 这块程序还不会写
public static string GetHtml(string url, Encoding encoding)
    {
        WebClient wc = new WebClient();        byte[] buf = wc.DownloadData(url);
        if (encoding != null) return encoding.GetString(buf);
        string html = Encoding.UTF8.GetString(buf);
        encoding = GetEncoding(html);
        if (encoding == null || encoding == Encoding.UTF8) return html;
        return encoding.GetString(buf);
    }中间需要加入一块登录验证程序,请高手指点下,最好将程序完善好

解决方案 »

  1.   

    先获取目标页面的html 。
    然后正则匹配<title>标签了 。
      

  2.   

    获取 string get_Html = GetHtml("http://www.baidu.com/index.html", "gb2312");
                
                string pattern = @"<title[^>]*>([^<]*)</title>";
                string title_all = Regex.Match(get_Html, pattern).Value;//输出:<title>百度一下,你就知道      </title>
                string title_content = Regex.Match(get_Html, pattern).Groups[1].Value;//输出:百度一下,你就知道
    得到htmlpublic static string GetHtml(string url, string charSet)
            {
                string strWebData = string.Empty;
                try
                {
                    WebClient myWebClient = new WebClient(); //创建WebClient实例
                    byte[] myDataBuffer = myWebClient.DownloadData(url);
                    strWebData = System.Text.Encoding.Default.GetString(myDataBuffer);
                    //获取网页字符编码描述信息 
                    if (string.IsNullOrEmpty(charSet))
                    {
                        Match charSetMatch = Regex.Match(strWebData, "<meta([^>]*)charset=(\")?(.*)?\"", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                        string webCharSet = charSetMatch.Groups[3].Value.Trim().ToLower();
                        if (webCharSet != "gb2312")
                        {
                            webCharSet = "utf-8";
                        }
                        if (System.Text.Encoding.GetEncoding(webCharSet) != System.Text.Encoding.Default)
                        {
                            strWebData = System.Text.Encoding.GetEncoding(webCharSet).GetString(myDataBuffer);
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
                return strWebData;
            }