ASIHttpRequest登录身份验证问题

时间:2013-08-06 07:52:24

标签: iphone ios web-services authentication 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

这是什么回应以及我如何使用它进行身份验证

在我的网络服务中,如果用户名和密码正确,则显示“成功”消息,否则显示失败消息...

3 个答案:

答案 0 :(得分:1)

如果您正在为新项目工作,那么您不应该使用ASIHTTP Framework。检查此链接中给出的说明http://allseeing-i.com/ASIHTTPRequest/它最后一次更新于2011年。

尝试在NSLog中仅显示“strResponse”。我认为它应该正确显示回应。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

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

这似乎是网页的网址,而不是网络服务。如果您在该URL的登录页面上查看登录表单,它是否有两个凭据字段,并且他们使用名称或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编码的表单数据格式化您的身份验证请求。