Iphone ASIHttpRequest登录身份验证问题
我正在尝试验证网站的用户名和密码 我使用的AsiTRequest如下所示:Iphone ASIHttpRequest登录身份验证问题,iphone,ios,web-services,authentication,asihttprequest,Iphone,Ios,Web Services,Authentication,Asihttprequest,我正在尝试验证网站的用户名和密码 我使用的AsiTRequest如下所示: - (IBAction)fetchTopSecretInformation:(id)sender{ Ustr = User.text; //Ustr and Pstr are strings Pstr = Pass.text; NSURL *url1 = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.MySite/log
- (IBAction)fetchTopSecretInformation:(id)sender{
Ustr = User.text; //Ustr and Pstr are strings
Pstr = Pass.text;
NSURL *url1 = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.MySite/login/"]];
ASIFormDataRequest *request1 = [ASIFormDataRequest requestWithURL:url1];
[request1 setUseKeychainPersistence:YES];
request1.delegate = self;
request1.shouldPresentCredentialsBeforeChallenge = YES;
[request1 setRequestMethod:@"POST"];
[request1 setPostValue:User.text forKey:@"username"];
[request1 setPostValue:Pass.text forKey:@"passwd"];
[request1 setTimeOutSeconds:30];
NSLog(@"Useer :%@",User.text);
[request1 setDidFailSelector:@selector(topSecretFetchFailed:)];
[request1 setDidFinishSelector:@selector(topSecretFetchComplete:)];
[request1 startAsynchronous];
}
- (IBAction)topSecretFetchFailed:(ASIHTTPRequest *)theRequest{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error sending request to the server" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
- (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)theRequest{
int statusCode = [theRequest responseStatusCode];
NSString *statusMessage = [theRequest responseStatusMessage];
NSString *strResponse = [[theRequest responseString] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *strFinal = [self flattenHTML:strResponse];
NSLog(@"Response :%@",strFinal);
NSLog(@"StatusCode: %d", statusCode);
NSLog(@"StatusMessage: %@", statusMessage);
}
我在NSLog中得到如下响应:
jQuery(function($) { $(".main").addClass("show-bg"); });
JavaScript seems to be disabled in your browser.
You must have JavaScript enabled in your browser to utilize the functionality of this website.
This is a demo store. Any orders placed through this store will not be honored or fulfilled.
+995 91 190601
მოგესალმებით ელექტრონული წიგნების მაღაზიაში
READERwill.com
კალათა
კალათა GEL 0,00
სავაჭრო კალათა ცარიელია.
ჩემი ბიბლიოთეკა
სურვილები
რეგისტრაცია
.
.
.
.
.
以及状态代码和状态消息
状态代码:200StatusMessage:HTTP/1.1 200正常 这个响应是什么?我如何使用它进行身份验证
在我的Web服务中,如果用户名和密码正确,则显示“成功”消息,否则显示失败消息 如果您正在为一个新项目工作,那么您不应该使用ASIHTTP框架。检查此链接中给出的说明,最近一次更新是在2011年
- (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)theRequest
{
NSString *statusMessage = [theRequest responseData];
NSLog(@"StatusCode: %d", statusCode);
//at this methods you get authuntication
//write code here
}
只需尝试在NSLog中仅显示“strResponse”。我认为它应该正确地显示响应。HTTP 200响应代码是来自web服务器的正常响应。如果您使用的是forms身份验证,那么我会检查响应中设置的cookie,因为我希望在成功登录时返回会话cookie。默认情况下,IIRC、ASIHTTPRequest将存储cookie,并将其与后续请求一起自动发送 只要设置了cookie并且没有出现其他错误情况(如HTTP 401或其他错误),您在响应中接收到的内容可能无关紧要,但这在很大程度上取决于您的情况。您可能需要解析错误响应,此时,服务器可能正在嗅探您请求的代理字符串并检测到您的客户端不支持JavaScript,或者它希望在AJAX调用中以JSON格式提交表单,并且假设由于脚本失败,JavaScript不能在浏览器中正常执行(事实并非如此) 现在,您正在将名为
username
和passwd
的字段提交到URLhttps://www.MySite/login/
这似乎是网页的URL,而不是web服务。如果您在该URL的登录页面上查看登录表单,它是否有两个凭据字段,并且它们使用名称或IDusername
和passwd
,例如:
<form action="https://www.MySite/login/" method="POST">
<input name="username" type="text" />
<input name="passwd" type="password" />
</form>
或:
否则,如果您使用的是通过SOAP或JSON进行通信的web服务,则需要将身份验证请求格式化为该格式,而不是URL编码的表单数据。当身份验证成功时,我得到了statusCode:200和statusMessage:Sattus Code:'topSecretFetchComplete'方法调用。因此,您在此处编写代码以重定向到其他视图,该视图将在身份验证后显示OK现在我直接使用web登录页,但如果我输入了错误的密码,那么还需要csll topSecretFetchComplete why???@Denny-由于请求正确完成,服务器返回的响应表明密码不正确。这本身不是一个错误。
ASIHTTPRequest
framework仅在发送/接收请求/响应时出错时调用failure方法,而不是在应用程序的响应指示登录失败时调用。您可以自行分析指示登录失败的错误代码/消息的响应。在这里,最好将您的请求发送到一个web服务,该服务将生成一个响应,可靠地以预期的格式指示登录失败。
<form action="https://www.MySite/login/" method="POST">
<input id="username" type="text" />
<input id="passwd" type="password" />
</form>