iPhone发出POST请求,处理cookie [英] iPhone Make POST request, handle cookie

查看:154
本文介绍了iPhone发出POST请求,处理cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人能够阐明以下几点,我想我正在朝着正确的方向前进。我想用一个用户/密码组合登录到我的服务器,然后我需要能够告诉我是否正确登录(应该删除一个cookie),如果是这种情况,我会提出另一个请求。



任何帮助表示感谢,继承人我正在使用的代码:

  [[ NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; 

NSString * post = [NSString stringWithFormat:@name =%@&pass =%@,@foo,@bar];
NSData * postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString * postLength = [NSString stringWithFormat:@%d,[postData length]];

NSMutableURLRequest * request = [[[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@http://www.mywebserver.com/login.php]];
[request setHTTPMethod:@POST];
[request setValue:postLength forHTTPHeaderField:@Content-Length];
[request setValue:@application / x-www-form-urlencodedforHTTPHeaderField:@Content-Type];
[request setHTTPBody:postData];

NSError *错误;
NSURLResponse *响应;
NSData * urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:& response error:& error];
NSString * data = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(data);

//如何检查Cookie是否被删除?


//发出另一个请求..


解决方案

这应该可以工作:

  NSDictionary * headerFields = [(NSHTTPURLResponse *)response allHeaderFields]; 
NSURL * url = [NSURL URLWithString:@http://www.mywebserver.com/login.php];
NSArray * cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:headerFields forURL:url];

然后您就可以知道 cookies 数组包含你想要的cookie。



您也可以在收到回复后调用它:

  NSArray * cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url]; 


I'm hoping someone can shed some light on the following, I think I am heading in the right direction with this. I want to login to my server with a user/pass combo, then I need to be able to tell If I logged in correctly (a cookie should be dropped), then I will make another request if this is the case.

Any help appreciated, heres the code I am working with:

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];

NSString *post =[NSString stringWithFormat:@"name=%@&pass=%@",@"foo", @"bar"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://www.mywebserver.com/login.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(data);

// HOW to Check if there was a Cookie dropped??


// Make another request..

解决方案

This should work:

NSDictionary *headerFields = [(NSHTTPURLResponse*)response allHeaderFields]; 
NSURL *url = [NSURL URLWithString:@"http://www.mywebserver.com/login.php"];   
NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:headerFields forURL:url];

Then you can know if the cookies array contains the cookie you want.

You could also call this after receiving the response:

NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url];

这篇关于iPhone发出POST请求,处理cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆