首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSMutableURLRequest正确发布

NSMutableURLRequest正确发布
EN

Stack Overflow用户
提问于 2010-09-09 00:48:46
回答 1查看 2.1K关注 0票数 1

因此,我尝试使用NSMutableURLRequest登录我的网站,该via通过帖子提供凭据。作为一个noobie,我有一些关于这个方法的功能的问题,以确保我理解它。

代码语言:javascript
复制
NSString *post = [NSString stringWithFormat:@"username=%@&password=%@&TARGET=%@",LoginId,LoginPwd,target];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setHTTPShouldHandleCookies:YES];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://somelogin.mycomp.verify.fcc"]]];
//[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];

NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

现在,这将自动处理任何通过委托方法的重定向,对吗?它还会在一路上收集任何饼干吗?另外,我是否应该使用异步请求呢?谢谢!

更新:所以我推断一些cookie被请求和后续重定向忽略了。有人知道为什么会发生这种情况吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-09-09 01:30:33

您可以在同步登录时执行以下操作...代码并不完美,但这是大体思路。

请在此处查找文档

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/AuthenticationChallenges.html#//apple_ref/doc/uid/TP40009507-SW1

代码语言:javascript
复制
// credentials set here
NSURLCredential *creds = [NSURLCredential credentialWithUser:LoginId password:LoginPwd
                                           persistence:NSURLCredentialPersistenceForSession];

NSURLProtectionSpace *protectionedSpace = [[NSURLProtectionSpace alloc]
  initWithHost:@"mycomp.verify.fcc"
  port:8443
  protocol:@"https"
  realm:nil
  authenticationMethod:nil];


[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:creds
                                            forProtectionSpace:protectionedSpace];

// removed credentials from line...
NSString *post = [NSString stringWithFormat:@"TARGET=%@",target];

下面是一个异步完成此操作的教程

http://iosdevelopertips.com/networking/handling-url-authentication-challenges-accessing-password-protected-servers.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3670029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档