代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;
namespace WebSiteAccess
{
public class HttpHelper
{
private static string contentType = "application/x-www-form-urlencoded";
private static string accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
private static string userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
private static int currentTry = 0;
#region 属性
private static CookieContainer cookie = new CookieContainer();
/// <summary>
/// Cookie容器
/// </summary>
public static CookieContainer CookieContainer
{
get
{
return cookie;
}
}
private static Encoding encoding = Encoding.GetEncoding("utf-8");
/// <summary>
/// 获取网页源码时使用的编码
/// </summary>
public static Encoding Encoding
{
get
{
return encoding;
}
set
{
encoding = value;
}
}
private static int delay = 3000;//延迟访问防止连续访问被发现
public static int NetworkDelay
{
get
{
Random r = new Random();
return (r.Next(delay / 1000, delay / 1000 * 2)) * 1000;
}
set
{
delay = value;
}
}
private static int maxTry = 300;
public static int MaxTry
{
get
{
return maxTry;
}
set
{
maxTry = value;
}
}
#endregion
/// <summary>
/// 获取指定页面的HTML代码
/// </summary>
/// <param name="url">指定页面的路径</param>
/// <param name="postData">回发的数据</param>
/// <param name="isPost">是否以post方式发送请求</param>
/// <param name="cookieContainer">Cookie集合</param>
/// <returns></returns>
public static string GetHtml(string url, string postData, bool isPost, CookieContainer cookieContainer)
{
if (string.IsNullOrEmpty(postData))
{
return GetHtml(url, cookieContainer);
}
Thread.Sleep(NetworkDelay);//延迟访问
currentTry++;
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
try
{
byte[] byteRequest = Encoding.Default.GetBytes(postData);
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.ContentType = contentType;
httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
httpWebRequest.Referer = url;
httpWebRequest.Accept = accept;
httpWebRequest.UserAgent = userAgent;
httpWebRequest.Method = isPost ? "POST" : "GET";
httpWebRequest.ContentLength = byteRequest.Length;
Stream stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0, byteRequest.Length);
stream.Close();
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, encoding);
string html = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
currentTry = 0;
httpWebRequest.Abort();
httpWebResponse.Close();
return html;
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);
Console.ForegroundColor = ConsoleColor.White;
if (currentTry <= maxTry)
{
GetHtml(url, postData, isPost, cookieContainer);
}
currentTry--;
if (httpWebRequest != null)
{
httpWebRequest.Abort();
} if (httpWebResponse != null)
{
httpWebResponse.Close();
}
return string.Empty;
}
}
/// <summary>
/// 获取指定页面的HTML代码
/// </summary>
/// <param name="url">指定页面的路径</param>
/// <param name="cookieContainer">Cookie集合</param>
/// <returns></returns>
public static string GetHtml(string url, CookieContainer cookieContainer)
{
Thread.Sleep(NetworkDelay);
currentTry++;
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.ContentType = contentType;
httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
httpWebRequest.Referer = url;
httpWebRequest.Accept = accept;
httpWebRequest.UserAgent = userAgent;
httpWebRequest.Method = "GET";
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, encoding);
string html = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
currentTry--;
httpWebRequest.Abort();
httpWebResponse.Close();
return html;
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);
Console.ForegroundColor = ConsoleColor.White;
if (currentTry <= maxTry)
{
GetHtml(url, cookieContainer);
}
currentTry--;
if (httpWebRequest != null)
{
httpWebRequest.Abort();
} if (httpWebResponse != null)
{
httpWebResponse.Close();
}
return string.Empty;
}
}
/// <summary>
/// 获取指定页面的HTML代码
/// </summary>
/// <param name="url">指定页面的路径</param>
/// <returns></returns>
public static string GetHtml(string url)
{
return GetHtml(url, cookie);
}
/// <summary>
/// 获取指定页面的HTML代码
/// </summary>
/// <param name="url">指定页面的路径</param>
/// <param name="postData">回发的数据</param>
/// <param name="isPost">是否以post方式发送请求</param>
/// <returns></returns>
public static string GetHtml(string url, string postData, bool isPost)
{
return GetHtml(url, postData, isPost, cookie);
}
/// <summary>
/// 获取指定页面的Stream
/// </summary>
/// <param name="url">指定页面的路径</param>
/// <param name="cookieContainer">Cookie集合</param>
/// <returns>responseStream</returns>
public static Stream GetStream(string url, CookieContainer cookieContainer)
{
//Thread.Sleep(delay);
currentTry++;
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.ContentType = contentType;
httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
httpWebRequest.Referer = url;
httpWebRequest.Accept = accept;
httpWebRequest.UserAgent = userAgent;
httpWebRequest.Method = "GET";
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
currentTry--;
//httpWebRequest.Abort();
//httpWebResponse.Close();
return responseStream;
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);
Console.ForegroundColor = ConsoleColor.White;
if (currentTry <= maxTry)
{
GetHtml(url, cookieContainer);
}
currentTry--;
if (httpWebRequest != null)
{
httpWebRequest.Abort();
} if (httpWebResponse != null)
{
httpWebResponse.Close();
}
return null;
}
}
/// <summary>
/// 获取指定页面的Stream
/// </summary>
/// <param name="url">指定页面的路径</param>
/// <returns></returns>
public static Stream GetStream(string url)
{
return GetStream(url, cookie);
}
/// <summary>
/// 在客户端提交数据,不太安全
/// </summary>
/// <param name="url">http://172.21.140.125:9704/analytics/saw.dll?Dashboard&_scid=VudJojSNiYU</param>
/// <param name="postData">NQUser=DMS-QA&NQPassword=DMS-QA&saveLangPref=true&icharset=gb2312</param>
public static void PostByClient(string url, string postData)
{
string htmlData = "";
htmlData = string.Format("<form id=\"logonForm\" method=\"post\" action=\"{0}\">", url);
string[] data = postData.Split('&');
foreach (string item in data)
{
string[] key = item.Split('=');
if (key.Length == 2)
{
htmlData += string.Format("<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", key[0], key[1]);
}
}
htmlData += "<script type=\"text/javascript\">";
htmlData += "function PostData(){ var theForm = document.forms['logonForm'];theForm.submit();} PostData();";
htmlData += "</script></form>";
System.Web.HttpContext.Current.Response.Write(htmlData);
//<form id="logonForm" method="post" action="http://172.21.140.125:9704/analytics/saw.dll?Dashboard&_scid=VudJojSNiYU">
// <input type="hidden" name="NQUser" value="DMS-QA" />
// <input type="hidden" name="NQPassword" value="DMS-QA" />
// <input type="hidden" name="saveLangPref" value="true" />
// <input type="hidden" name="icharset" value="gb2312" />
// <script type="text/javascript">
// function PostData(){ var theForm = document.forms['logonForm'];theForm.submit();}
// PostData();
// </script>
//</form>
}
}
}
代码
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.cs" Inherits="WebSiteAccess._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Login" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
<div>
数据:
<div id="div1" runat="server">
</div>
</div>
</form>
</body>
</html>
代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
namespace WebSiteAccess
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
void LoginByServer()
{
string url = "http://172.21.140.125:9704/analytics/saw.dll?Dashboard&_scid=VudJojSNiYU";
string postData = "NQUser = DMS - QA & NQPassword = DMS - QA & saveLangPref = true & icharset = gb2312";
div1.InnerHtml = HttpHelper.GetHtml(url, postData, true);
}
void LoginByClient()
{
string url = "http://172.21.140.125:9704/analytics/saw.dll?Dashboard&_scid=VudJojSNiYU";
string postData = "NQUser = DMS - QA & NQPassword = DMS - QA & saveLangPref = true & icharset = gb2312";
div1.InnerHtml = HttpHelper.PostByClient(url, postData);
}
protected void Button1_Click(object sender, EventArgs e)
{
LoginByServer();
}
protected void Button2_Click(object sender, EventArgs e)
{
LoginByServer();
}
}
}