string html;
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
request.UserAgent = Comm.GetRandomUserAgent();
var response = request.GetResponse();
Stream stream = response.GetResponseStream();
byte[] data = new byte[1024];
int count = 1024;
StringBuilder sb = new StringBuilder();
MemoryStream ms = new MemoryStream();
do
{
count = stream.Read(data, 0, 1024);
sb.Append(Encoding.UTF8.GetString(data, 0, count));
} while (count >0);
stream.Close();
response.Close();
html = sb.ToString();
问题是如果一个汉字正好在1024的边界上跨两个循环的话这个汉字会成为乱码。大家有什么好的办法可以解决?
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
html =reader.ReadToEnd();
using System;
using System.Web;
using System.Net;
using System.IO;
using System.Text;
namespace bxg.framework.net
{
public class UrlRequest
{
public static HttpWebResponse Send(string url, string contentType, string method, CookieContainer defaultCookieContainer, Encoding encoding, string postData, out CookieContainer cookieContainer)
{
try
{
HttpWebRequest hwr;
hwr = (HttpWebRequest)WebRequest.Create(url);
if (defaultCookieContainer != null)
hwr.CookieContainer = defaultCookieContainer;
else
hwr.CookieContainer = new CookieContainer();
hwr.KeepAlive = false;
hwr.ContentLength = 0;
if (!string.IsNullOrEmpty(contentType))
hwr.ContentType = contentType;
if (!string.IsNullOrEmpty(method))
hwr.Method = method;
if (!string.IsNullOrEmpty(postData))
{
byte[] byte1 = encoding.GetBytes(postData);
hwr.ContentLength = byte1.Length;
Stream stream = hwr.GetRequestStream();
stream.Write(byte1, 0, byte1.Length);
stream.Close();
}
HttpWebResponse response = (HttpWebResponse)hwr.GetResponse();
cookieContainer = hwr.CookieContainer;
return response;
}
catch (System.Exception e)
{
throw e;
}
}
public static HttpWebResponse Send(string url)
{
return Send(url, string.Empty, "GET", Encoding.UTF8, string.Empty);
}
public static HttpWebResponse Send(string url, string method)
{
string contentType = "";
if (method == "POST")
contentType = "application/x-www-form-urlencoded";
return Send(url, contentType, method, Encoding.UTF8, string.Empty);
}
public static HttpWebResponse Send(string url, string contentType, string method)
{
return Send(url, contentType, method, Encoding.UTF8, string.Empty);
}
public static HttpWebResponse Send(string url, string contentType, string method, Encoding encoding, string postData)
{
CookieContainer cookieContainer;
return Send(url, contentType, method, encoding, postData, out cookieContainer);
}
public static HttpWebResponse Send(string url, string contentType, string method, Encoding encoding, string postData, out CookieContainer cookieContainer)
{
return Send(url, contentType, method, null, encoding, postData, out cookieContainer);
}
public static string GetText(string url)
{
string fileBody = string.Empty;
StreamReader reader = null;
Stream stream = null;
try
{
stream = GetStream(url);
reader = new StreamReader(stream, Encoding.UTF8);
fileBody = reader.ReadToEnd();
}
finally
{
if (null != stream)
stream.Close();
}
return fileBody;
}
public static Stream GetStream(string url)
{
WebResponse response = Send(url);
Stream stream = null;
if (response != null)
stream = response.GetResponseStream();
return stream;
}
public static void Save(string file, string fileName)
{
FileStream fs = null;
try
{
fs = File.Create(fileName);
byte[] buffer;
buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(file);
fs.Write(buffer, 0, buffer.Length);
fs.Close();
}
catch (IOException e)
{
throw e;
}
finally
{
if (fs != null) fs.Close();
}
}
public static void Save(Stream file, string fileName)
{
FileStream fs = null;
try
{
fs = File.Create(fileName);
byte[] buffer = new byte[1024];
int l;
do
{
l = file.Read(buffer, 0, buffer.Length);
if (l > 0) fs.Write(buffer, 0, l);
}
while (l > 0);
}
catch (IOException e)
{
throw e;
}
finally
{
if (fs != null) fs.Close();
file.Close();
}
}
}
}
好的方法就是用StreamReader,TextReader等流读取器替换你的Encoding.UTF8.GetString.