日期:2014-05-17 浏览次数:20854 次
using System; using System.Collections.Generic; using System.Text; using System.Net; namespace winhttptest { class Program { static void Main(string[] args) { WinHttp.WinHttpRequest whr = new WinHttp.WinHttpRequest(); string url = Console.ReadLine(); while (url != string.Empty) { whr.Open("GET", url, false); whr.Send(""); string html = whr.ResponseText; Console.WriteLine(html); url = Console.ReadLine(); } } } }
string html = whr.ResponseText; html = Encoding.GetEncoding("GB2312").GetString(Encoding.UTF8.GetBytes(html));
------解决方案--------------------
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO; namespace winhttptest { class Program { private static string GetResponse(string url) { url.Trim(); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.AllowAutoRedirect = true; req.MaximumAutomaticRedirections = 3; //text/xml;charset=uft-8 req.UserAgent = "Mozilla/4.0 (compatible;MSIE 6.0;Windows NT 5.2;.NET CLR 1.1.4322)"; req.Referer = req.RequestUri.ToString(); req.KeepAlive = true; //req.Method = "Get"; req.Timeout = -1; HttpWebResponse webresponse = null; try { webresponse = (HttpWebResponse)req.GetResponse(); if (webresponse != null) { StreamReader reader = new StreamReader(webresponse.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312")); return reader.ReadToEnd(); } } catch (System.Net.WebException ex) { return ex.Message; } if (webresponse != null) { return ""; } return ""; } static void Main(string[] args) { string html = GetResponse("http://news.sina.com.cn/c/2008-10-12/095416439207.shtml"); Console.WriteLine(html); Console.ReadLine(); } } }
------解决方案--------------------
主要还是编码问题 有些是utf8 有些是gb2312 根据实际情况修改