public static string getContent(string Url, string encode)
{
string strResult = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
//声明一个HttpWebRequest请求
request.Timeout = 30000;
//设置连接超时时间
request.Headers.Set("Pragma", "no-cache");
request.UserAgent = "Mozilla/5.0 (Windows NT 5.2; rv:8.0) Gecko/20100101 Firefox/8.0";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
byte[] data = null;
string ce = response.Headers[HttpResponseHeader.ContentEncoding];
int ContentLength = (int)response.ContentLength;
Stream s = response.GetResponseStream();
int c = 1024 * 10;
if (ContentLength < 0)
{
data = new byte[c];
MemoryStream ms = new MemoryStream();
int l = s.Read(data, 0, c);
while (l > 0)
{
Console.WriteLine("1--> " + l);
ms.Write(data, 0, l);
l = s.Read(data, 0, c);
}
data = ms.ToArray();
ms.Close();
}
else
{
data = new byte[ContentLength];
int pos = 0;
while (ContentLength > 0)
{
int l = s.Read(data, pos, ContentLength);
pos += l;
ContentLength -= l;
Console.WriteLine("2--> " + l);
}
}
s.Close();
response.Close();
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding(encode);
strResult = encoding.GetString(data);
}
catch
{
//throw;
}
return strResult;
}
找到了个c#的代码片段,有用: