【问题标题】:Error occurs when send http post request发送http post请求时出错
【发布时间】:2013-12-08 20:03:07
【问题描述】:

我使用此方法向我的服务器发送 post 请求并将数据保存到 sql 数据库 但是我有一个问题,当数据保存到数据库时我错过了一些字符 例如我有这个字符串“example post ++ request ++” 当接收到数据库字符串时,通过缺少 + 加号更改“示例发布请求” 这是我的代码

xcode

NSString *STR = @"mystring=example post ++ request ++";
NSData *PostData = [STR dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://MySiteUrl.com/example.php"]];

    [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:PostData];
    [request setHTTPMethod:@"POST"];

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

php代码

$STR = $_POST['mystring'];

          header ('Content-Type: text/html; charset=UTF-8'); 
$con    = mysql_connect("localhost","xxxxxx","xxxx");
          mysql_select_db("xxxxxxx", $con);
          mysql_query("SET NAMES utf8");

$STR = trim($STR);

 mysql_query("INSERT INTO `xxxxxxx`.`table` (`id`, `mystring`) VALUES (NULL, '$STR');");

【问题讨论】:

标签: php post nsurlconnection httprequest nsurlrequest


【解决方案1】:

请记住,在 URL 中,+ 表示空格。如果你想传递一个 + 符号,那么你必须对其进行编码,我认为你的 xcode 是通过 UTF8 格式进行的。在 POST 中,请求参数被视为请求正文中的查询字符串。因此,调试您的 PHP 代码并确保一旦您的 PHP 接收到该数据,然后仍将其保存为 UTF8。如果不是,那么只需使用 php 中的 urlencode ( string $str ) 来转换它。我认为您的问题在于编码,您需要调试代码以准确识别哪个部分没有正确编码。好问题,祝你好运

【讨论】:

    猜你喜欢
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多