2012-01-18 60 views
0

我有下面的代码运行良好,但似乎没有发布任何东西,或者至少我的PHP没有拿起它。我究竟做错了什么?HTTP发布在objective-c不发布任何东西

这里是我的PHP:

$usr = $_POST['username']; 
$psw = $_POST['password']; 
if ($usr == '1' && $psw == '1') { 
    echo 'Yes'; 
} 

这是我的目标c

NSString *post =[NSString stringWithFormat:@"username=%@&password=%@",usernameField.text, passwordField.text]; 

NSString *hostStr = @"http://ep.samico.dk/blogapp/login.php?"; 
hostStr = [hostStr stringByAppendingString:post]; 
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];  
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding]; 
if([serverOutput isEqualToString:@"Yes"]){ 
    UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"success" message:@"You are authorized" 
                  delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; 
    [alertsuccess show]; 

} else { 
    UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Fail" message:@"Invalid Access" 
                  delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; 
    [alertsuccess show]; 

} 
+0

变化的响应只是'的print_r($ _ POST);',看看它返回。 – 2012-01-18 17:37:13

+3

我猜你实际上正在格式化GET请求而不是发布。 – 2012-01-18 17:39:32

+0

或'var_dump($ _ POST);'tomato - tomahto。 – rdlowrey 2012-01-18 17:41:14

回答

0

去关什么在评论中说:

下段(代码)生成参数添加到结尾的URL字符串。

NSString *post =[NSString stringWithFormat:@"username=%@&password=%@",usernameField.text, passwordField.text]; 

NSString *hostStr = @"http://ep.samico.dk/blogapp/login.php?"; 
hostStr = [hostStr stringByAppendingString:post]; 

您可以修改这实际上做一个POST 你可以修改你的PHP代码来处理它们作为一个收到信息代替。

$usr = $_GET['username']; 
$psw = $_GET['password']; 
if ($usr == '1' && $psw == '1') { 
    echo 'Yes'; 
} 
0

要做到在objc一个POST,你需要直接使用NSURLConnection,如下所示:

NSString *post = [NSString stringWithFormat:@"username=%@&password=%@", @"username", @"pass"]; 

NSString *hostStr = @"http://ep.samico.dk/blogapp/login.php?"; 
NSURL *hostURL = [NSURL URLWithString:hostStr]; 
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:hostURL]; 
[req setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]]; 

NSURLResponse *response = nil; 
NSError *error = nil; 
NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error]; 

NSString *contents = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];