通过SSL HTTPS从iphone发送POST数据 [英] Sending POST data from iphone over SSL HTTPS

查看:124
本文介绍了通过SSL HTTPS从iphone发送POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hai all,

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

In my iphone project i need to pass the user name and password to a web server,previously i pass data using GET method and used the url with GET format (eg: localhost:8888/login?userName=admin&password=password ) but now i need to sent this as a POST data,

可以任何一个帮我看下面这段代码有什么问题?

can any one help me to find what wrong in this code below ?

我试过的代码..

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失败

this page will return success or failed using php echo

推荐答案

您正在发送请求为NSASCIIStringEncoding但寻找NSUTF8StringEncoding

You are sending the request as NSASCIIStringEncoding but looking for NSUTF8StringEncoding

我设置


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


[request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];

然而,正如Nikolai所说 - 如果我们知道错误是什么会更容易: - )

however, as Nikolai noted - this would be easier if we knew what the error was :-)

这篇关于通过SSL HTTPS从iphone发送POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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