在C#应用程序中,要获取HTML源代码是非常容易的事。但是,近日我发现soso的搜吧居然获取不到HTML代码。请问如何解决?
比如说这句网址的HTML代码:
http://post.soso.com/cgi-bin/cgimain?proto=8&tid=407966&pgn=1谢谢!近日我在一个采集程序时,想采集搜吧呢。请大家贴代码前测试一下,这样可以节省你我的时间。

解决方案 »

  1.   

    懒得写代码,随便扯下。
    403会不会是服务器对User-Agent进行了验证?弄个AddHeader()添加上Internet Explorer试试
      

  2.   

    既然有这么多朋友问这问题,我就写了一个小程序,希望大家能理解,拖个button1 listBox1
    在button事件里写代码,参考代码如下:(不要忘了加引用哦)
    using System.Net;
    using System.IO; 
    private void button1_Click(object sender, EventArgs e)
            {
               
                WebRequest request = WebRequest.Create(
                  "http://post.soso.com/cgi-bin/cgimain?proto=8&tid=407966&pgn=1"); 
                request.Credentials = CredentialCache.DefaultCredentials;
                WebResponse response = request.GetResponse();
                Stream dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream);
                string responseFromServer = "";
                string strTemp;
                while ( (strTemp = reader .ReadLine ()) != null )
                {
                     listBox1.Items.Add(strTemp );
                }
                reader.Close();
                response.Close();        }
    -------------------------------------------
    在vs2005 winform下测试通过,并顺利取得html代码,只要将网址改成你想要的,基本都能获得html(对用不常用编码写的html代码,要考虑转换了再显示出来),在此例中是将它show到listbox
    中,既然能获得流了,你要怎么处理都是不难的事了,呵呵
      

  3.   

    你可以看看我的CSDN Reader的源代码可能对你有帮助
      
    *****************************************************************************
    欢迎使用CSDN论坛阅读器 : CSDN Reader(附全部源代码) 
    http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
      

  4.   

    把模板页面写成一个用户控件,然后render就可以了.
      

  5.   

    dyw31415926(dyw31415926) ,这位兄弟你好。你难道没测试过你的代码吗?
    你的代码在读SOSO时,总是返回403错误。
      

  6.   

    down 個 VS2005 試試。
      

  7.   

    我用php很容易就抓下来了啊,没用什么特别的操作呢。
    <?php
    $file = fopen ("http://post.soso.com/cgi-bin/cgimain?proto=8&tid=407966&pgn=1", "r");
    if (!$file) {
       echo "<p>Unable to open remote file.\n";
       exit;
    }
    while (!feof ($file)) {
       $line = fgets ($file, 1024);
    print($line);
    }
    fclose($file);
    ?> 
      

  8.   

    用InternetExplorerClass方法:
    ---------------------------------------------------------------------------------
    InternetExplorerClass m_IE=new InternetExplorerClass();
    m_IE.Silent=true;
    stbr.Text="正在打开IE...";
    object o=null;
    string url="http://post.soso.com/cgi-bin/cgimain?proto=8&tid=407966&pgn=1";
    m_IE.Navigate(url,ref o,ref o,ref o,ref o);
    stbr.Text="正在打开网页...";
    while(m_IE.ReadyState!=SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
    {
    Thread.Sleep(500);
    Application.DoEvents();
    }
    HTMLDocument doc=(HTMLDocument)m_IE.Document;
    //MessageBox.Show(doc.body.outerHTML);//doc.createDocumentFromUrl
    rch1.AppendText(doc.body.outerHTML);
    m_IE.Quit();
      

  9.   

    楼上的兄弟,你这个方法到时必须另外调用一个dll文件的。
    不符合我的项目要求。
      

  10.   

    可以去找网络蜘蛛,我曾经写过一个,关键在于解析html代码,通过webrequest和webresponse来写
      

  11.   

    是否可以用webbrowser,设置路径,然后得到它的htmlText呢?
      

  12.   

    楼主,我的代码不需要别的DLL;
    你只须将axWebBrowser放在窗体中,然后删除该控件
    然后右击"引用",添加Microsoft.mshtml
    在文件中添加:
    using SHDocVw;
    using mshtml;
    我上述代码就可以运行了,不需要WebBrowser控件
      

  13.   

    还有一办法只需要添加对mshtml的引用,using mshtml,可以得到指定URL的HTMLDocument doc对象(用HTMLDocument的createDocumentFromUrl)
    那样同样可以得到该文档所有节点,得到的doc.documentElement.outerHTML好象和HTML的源文件差不多
      

  14.   

    最简单通知的方法, 用JS:
    <input type=button value=查看网页源代码 onclick="window.location = 'view-source:'+ 'http://post.soso.com/cgi-bin/cgimain?proto=8&tid=407966&pgn=1'">
      

  15.   

    解决了!                WebClient client = new WebClient();
                    client.Headers.Set("User-Agent", "Microsoft Internet Explorer");//此句是关键
                    Byte[] pageData = client.DownloadData(textBox2 .Text);
                    textBox1.Text = Encoding.GetEncoding("gb2312").GetString(pageData);
      

  16.   

    现在CSDN上倒分贴太多,以后真不想化时间在回贴上了,最近给别人回了好多贴,其中有一贴好象被删除了,明显倒分的,唉,不知道说什么好