C#实现12306自动登录的方法

 更新时间:2020年6月25日 11:27  点击:2124

依然使用IE9的捕获参数,做了一个12306的登录功能。参照了网上童鞋们的做法。
其他都和前面几篇读取余票、票价一样,不过登录要用到证书的问题,这个参考了一个网上的例子。
不过12306会随时变化,下面的登录不一定一直都能成功。如果12306有变化,大家可以根据变化对代码做修改。总之使用的方法不变,就是捕获参数和url,然后自己补充参数。
效果如下:

项目名称:Test12306AutoLogin;
环境:.net 4.0,Visual studio 2010;
项目图:

核心代码如下,
信任证书代码:

 public class Messenger
  {
    public Messenger()
    {
    }

    public void Register(string message, Action callback)
    {
      this.Register(message, callback, null);
    }

    public void Register<T>(string message, Action<T> callback)
    {
      this.Register(message, callback, typeof(T));
    }


    void Register(string message, Delegate callback, Type parameterType)
    {
      if (String.IsNullOrEmpty(message))
        throw new ArgumentException("'message' cannot be null or empty.");

      if (callback == null)
        throw new ArgumentNullException("callback");

      this.VerifyParameterType(message, parameterType);

      _messageToActionsMap.AddAction(message, callback.Target, callback.Method, parameterType);
    }

    [Conditional("DEBUG")]
    void VerifyParameterType(string message, Type parameterType)
    {
      Type previouslyRegisteredParameterType = null;
      if (_messageToActionsMap.TryGetParameterType(message, out previouslyRegisteredParameterType))
      {
        if (previouslyRegisteredParameterType != null && parameterType != null)
        {
          if (!previouslyRegisteredParameterType.Equals(parameterType))
            throw new InvalidOperationException(string.Format(
              "The registered action's parameter type is inconsistent with the previously registered actions for message '{0}'.\nExpected: {1}\nAdding: {2}",
              message, 
              previouslyRegisteredParameterType.FullName,
              parameterType.FullName));
        }
        else
        {
          // One, or both, of previouslyRegisteredParameterType or callbackParameterType are null.
          if (previouslyRegisteredParameterType != parameterType)  // not both null?
          {
            throw new TargetParameterCountException(string.Format(
              "The registered action has a number of parameters inconsistent with the previously registered actions for message \"{0}\".\nExpected: {1}\nAdding: {2}",
              message,
              previouslyRegisteredParameterType == null ? 0 : 1,
              parameterType == null ? 0 : 1));
          }
        }
      }
    }

    public void NotifyColleagues(string message, object parameter)
    {
      if (String.IsNullOrEmpty(message))
        throw new ArgumentException("'message' cannot be null or empty.");


      Type registeredParameterType;
      if (_messageToActionsMap.TryGetParameterType(message, out registeredParameterType))
      {
        if (registeredParameterType == null)
          throw new TargetParameterCountException(string.Format("Cannot pass a parameter with message '{0}'. Registered action(s) expect no parameter.", message));
      }


      var actions = _messageToActionsMap.GetActions(message);
      if (actions != null)
        actions.ForEach(action => action.DynamicInvoke(parameter));
    }


    public void NotifyColleagues(string message)
    {
      if (String.IsNullOrEmpty(message))
        throw new ArgumentException("'message' cannot be null or empty.");


      Type registeredParameterType;
      if (_messageToActionsMap.TryGetParameterType(message, out registeredParameterType))
      {
        if (registeredParameterType != null)
          throw new TargetParameterCountException(string.Format("Must pass a parameter of type {0} with this message. Registered action(s) expect it.", registeredParameterType.FullName));
      }


      var actions = _messageToActionsMap.GetActions(message);
      if (actions != null)
        actions.ForEach(action => action.DynamicInvoke());
    }
   
    private class MessageToActionsMap
    {
      internal MessageToActionsMap()
      {
      }


      
      internal void AddAction(string message, object target, MethodInfo method, Type actionType)
      {
        if (message == null)
          throw new ArgumentNullException("message");


        if (method == null)
          throw new ArgumentNullException("method");


        lock (_map)
        {
          if (!_map.ContainsKey(message))
            _map[message] = new List<WeakAction>();


          _map[message].Add(new WeakAction(target, method, actionType));
        }
      }


      internal List<Delegate> GetActions(string message)
      {
        if (message == null)
          throw new ArgumentNullException("message");


        List<Delegate> actions;
        lock (_map)
        {
          if (!_map.ContainsKey(message))
            return null;


          List<WeakAction> weakActions = _map[message];
          actions = new List<Delegate>(weakActions.Count);
          for (int i = weakActions.Count - 1; i > -1; --i)
          {
            WeakAction weakAction = weakActions[i];
            if (weakAction == null)
              continue;


            Delegate action = weakAction.CreateAction();
            if (action != null)
            {
              actions.Add(action);
            }
            else
            {
              // The target object is dead, so get rid of the weak action.
              weakActions.Remove(weakAction);
            }
          }


          // Delete the list from the map if it is now empty.
          if (weakActions.Count == 0)
            _map.Remove(message);
        }


        // Reverse the list to ensure the callbacks are invoked in the order they were registered.
        actions.Reverse();


        return actions;
      }


      internal bool TryGetParameterType(string message, out Type parameterType)
      {
        if (message == null)
          throw new ArgumentNullException("message");


        parameterType = null;
        List<WeakAction> weakActions;
        lock (_map)
        {
          if (!_map.TryGetValue(message, out weakActions) || weakActions.Count == 0)
            return false;
        }
        parameterType = weakActions[0].ParameterType;
        return true;
      }


      readonly Dictionary<string, List<WeakAction>> _map = new Dictionary<string, List<WeakAction>>();
    }


   
    private class WeakAction
    {
      
      internal WeakAction(object target, MethodInfo method, Type parameterType)
      {
        if (target == null)
        {
          _targetRef = null;
        }
        else
        {
          _targetRef = new WeakReference(target);
        }


        _method = method;


        this.ParameterType = parameterType;


        if (parameterType == null)
        {
          _delegateType = typeof(Action);
        }
        else
        {
          _delegateType = typeof(Action<>).MakeGenericType(parameterType);
        }
      }


      internal Delegate CreateAction()
      {
        // Rehydrate into a real Action object, so that the method can be invoked.
        if (_targetRef == null)
        {
          return Delegate.CreateDelegate(_delegateType, _method);
        }
        else
        {
          try
          {
            object target = _targetRef.Target;
            if (target != null)
              return Delegate.CreateDelegate(_delegateType, target, _method);
          }
          catch
          {
          }
        }


        return null;
      }


      internal readonly Type ParameterType;
      readonly Type _delegateType;
      readonly MethodInfo _method;
      readonly WeakReference _targetRef;
    }


    readonly MessageToActionsMap _messageToActionsMap = new MessageToActionsMap();
  }

登录的所有方法类:

public class Login12306Manager
  {
    private static readonly Messenger s_messenger = new Messenger();

    public static Messenger SMessenger { get { return s_messenger; } }

    public const string APPEND_MESSAGE = "append_message";

    public static string afterLoginCookie;

    private static string beforLoginCookie;

    static Login12306Manager()
    {
      SetCertificatePolicy();
    }

    /// <summary>
    /// 登 录
    /// </summary>
    public static string Login(string userName,string password, string randomCode)
    {
      string resultHtml = string.Empty;

      try
      {
        string loginRand= DoGetLoginRand();

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create
          (@"https://dynamic.12306.cn/otsweb/loginAction.do?method=login");

        request.Accept = @"text/html, application/xhtml+xml, */*";

        request.UserAgent = @"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";

        request.Referer = @"https://dynamic.12306.cn/otsweb/loginAction.do?method=init";

        request.ContentType = @"application/x-www-form-urlencoded";

        request.Headers[HttpRequestHeader.Cookie] = beforLoginCookie;

        request.Method = "POST";

        byte[] buffer = new byte[0];
        string parameter =
@"loginRand={0}&refundLogin=N&refundFlag=Y&isClick=&form_tk=null&loginUser.user_name={1}&nameErrorFocus=&user.password={2}&passwordErrorFocus=&randCode={3}&randErrorFocus=&NDU0NzY4NA%3D%3D=Nzg4ZDAxMGNkYTZlMTRjZA%3D%3D&myversion=undefined";

        parameter = string.Format(parameter, loginRand, userName, password, randomCode);

        buffer = Encoding.UTF8.GetBytes(parameter);

        request.ContentLength = buffer.Length;
        using (Stream writer = request.GetRequestStream())
        {
          writer.Write(buffer, 0, buffer.Length);

          writer.Flush();
        }

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
          afterLoginCookie = response.GetResponseHeader("Set-cookie");


          using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
          {
            resultHtml = reader.ReadToEnd();


            resultHtml = ProcessLoginResult(resultHtml);
          }
        }
      }
      catch{ }

      return resultHtml;
    }

    /// <summary>
    /// 刷新验证码
    /// </summary>
    public static string RefreshCode()
    {
      string randImageUrl = string.Empty;

      try
      {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format(@"https://dynamic.12306.cn/otsweb/passCodeNewAction.do?module=login&rand=sjrand&{0}",

          new Random().Next(10000, 1000000)));

        request.Accept = @"image/png, image/svg+xml, image/*;q=0.8, */*;q=0.5";

        request.UserAgent = @"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";

        request.Referer = @"https://dynamic.12306.cn/otsweb/loginAction.do?method=init";
 
        request.Method = "GET";

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
          beforLoginCookie = response.GetResponseHeader("Set-cookie");

          beforLoginCookie = Regex.Replace(beforLoginCookie, "path(?:[^,]+),?", "", RegexOptions.IgnoreCase);
 
          using (Stream reader = response.GetResponseStream())
          {
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, new Random().Next(10000, 99999) + @"loginRandCode.JPEG");


            using (FileStream file = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
              reader.CopyTo(file);
            }

            randImageUrl = path;
          }
        }
      }
      catch { }

      return randImageUrl;
    }

    private static string DoGetLoginRand()
    {
      string loginRand=string.Empty;

      try
      {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://dynamic.12306.cn/otsweb/loginAction.do?method=loginAysnSuggest");

        request.Accept = @"application/json, text/javascript, */*";

        request.UserAgent = @"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
 
        request.Referer = @"https://dynamic.12306.cn/otsweb/loginAction.do?method=init";

        request.Headers[HttpRequestHeader.Cookie] = beforLoginCookie;

        request.Method = "POST";

        byte[] buffer = new byte[0];

        buffer = Encoding.UTF8.GetBytes(string.Empty);

        request.ContentLength = buffer.Length;

        using (Stream writer = request.GetRequestStream())
        {
          writer.Write(buffer, 0, buffer.Length);

          writer.Flush();
        }

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
          using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
          {
            string result = reader.ReadToEnd();

            var loginRandContent = JsonConvert.DeserializeObject<BeforLoginRnad>(result);

            loginRand = loginRandContent.loginRand;
          }
        }
      }
      catch {}

      return loginRand;
    }
    /// <summary>
    /// 处理登录结果
    /// </summary>
    /// <param name="html">登录后返回的html文本</param>
    private static string ProcessLoginResult(string html)
    {
      string m_msgPattern = "message[^\"]+\"(?'message'[^\"]+)\";";

      string m_isLoginPatter = "isLogin\\s*=\\s*(?<val>.+)\n";

      string m_loginUserNamePattern = "u_name\\s*=\\s*['\"](?<name>.+)['\"]";

      if (html.Contains("请输入正确的验证码"))
      {
        return "验证码错误";
      }
      else if (html.Contains("当前访问用户过多"))
      {
        return "当前访问用户过多,请稍后再试...";
      }
      else
      {
        var match0 = Regex.Match(html, m_msgPattern, RegexOptions.Compiled);

        if (match0.Success)
        {
          string text = match0.Groups["message"].Value;

          if (text.Contains("密码") || text.Contains("登录名不存在"))
          {
            return "用户名或者密码错误";
          }
          else
          {
           return text;
          }
        }

        var match = Regex.Match(html, m_isLoginPatter, RegexOptions.Compiled);

        if (match.Success && (match.Groups["val"].Value.Trim().ToLower() == "true"))
        {
          match = Regex.Match(html, m_loginUserNamePattern, RegexOptions.Compiled);
          if (match.Success)
          {
            string name = match.Groups["name"].Value;


            return "登录成功:" + name;
          }
          else
          {
            return "登录失败,未知错误";
          }
        }
        else
        {
          return "登录失败!!!";
        }
      }
    }

    /// <summary>
    /// Sets the cert policy.
    /// </summary>
    private static void SetCertificatePolicy()
    {
      ServicePointManager.ServerCertificateValidationCallback
            += RemoteCertificateValidate;
    }


    /// <summary>
    /// Remotes the certificate validate.
    /// </summary>
    private static bool RemoteCertificateValidate(
      object sender, X509Certificate cert,
      X509Chain chain, SslPolicyErrors error)
    {
      SMessenger.NotifyColleagues(APPEND_MESSAGE, "信任任何证书...");
      return true;
    }
  }

  public class BeforLoginRnad
  {
    public string loginRand { get; set; }


    public string randError { get; set; }
  }

注意登录时,主要的正文是:

               string parameter =
@"loginRand={0}&refundLogin=N&refundFlag=Y&isClick=&form_tk=null&loginUser.user_name={1}&nameErrorFocus=&user.password={2}&passwordErrorFocus=&randCode={3}&randErrorFocus=&NDU0NzY4NA%3D%3D=Nzg4ZDAxMGNkYTZlMTRjZA%3D%3D&myversion=undefined",它有三个参数,登录时的随机码,用户名,密码和验证码组成。

调用如下:

前台wpf代码:

<Window x:Class="Test12306AutoLogin.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow">
  <StackPanel>
    <Grid>
      <Grid.Resources>
        <Style TargetType="TextBlock">
          <Setter Property="FontFamily" Value="Microsoft YaHei"/>
          <Setter Property="FontSize" Value="20"/>
          <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>


        <Style TargetType="TextBox">
          <Setter Property="FontSize" Value="20"/>
          <Setter Property="MinWidth" Value="300"/>
          <Setter Property="Height" Value="50"/>
          <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>


        <Style TargetType="PasswordBox">
          <Setter Property="FontSize" Value="20"/>
          <Setter Property="MinWidth" Value="300"/>
          <Setter Property="Height" Value="50"/>
          <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>
      </Grid.Resources>
      <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
      </Grid.RowDefinitions>


      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto"/>
        <ColumnDefinition Width="auto"/>
      </Grid.ColumnDefinitions>


      <TextBlock Grid.Row="0" Grid.Column="0" Text="用户名:"/>
      <TextBox Grid.Row="0" Grid.Column="1" x:Name="txtUserName"/>


      <TextBlock Grid.Row="1" Grid.Column="0" Text="密 码:"/>
      <PasswordBox Grid.Row="1" Grid.Column="1" x:Name="txtPassword"/>


      <TextBlock Grid.Row="3" Grid.Column="0" Text="验证码"/>
      <StackPanel Grid.Row="3" Grid.Column="1" Orientation="Horizontal"
            VerticalAlignment="Center">
        <TextBox x:Name="txtRandCode" Width="150"/>
        <Image x:Name="imageRandCode" Width="70"/>
        <Button Content="刷新验证码" Height="30" Width="80" Click="ButtonRefreshRandCode_Click" />
      </StackPanel>
    </Grid>


    <Button Content="登 录" Width="150" Height="50" Click="ButtonLogin_Click" />


    <RichTextBox x:Name="rtxResultContent" MinHeight="200"/>


  </StackPanel>
</Window>

后台代码:

public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();


      this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }


    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
      DoRefreshRandCode();
    }


    private void DoRefreshRandCode()
    {
      string imageRandUrl = Login12306Manager.RefreshCode();


      if (File.Exists(imageRandUrl))
      {
        ImageSource src = (ImageSource)(new ImageSourceConverter().ConvertFromString(imageRandUrl));


        this.imageRandCode.Source = src;
      }


      this.txtRandCode.Text = string.Empty;
    }


    /// <summary>
    /// 登录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void ButtonLogin_Click(object sender, RoutedEventArgs e)
    {
      string userName = this.txtUserName.Text;


      string password = this.txtPassword.Password;


      string randCode = this.txtRandCode.Text;


      if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(randCode))
      {
        MessageBox.Show("请填写完整信息");


        return;
      }

      string html = Login12306Manager.Login(userName, password, randCode);


      System.Windows.Documents.FlowDocument doc = this.rtxResultContent.Document;


      doc.Blocks.Clear();


      this.rtxResultContent.AppendText(html); 
    }


    /// <summary>
    /// 刷新验证码
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void ButtonRefreshRandCode_Click(object sender, RoutedEventArgs e)
    {
      DoRefreshRandCode();
    }
  }

以上就是本文的全部内容,希望对大家的学习有所帮助。

[!--infotagslink--]

相关文章

  • C#实现简单的登录界面

    我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
  • 浅谈C# 字段和属性

    这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
  • C#中截取字符串的的基本方法详解

    这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
  • C#实现简单的Http请求实例

    这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • C#中new的几种用法详解

    本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
  • 使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)

    这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • C#和JavaScript实现交互的方法

    最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • 轻松学习C#的基础入门

    轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
  • C#变量命名规则小结

    本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
  • C#绘制曲线图的方法

    这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • c#自带缓存使用方法 c#移除清理缓存

    这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
  • c#中(&&,||)与(&,|)的区别详解

    这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • 经典实例讲解C#递归算法

    这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#中list用法实例

    这篇文章主要介绍了C#中list用法,结合实例形式分析了C#中list排序、运算、转换等常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25