【发布时间】:2010-12-06 23:30:21
【问题描述】:
大家好,
在我的 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 返回成功或失败
【问题讨论】:
-
你的代码怎么不工作?
-
我的意思是,它到底是怎么失败的?有什么错误?它崩溃了吗?您在哪里看到问题,在服务器上还是在您的客户端代码中?
-
嘿,我发现了问题,因为我使用的是 HTTPS
-
嗨@shinto Joseph,我正在创建类似于你的应用程序,我正在使用 get 方法将用户名和密码传递给网络服务器。但我遇到了如何传递它的问题。你能帮我吗跨度>
-
我知道这是一个老问题,但我想知道你的 php 代码是什么。
标签: iphone-sdk-3.0 iphone