var result = SlRequest.GetHtmlNoShakeHand(url, Encoding.UTF8, 5000, SlHttpActionType.Post, rparam, false);
/// <summary>
/// 获取url里面的html
/// </summary>
/// <param name="url"></param>
/// <param name="encoding"></param>
/// <param name="timeout"></param>
/// <param name="httpActionType"></param>
/// <param name="postParameter"></param>
/// <returns></returns>
public static string GetHtmlNoShakeHand(string url, Encoding encoding = null, int timeout = 1000, SlHttpActionType httpActionType = SlHttpActionType.Get, string postParameter = "",bool isHandShaking=true)
{
if (encoding == null)
{
encoding = Encoding.Default;
}
if (!isHandShaking)
{
System.Net.ServicePointManager.Expect100Continue = false;
}
var request = HttpWebRequest.Create(url);
request.Proxy = null;
request.Timeout = timeout;
request.Headers.Add("Accept-Encoding", "gzip,deflate");
if (httpActionType == SlHttpActionType.Post)
{
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
if (!string.IsNullOrEmpty(postParameter))
{
var postData = encoding.GetBytes(postParameter);
request.ContentLength = postData.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(postData, 0, postData.Length);
}
}
}
else
{
request.Method = "GET";
}
using (var response = (HttpWebResponse)request.GetResponse())
{
if (response.ContentEncoding.ToLower().Contains("gzip"))
{
using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
{
using (var streamReader = new StreamReader(stream, encoding))
{
var html = streamReader.ReadToEnd();
return html;
}
}
}
else if (response.ContentEncoding.ToLower().Contains("deflate"))
{
using (DeflateStream stream = new DeflateStream(response.GetResponseStream(), CompressionMode.Decompress))
{
using (var streamReader = new StreamReader(stream, encoding))
{
var html = streamReader.ReadToEnd();
return html;
}
}
}
else
{
using (var responseStream = response.GetResponseStream())
{
using (var streamReader = new StreamReader(responseStream, encoding))
{
var html = streamReader.ReadToEnd();
return html;
}
}
}
}
}