【问题标题】:AFNetworking and submitting html form with user validated captchaAFNetworking 并使用用户验证的验证码提交 html 表单
【发布时间】:2013-05-29 05:06:19
【问题描述】:

我试图能够登录一个带有验证码的 html 表单的网站。我一直在尝试这样做的方式是获取登录表单的 html,向用户显示验证码,用户将其放在文本字段中,然后我尝试提交表单。

我总是得到的错误是无效的密钥代码,所以我猜问题是我在第一个实例中获取的验证码,对第二个无效...任何想法我可以如何做到这一点?

该网页是同人小说,我作为个人项目这样做,看看我是否能够导出我的收藏列表和关注。

我这样做是为了向用户显示验证码。

NSURL *url = [NSURL URLWithString:@"https://www.fanfiction.net"];
self.httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

[self.httpClient getPath:@"/login.php" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

    TFHpple * doc       = [[TFHpple alloc] initWithHTMLData:responseObject];
    NSArray * elements  = [doc searchWithXPathQuery:@"//img[@id='xcaptcha']"];

    TFHppleElement * element = [elements objectAtIndex:0];

    [self.captchaView setImageWithURL:[NSURL URLWithString:[element objectForKey:@"src"]]];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { }];

然后,当用户在文本字段中输入验证码并按下 UIButton 时,我会这样做

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            kFFNMail,       @"email",
                            kFFNPass,       @"password",
                            self.captchaField.text,     @"captcha",
                        nil];

NSURLRequest *postRequest = [self.httpClient multipartFormRequestWithMethod:@"POST"
                                                                  path:@"/login.php"
                                                            parameters:params
                                             constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { }];

/*
// I think this is the same as the one before in this case
NSMutableURLRequest *postRequest = [self.httpClient requestWithMethod:@"POST"
                                                        path:@"/login.php"
                                                  parameters:params2];
*/

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:postRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    TFHpple * doc       = [[TFHpple alloc] initWithHTMLData:responseObject];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { }];

[operation start];

如果有任何迹象,我可以在这样的 ruby​​ 脚本中使用它

require 'rubygems'
require 'mechanize'
require "highline/import"

a = Mechanize.new
a.get('https://www.fanfiction.net/login.php') do |page|

  images = page.search("#xcaptcha")
  a.get(images.first.attributes["src"]).save "captcha.jpg"

  # I read the saved image,and enter the captcha code
  captcha = ask "Input captcha: "

  # Submit the login form
  my_page = page.form_with(:action => '/login.php') do |f|
    f.email     = my_mail
    f.password  = my_pass
    f.captcha   = captcha
  end.click_button

  # already logged!
  a.get('https://www.fanfiction.net/alert/story.php') do |page|
    page.links.each do |link|
      text = link.text.strip
      next unless text.length > 0
      puts text
    end
  end
end

【问题讨论】:

    标签: objective-c session captcha afnetworking


    【解决方案1】:

    好吧,事实证明我一切正常,只是我忘记了表单中的一个额外参数,一个隐藏的输入,它具有验证码的 id。 我只需要在捕获验证码图像的同时捕获 id,然后将其作为额外参数以 POST 形式发送。

    希望这对任何人都有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-10
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多