C#模拟登录的htmlHelper类

时间:2022-10-13 10:18:19
     public class HTMLHelper
{
/// <summary>
/// 获取CooKie
/// /// </summary>
/// /// <param name="loginUrl"></param>
/// /// <param name="postdata"></param>
/// /// <param name="header"></param>
/// /// <returns></returns>
public static CookieContainer GetCooKie(string loginUrl, HttpHeader header)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
CookieContainer cc = new CookieContainer();
request = (HttpWebRequest)WebRequest.Create(loginUrl);
request.Method = "GET";
request.ContentType = header.contentType; request.AllowAutoRedirect = false;
request.CookieContainer = cc;
request.KeepAlive = true; //接收响应
response = (HttpWebResponse)request.GetResponse();
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
CookieCollection cook = response.Cookies; //Cookie字符串格式
string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
return cc;
}
catch (Exception ex)
{
throw ex;
}
} /// <summary>
/// 获取CooKie
/// /// </summary>
/// /// <param name="loginUrl"></param>
/// /// <param name="postdata"></param>
/// /// <param name="header"></param>
/// /// <returns></returns>
public static CookieContainer GetCooKie(string loginUrl, string postdata, HttpHeader header)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
CookieContainer cc = new CookieContainer();
request = (HttpWebRequest)WebRequest.Create(loginUrl);
request.Method = header.method;
request.ContentType = header.contentType;
byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata);
request.ContentLength = postdatabyte.Length;
request.AllowAutoRedirect = false;
request.CookieContainer = cc;
request.KeepAlive = true;
//提交请求
Stream stream;
stream = request.GetRequestStream();
stream.Write(postdatabyte, , postdatabyte.Length);
stream.Close();
//接收响应
response = (HttpWebResponse)request.GetResponse();
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
CookieCollection cook = response.Cookies; //Cookie字符串格式
string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
return cc;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 获取html
/// </summary>
/// <param name="getUrl"></param>
/// <param name="cookieContainer"></param>
/// <param name="header"></param>
/// <returns></returns> public static string GetHtml(string getUrl, CookieContainer cookieContainer, HttpHeader header)
{
Thread.Sleep();
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(getUrl);
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.ContentType = header.contentType;
httpWebRequest.ServicePoint.ConnectionLimit = header.maxTry;
httpWebRequest.Referer = getUrl;
httpWebRequest.Accept = header.accept;
httpWebRequest.UserAgent = header.userAgent;
httpWebRequest.Method = "GET";
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
string html = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
httpWebRequest.Abort();
httpWebResponse.Close();
return html;
}
catch (Exception e)
{
if (httpWebRequest != null) httpWebRequest.Abort();
if (httpWebResponse != null) httpWebResponse.Close();
return string.Empty;
}
} /// <summary>
/// 获取html
/// </summary>
/// <param name="getUrl"></param>
/// <param name="cookieContainer"></param>
/// <param name="header"></param>
/// <returns></returns> public static string GetHtml(string getUrl,string post, CookieContainer cookieContainer, HttpHeader header,Encoding en)
{
Thread.Sleep();
HttpWebRequest myHttpWebRequest = null;
HttpWebResponse httpWebResponse = null;
try
{
byte[] oneData = Encoding.Default.GetBytes(post);
Uri uri = new Uri(getUrl);
myHttpWebRequest = (HttpWebRequest)WebRequest.Create(uri);//请求的URL
myHttpWebRequest.CookieContainer = cookieContainer;//*发送COOKIE
myHttpWebRequest.Method = header.method;
myHttpWebRequest.ContentType = header.contentType;
myHttpWebRequest.ContentLength = oneData.Length;
Stream newMyStream = myHttpWebRequest.GetRequestStream();
newMyStream.Write(oneData, , oneData.Length); try
{
httpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream(), en); string str = sr.ReadToEnd(); string msg = string.Empty; return str;
}
catch (Exception ex)
{
return string.Empty;
}
}
catch (Exception e)
{
if (myHttpWebRequest != null) myHttpWebRequest.Abort();
if (httpWebResponse != null) httpWebResponse.Close();
return string.Empty;
}
return string.Empty;
} /// <summary>
/// 获取html
/// </summary>
/// <param name="getUrl"></param>
/// <param name="cookieContainer"></param>
/// <param name="header"></param>
/// <returns></returns> public static string GetHtml(string getUrl, string post, CookieContainer cookieContainer,out CookieContainer co, HttpHeader header, Encoding en)
{
Thread.Sleep();
HttpWebRequest myHttpWebRequest = null;
HttpWebResponse httpWebResponse = null;
co = new CookieContainer();
try
{
byte[] oneData = Encoding.Default.GetBytes(post);
Uri uri = new Uri(getUrl);
myHttpWebRequest = (HttpWebRequest)WebRequest.Create(uri);//请求的URL
if (cookieContainer.Count > )
{
myHttpWebRequest.CookieContainer = cookieContainer;
}
else
{
myHttpWebRequest.CookieContainer = co;
} myHttpWebRequest.Method = header.method;
myHttpWebRequest.ContentType = header.contentType;
myHttpWebRequest.ContentLength = oneData.Length;
Stream newMyStream = myHttpWebRequest.GetRequestStream();
newMyStream.Write(oneData, , oneData.Length); try
{
httpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream(), en); httpWebResponse.Cookies = myHttpWebRequest.CookieContainer.GetCookies(myHttpWebRequest.RequestUri);
CookieCollection cook = httpWebResponse.Cookies; //Cookie字符串格式
string strcrook = myHttpWebRequest.CookieContainer.GetCookieHeader(myHttpWebRequest.RequestUri); string str = sr.ReadToEnd(); string msg = string.Empty; return str;
}
catch (Exception ex)
{
return string.Empty;
}
}
catch (Exception e)
{
if (myHttpWebRequest != null) myHttpWebRequest.Abort();
if (httpWebResponse != null) httpWebResponse.Close();
return string.Empty;
}
return string.Empty;
} /// <summary>
/// 获取Stream
/// </summary>
/// <param name="getUrl"></param>
/// <param name="cookieContainer"></param>
/// <param name="header"></param>
/// <returns></returns> public static Stream GetStream(string getUrl, CookieContainer cookieContainer, HttpHeader header)
{
Thread.Sleep();
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(getUrl);
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.ContentType = header.contentType;
httpWebRequest.ServicePoint.ConnectionLimit = header.maxTry;
httpWebRequest.Referer = header.referer;
httpWebRequest.Accept = header.accept;
httpWebRequest.UserAgent = header.userAgent;
httpWebRequest.Method = "GET";
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream(); return responseStream;
}
catch (Exception e)
{
if (httpWebRequest != null) httpWebRequest.Abort();
if (httpWebResponse != null) httpWebResponse.Close();
return null;
}
} public static Stream GetStream(string getUrl, CookieContainer cookieContainer, out CookieContainer co, HttpHeader header)
{ Thread.Sleep();
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
co = new CookieContainer();
try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(getUrl);
if (cookieContainer.Count > )
{
httpWebRequest.CookieContainer = cookieContainer;
}
else
{
httpWebRequest.CookieContainer = co;
} httpWebRequest.ContentType = header.contentType;
httpWebRequest.ServicePoint.ConnectionLimit = header.maxTry;
httpWebRequest.Referer = getUrl;
httpWebRequest.Accept = header.accept;
httpWebRequest.UserAgent = header.userAgent;
httpWebRequest.Method = "GET";
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream(); httpWebResponse.Cookies = httpWebRequest.CookieContainer.GetCookies(httpWebRequest.RequestUri);
CookieCollection cook = httpWebResponse.Cookies; //Cookie字符串格式
string strcrook = httpWebRequest.CookieContainer.GetCookieHeader(httpWebRequest.RequestUri); return responseStream;
}
catch (Exception e)
{
if (httpWebRequest != null) httpWebRequest.Abort();
if (httpWebResponse != null) httpWebResponse.Close();
return null;
}
} } public class HttpHeader
{
public string contentType { get; set; }
public string accept { get; set; }
public string userAgent { get; set; }
public string method { get; set; }
public int maxTry { get; set; }
public string referer { get; set; }
}