• C#---HttpWebRequest+HttpWebResponse实现登录人人网


    1.Form做成这样:

    2.添加新建项HttpHelper.cs

    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    
    namespace XIN_001
    {
        class HttpHelper
        {
            private static string accept = "*/*";
            private static CookieContainer cc = new CookieContainer();
            private static string contentType = "application/x-www-form-urlencoded";
            private static Encoding encoding = Encoding.GetEncoding("utf-8");
            private static string userAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E)";
    
            public static string GetHtml(string url, CookieContainer cookieContainer, string referer, string postData, bool isPost)
            {
                HttpWebRequest request = null;
                HttpWebResponse response = null;
                try
                {
                    request = (HttpWebRequest)WebRequest.Create(url);
                    request.CookieContainer = cookieContainer;
                    request.ContentType = contentType;
                    if (!string.IsNullOrEmpty(referer))
                        request.Referer = referer;
                    else
                        request.Referer = url;
                    request.Accept = accept;
                    request.UserAgent = userAgent;
                    request.Method = isPost ? "POST" : "GET";
                    if (!string.IsNullOrEmpty(postData))
                    {
                        byte[] bytes = Encoding.Default.GetBytes(postData);
                        request.ContentLength = bytes.Length;
                        Stream requestStream = request.GetRequestStream();
                        requestStream.Write(bytes, 0, bytes.Length);
                        requestStream.Close();
                    }
                    response = (HttpWebResponse)request.GetResponse();
                    Stream responseStream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(responseStream, encoding);
                    string str = reader.ReadToEnd();
                    reader.Close();
                    responseStream.Close();
                    response.Close();
                    return str;
                }
                catch(Exception Ex)
                {
                    MessageBox.Show(Ex.Message.ToString());
                    throw Ex;
                }
            }
            public static string GetHtml(string url)
            {
                return GetHtml(url, cc, null, null, false);
            }
            public static string GetHtml(string url,string postData,bool isPost)
            {
                return GetHtml(url, cc, null, postData, isPost);
            }
            public static string GetHtml(string url, string referer)
            {
                return GetHtml(url, cc, referer, null, false);
            }
            public static void Delay()
            {
                Random ran = new Random();
                int ranKey = ran.Next(100, 900);
                Thread.Sleep(ranKey);
            }
        }
    }

    3.Form代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using XIN_001;
    using System.IO;
    
    namespace LoginRenren
    {
        public partial class FrmLoginRenren : Form
        {
            public FrmLoginRenren()
            {
                InitializeComponent();
            }
            private static string html = "";
            private static string url = "http://www.renren.com/PLogin.do";
            private void btnLogin_Click(object sender, EventArgs e)
            {
                string postData = "email=" + txtUserName.Text + "&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&captcha_type=web_login&password=" + txtPwd.Text;
                html = HttpHelper.GetHtml(url, postData, true);
                LogFile("D:\LoginRenren.txt", html);
            }
            private bool LogFile(string LogFileName, string LogText)
            {
                if (File.Exists(LogFileName))
                {
                    StreamWriter LogStreamWriter = new StreamWriter(LogFileName, true, Encoding.Default);
                    LogStreamWriter.WriteLine(LogText);
                    LogStreamWriter.Close();
                    return true;
                }
                else
                {
                    try
                    {
                        FileStream LogFileStream = new FileStream(LogFileName, FileMode.CreateNew);
                        LogFileStream.Close();
                        StreamWriter LogStreamWriter = new StreamWriter(LogFileName, true, Encoding.Default);
                        LogStreamWriter.WriteLine(LogText);
                        LogStreamWriter.Close();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                        return false;
                    }
                }
            }
        }
    }

    如果成功登录人人网,本程序将在D盘根目录下生成一个LoginRenren.txt文件,里面是登录成功后的好友新鲜事页面的源文件。

    附登录人人网源代码【VS2010解决方案】:LoginRenren.rar

  • 相关阅读:
    算术运算符
    短路运算
    基本运算符
    类型转换
    数据类型讲解
    关键字
    河北省重大技术需求征集八稿第六天
    河北省重大技术需求征集八稿第五天
    河北省重大技术需求征集八稿第四天
    河北省重大技术需求征集八稿第三天
  • 原文地址:https://www.cnblogs.com/sunxin88/p/3493440.html
Copyright © 2020-2023  润新知