gpt4 book ai didi

iphone-sdk-3.0 - 通过 SSL HTTPS 从 iPhone 发送 POST 数据

转载 作者:行者123 更新时间:2023-12-03 18:16:42 25 4
gpt4 key购买 nike

大家好,

在我的 iPhone 项目中,我需要将用户名和密码传递到 Web 服务器,以前我使用 GET 方法传递数据并使用 GET 格式的 url(例如: localhost:8888/login?userName=admin&password=password )但现在我需要将其作为 POST 数据发送,

谁能帮我找出下面这段代码中的错误吗?

我尝试过的代码..

<小时/>
NSString *post =[NSString stringWithFormat:@"userName=%@&password=%@",userName.text,password.text];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"https://localhost:443/SSLLogin/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);

此页面将使用 php echo 返回成功或失败

最佳答案

我终于找到了一种通过安全连接从 iPhone 发送数据的方法:

NSString *post =[[NSString alloc] initWithFormat:@"userName=%@&password=%@",userName.text,password.text];
NSURL *url=[NSURL URLWithString:@"https://localhost:443/SSLLogin/Login.php"];

NSLog(post);
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

/* when we user https, we need to allow any HTTPS cerificates, so add the one line code,to tell teh NSURLRequest to accept any https certificate, i'm not sure about the security aspects
*/

[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"%@",data);

为避免出现警告消息,请添加

<小时/>
@interface NSURLRequest (DummyInterface)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
@end

礼貌:

  1. 致我所有的 Stack Over Flow friend 和支持者
  2. http://www.cocoadev.com/index.pl?HTTPFileUpload
  3. http://blog.timac.org/?tag=nsurlrequest

关于iphone-sdk-3.0 - 通过 SSL HTTPS 从 iPhone 发送 POST 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1571336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com