admin 管理员组

文章数量: 1184232



Http Analyzer  下载1   下载2


登录与在浏览器中查看

/// <summary>
/// 发送登录信息,进行登录
/// </summary>
private void Login_Click()
{
    // 设置登录信息
    string url = "http://10.80.3.212/zentao/user-login.html";
    string postData = "account=test&password=123456&referer=http://10.80.3.212/zentao/my/";

    // 发送post信息,进行登录
    CookieCollection cookie;
    string html = HttpTool.GetHtml(url, postData, Method.POST, out cookie);

    if (cookie.Count > 0)
    {
        MessageBox.Show("您已成功登录!");
    }
}

/// <summary>
/// 获取其他页面数据信息(登录成功后,或不需要登录)
/// </summary>
private void getURL(string url)
{
    string data = HttpTool.GetHtml(url);   // 实时获取网页数据
    MessageBox.Show(data);
}

/// <summary>
/// 登录成功后,在浏览器中打开url
/// </summary>
private void openURl(string url)
{
    HttpTool.openUrl(url);
    //System.Diagnostics.Process.Start(url); //直接可以访问的网页打开方式
}

发送POST信息与网页数据的获取

public enum Method
{
    POST = 0,
    GET = 1
}

/// <summary>
/// 用于发送http请求,访问WEB页面
/// </summary>
public class HttpTool   //此类参考:http://blog.csdn/htsnoopy/article/details/7094224
{
    #region 设置信息

    public static CookieContainer cookie = new CookieContainer();           // 用于记录访问网页时cookie数据
    public static CookieCollection cookieCollection;

    private static string ContentType = "application/x-www-form-urlencoded";
    private static string Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    private static string UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14";
        
    public static void setting(string contentType, string accept, string userAgent)
    {
        ContentType = contentType;
        Accept = accept;
        UserAgent = userAgent;
    }

    public static void setting(CookieContainer cc, string contentType, string accept, string userAgent)
    {
        cookie = cc;
        ContentType = contentType;
        Accept = accept;
        UserAgent = userAgent;
    }

    /// <summary>
    /// 清空cookie数据
    /// </summary>
    public static void clearCookie()
    {
        cookie = new CookieContainer();
    }

    #endregion


    #region 网页数据获取的获取

    /// <summary>
    /// 获取指定的网页数据(不需要登录,可以直接访问的页面;或已登录)
    /// </summary>
    public static string GetHtml(string url)
    {
        return GetHtml(url, out cookieCollection);
    }

    /// <summary>
    /// 获取指定的网页数据(不需要登录,可以直接访问的页面)
    /// </summary>
    public static string GetHtml(string url, out CookieCollection cookieCollection)
    {
        try
        {
            HttpWebRequest httpWebRequest;

            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            httpWebRequest.CookieContainer = cookie;

            HttpWebResponse httpWebResponse;
            httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            Stream responseStream = httpWebResponse.GetResponseStream();
            StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
            string html = streamReader.ReadToEnd();
            streamReader.Close();
            responseStream.Close();

            cookieCollection = cookie.GetCookies(new Uri(url));

            return html;
        }
        catch (Exception)
        {
            cookieCollection = null;
            return null;
        }
    }
    #endregion


    #region 需要验证帐号信息的网页,数据获取

    /// <summary>
    /// post数据到指定的网址,获取cookie数据,和返回页
    /// </summary>
    public static string GetHtml(string url, string postData, Method method)
    {
        return GetHtml(url, postData, method, out cookieCollection);
    }

    /// <summary>
    /// post数据到指定的网址,获取cookie数据,和返回页
    /// </summary>
    public static string GetHtml(string url, string postData, Method method, out CookieCollection cookieCollection)
    {
        try
        {
            if (string.IsNullOrEmpty(postData))
            {
                return GetHtml(url, out cookieCollection);
            }

            byte[] byteRequest = Encoding.Default.GetBytes(postData);

            HttpWebRequest httpWebRequest;
            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);

            httpWebRequest.CookieContainer = cookie;
            httpWebRequest.ContentType = ContentType;

            httpWebRequest.Referer = url;
            httpWebRequest.Accept = Accept;
            httpWebRequest.UserAgent = UserAgent;
            httpWebRequest.Method = method == Method.POST ? "POST" : "GET";
            httpWebRequest.ContentLength = byteRequest.Length;

            Stream stream = httpWebRequest.GetRequestStream();
            stream.Write(byteRequest, 0, byteRequest.Length);
            stream.Close();

            HttpWebResponse httpWebResponse;
            httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            Stream responseStream = httpWebResponse.GetResponseStream();
            StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
            string html = streamReader.ReadToEnd();
            streamReader.Close();
            responseStream.Close();

            cookieCollection = cookie.GetCookies(new Uri(url));

            return html;
        }
        catch (Exception)
        {
            cookieCollection = null;
            return null;
        }
    }

    /// <summary>
    /// 登录成功后,在浏览器中打开指定的url
    /// </summary>
    public static void openUrl(string url)
    {
        openUrl(cookieCollection, url);
    }

    public static void openUrl(CookieCollection cookieCollection, string url)
    {
        string tmp = "";
        for (int i = 0; i < cookieCollection.Count; i++)
        {
            Cookie c = cookieCollection[i];
            tmp += (tmp.Equals("") ? "" : "&") + c.Name.ToString() + "=" + c.Value.ToString();
        }

        Process.Start(url + "?" + tmp);
    }

    #endregion

}

html数据的解析: http://blog.csdn/scimence/article/details/50274317




本文标签: 打开网页 器中 网页