iPhone发出POST请求,处理cookie

编程入门 行业动态 更新时间:2024-10-25 22:32:02
本文介绍了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:@www.mywebserver/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:@www.mywebserver/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:@"www.mywebserver/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:@"www.mywebserver/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

本文发布于:2023-10-10 01:31:43,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:iPhone   POST   cookie

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!