0

我正在开发一个应用程序,我需要使用 php Web 服务将一些长文本保存到 mysql 数据库。我已经创建了 Web 服务,但我无法将参数从 iphone 发送到 php Web 服务。以下是我的网络服务代码:

<?php
require_once('config.php');
$username = $_POST["username"];
$aboutme = $_POST["aboutme"];

$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Connection to database failed: ' . mysql_error());
mysql_select_db(DB_DATABASE) or die ('select_db failed!');
$query = "insert into Member(username, aboutMe) values(.'".$username."', '".$aboutme."')";
echo $query;
$result = mysql_query($query) or die(mysql_error());
echo $result;

mysql_close($con);

?>

我找到了将数据发布到 Web 服务的代码,但我无法将数据添加到发布请求。

status=[NSString stringWithFormat:@"username=%@&password=%@&interestedIn=%@&aboutme=%@&profilePic=%@&isFBLogin=%@&useFacebookPic=%@",[@"aaaaaaaaaa" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],[@ "bbbbbb" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],[@"female" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],[@"about meeeeeeeee" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],[@"profile.png" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],[@"0" stringByAddingPercentEncoding],[@"0" stringByAddingPercentEncoding ,[@"0" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; //状态=@“用户名=pankaj1234&

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

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.domain.com/registerUser.php"]];
[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];

NSURLResponse *response;
NSError *error;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"success!");

我将如何将这些值添加到请求中?有人可以帮帮我吗?

谢谢潘卡伊
_

4

1 回答 1

1

您正在发布 www 表单,但您没有正确编码数据。

表单具有以下语法:key=value&key=value其中键/值都是 url 编码的。

您的编码字符串(“状态”)应编码如下:

status=[NSString stringWithFormat:@"username=%@&aboutme=%@",[username stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],[about stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];

然后,您可以使用其余代码将其转换为 NSData 并发布到您的网络服务。

于 2012-03-03T18:59:31.550 回答