代码:
//得到指定url页面的源码
  public string getUrltoHtml(string Url)
  {
  string str = string.Empty;

  try
  {
  WebRequest wReq = WebRequest.Create(Url);
  WebResponse wResp = wReq.GetResponse();
  Stream respStream  = wResp.GetResponseStream();
  StreamReader reader = new StreamReader(respStream, code);
  str = reader.ReadToEnd();
  if ( str.Length > 0 )
  {
                        Response.Write("    yes");
  }
  return str;
  }
  catch//(Exception ex)
  {
  Response.Write("no1");
  }
    return "";
  }datalist的会不一样,怎么回事
用WebRequest请求网页产生的HTML代码:
<table id="DataList1" cellspacing="0" rules="all" bordercolor="DarkKhaki" border="1" width="790">直接访问这个网页产生的代码:
<table id="DataList1" cellspacing="0" rules="all" bordercolor="DarkKhaki" border="1" style="border-color:DarkKhaki;border-width:1px;border-style:solid;width:790px;border-collapse:collapse;">

解决方案 »

  1.   

    加一句 request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
    直接访问可能是按照特定浏览器支持解析过的,而用request输出的应该是按照W3C的HTML标准中包含的,比如说STYLE事实上是扩展的HTML标记,所以request不出来
      

  2.   

    换WebClient 试试WebClient wc = new WebClient();
    byte[] bs = wc.DownloadData("http://cneve.com");
    string html = Encoding.Default.GetString(bs);
    Response.Write(html);
    //下面将获得的html保存到本地
    string saveFile = Server.MapPath("cneve.htm");
    using(TextWriter w = File.CreateText(saveFile))
    {
        w.WriteLine(html);
    }
      

  3.   

    不行啊,显示不包含对UserAgent的定义
      

  4.   

    保你满意。。
    class webrequest
    {
    public static string  GetHtml(string url)
    {
    HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url);
    myHttpWebRequest.Accept=@"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";   
    myHttpWebRequest.UserAgent=@"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.40607; .NET CLR 1.1.4322)";


    try
    {
    HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
                   
    Stream streamResponse=myHttpWebResponse.GetResponseStream();
    StreamReader streamRead = new StreamReader(streamResponse,Encoding.Default);
    string htmlcode=streamRead.ReadToEnd();
    streamRead.Close();

    streamResponse.Close();
    myHttpWebResponse.Close();

    return htmlcode; } catch(Exception e)
    {

    return "error";
    }
    }
    };