Java、C#双语版HttpHelper类(解决网页抓取乱码问题)(四)

2014-11-24 02:08:35 · 作者: · 浏览: 3
va lidationCallback(RemoteCertificateva lidate);
}
if (method.ToUpper() == "POST")
{
if (!string.IsNullOrEmpty(data))
{
byte[] bytes = Encoding.UTF8.GetBytes(data);
if (config.GZipCompress)
{
using (MemoryStream stream = new MemoryStream())
{
using (GZipStream gZipStream = new GZipStream(stream, CompressionMode.Compress))
{
gZipStream.Write(bytes, 0, bytes.Length);
}
bytes = stream.ToArray();
}
}
request.ContentLength = bytes.Length;
request.GetRequestStream().Write(bytes, 0, bytes.Length);
}
else
{
request.ContentLength = 0;
}
}
return (HttpWebResponse)request.GetResponse();
}
}
public class HttpConfig
{
public string Referer { get; set; }
///
/// 默认(text/html)
///
public string ContentType { get; set; }
public string Accept { get; set; }
public string AcceptEncoding { get; set; }
///
/// 超时时间(毫秒)默认100000
///
public int Timeout { get; set; }
public string UserAgent { get; set; }
///
/// POST请求时,数据是否进行gzip压缩
///
public bool GZipCompress { get; set; }
public bool KeepAlive { get; set; }
public string CharacterSet { get; set; }
public HttpConfig()
{
this.Timeout = 100000;
this.ContentType = "text/html; charset=" + Encoding.UTF8.WebName;
this.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36";
this.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
this.AcceptEncoding = "gzip,deflate";
this.GZipCompress = false;
this.KeepAlive = true;
this.CharacterSet = "UTF-8";
}
}
}