AFNetworking并使用用户验证的验证码提交html表单

时间:2013-05-28 00:19:57

标签: objective-c session captcha afnetworking

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

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

网页是Fanfiction,我这是作为个人项目进行的,看看我是否能够导出我的收藏列表并跟随。

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

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];

如果有任何迹象,我可以使用像这样的红宝石脚本

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

1 个答案:

答案 0 :(得分:2)

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

希望这有助于任何人。