用 MSXML2.ServerXMLHTTP 一定行,其他几个好像不怎么行。
另外一篇文章 采集MSXML2.XMLHTTP“拒绝访问”的解决方案
/// <summary> /// 获取远程文件源代码 /// </summary> /// <param name="url">远程url</param> /// <returns></returns> public string GetHtmlCode(string Url) { string s = ""; MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass(); _xmlhttp.open("GET", Url, false, null, null); _xmlhttp.send(""); if (_xmlhttp.readyState == 4) { s = System.Text.Encoding.Default.GetString((byte[])_xmlhttp.responseBody); } return s; }
上面这段代码不行,下面一段代码可以:
1 private string GetHtmlCode(string Url) 2 { 3 string strResult = ""; 4 5 try 6 { 7 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); 8 request.Method = "GET"; 9 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 10 Stream streamReceive = response.GetResponseStream(); 11 Encoding encoding = Encoding.GetEncoding("GB2312"); 12 StreamReader streamReader = new StreamReader(streamReceive, encoding); 13 strResult = streamReader.ReadToEnd(); 14 } 15 catch { } 16 17 return strResult; 18 }