2013-08-06 113 views
0

我试图与网站进行身份验证用户名和密码...ASIHttpRequest登录身份验证问题

我使用ASIHttpRequest这样的:

- (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 
          სავაჭრო კალათა ცარიელია. 

ჩემი ბიბლიოთეკა 
სურვილები 
რეგისტრაცია 

. 
. 
. 
. 
. 

和状态代码和状态消息是

的StatusCode:200
StatusMessage:HTTP/1.1 200 OK

这是什么反应,我该如何使用这个认证

在我的web服务,如果用户名和密码是正确的“成功”消息显示和其他故障信息显示...

回答

0
- (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)theRequest 
{ 
    NSString *statusMessage = [theRequest responseData]; 
    NSLog(@"StatusCode: %d", statusCode); 
    //at this methods you get authuntication 
    //write code here 
} 
+0

我得到了statusCode:200和statusMessage:Sattus代码:<3c21444f 43545950 45206874 6d6c2050 55424c49 4320222d 2f2f5733 432f2f44像这样 – Denny

+0

'topSecretFetchComplete'方法在你的认证成功时调用。所以你在这里写代码重定向到认证后出现的其他视图 –

+0

如果我输入了错误的密码,那么也调用topSecretFetchComplete?为什么? – Denny

1

如果您正在为新项目工作,那么你不应该使用ASIHTTP框架。检查这个链接中给出的指令http://allseeing-i.com/ASIHTTPRequest/这是最后更新于2011年。

只是试图在NSLog中只显示“strResponse”。我认为它应该正确显示响应。

0

HTTP 200响应代码是来自Web服务器的正常响应。如果您使用表单身份验证,就像您看到的那样,我会检查响应中设置的cookie,因为我希望会话cookie在成功登录时返回。 IIRC,ASIHTTPRequest将默认存储cookie,并自动发送后续请求。

可能都无所谓什么样的内容,你在响应收到只要设置cookie和其他错误条件不发生(如HTTP 401或其他错误),但这是高度依赖于你情况。您可能需要解析错误的响应,此时,服务器似乎在嗅探请求的代理字符串,并检测到您的客户端不支持JavaScript,或者期望类似以JSON方式提交的表单格式作为AJAX调用的一部分,并假定该脚本失败,说明JavaScript在浏览器中没有正常执行(它不是)。

您现在正在将名为usernamepasswd的字段提交到网址https://www.MySite/login/

这似乎是网页的网址,而不是网络服务。如果你在这个网址查看登录页面上的登录表单,它有凭据两个字段,以及他们所使用的名称或ID usernamepasswd,如:

<form action="https://www.MySite/login/" method="POST"> 
    <input name="username" type="text" /> 
    <input name="passwd" type="password" /> 
</form> 

或者:

<form action="https://www.MySite/login/" method="POST"> 
    <input id="username" type="text" /> 
    <input id="passwd" type="password" /> 
</form> 

否则,如果您使用的是通过SOAP或JSON进行通信的Web服务,则需要使用该格式格式化认证请求,而不是URL编码的表单数据。

+0

好吧,现在我正在使用网页登录页面,但如果我输入错误的密码,那么也csll topSecretFetchComplete为什么? – Denny

+0

@ Denny - 由于请求正确完成,服务器刚刚返回了一个表明密码不正确的响应。这本身并不是一个错误。如果发送/接收请求/响应时发生错误,ASIHTTPRequest框架只会调用失败方法,而不是在您的应用程序发出响应时指示登录失败。解决指示登录失败的错误代码/消息的响应由您决定。在这种情况下,最好将请求发送到一个Web服务,该Web服务将形成可靠地指示以预期格式登录失败的响应。 – nekno