大家好,我写了下面的代码来解决我的验证码问题,它对我来说非常有用。
从验证码图像中读取文本的代码
您将需要 ASIHTTPRequest API 在 Web 上发送 HTTP 请求并获取以下代码的响应。
将 ASIHTTPRequest API 的各个类导入您的类中,您将在其中执行从验证码图像中获取文本的过程。
现在使用下面的方法并调用来解决 catpcha 。
-(NSString *)CaptchaToText
{
// Captcha Image Url in NSData object
NSData *urlData=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://oi47.tinypic.com/357hyea.jpg"]];
// base64 image data
NSString *imageBase64Data = [ASIHTTPRequest base64forData:urlData ];
//NSString *imageBase64Data =[self base64EncodedString:urlData];
NSLog(@"imageBase64Data =%@\n\n",imageBase64Data);
// prefix - base64 image data
NSString *prefixBase64Data = [NSString stringWithFormat:@"base64:%@",imageBase64Data];
NSLog(@"prefixBase64Data Value = %@\n\n",prefixBase64Data);
ASIHTTPRequest *req=[ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://oi47.tinypic.com/357hyea.jpg"]];
[req startSynchronous];
NSError *err=[req error];
if(!err){
NSString *respo=[req responseString];
NSLog(@"respo= %@",respo);
NSURL *url = [NSURL URLWithString:@"http://api.dbcapi.me/api/captcha"];
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"YourDeathByCaptchaUserName" forKey:@"username"];
[request setPostValue:@"YourDeathByCaptchaPassword!@#$" forKey:@"password"];
[request setPostValue:prefixBase64Data forKey:@"captchafile"];
[request setRequestMethod:@"POST"];
[request startSynchronous];
NSError *error=[request error];
if(!error){
// Here we will get "Text" data from Captcha Image in "response"
NSString *response=[request responseString];
NSLog(@"response= %@",response);
}
else{
NSLog(@"error= %@",error);
}
}
else{
NSLog(@"err= %@",err);
}
return response;
}
就是这样。完毕