iPhone用NSURLConnection发送POST [英] iPhone sending POST with NSURLConnection

查看:92
本文介绍了iPhone用NSURLConnection发送POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用NSURLConnection将POST数据发送到PHP脚本时遇到了一些问题。这是我的代码:

I'm having some problems with sending POST data to a PHP script with NSURLConnection. This is my code:

    const char *bytes = [[NSString stringWithFormat:@"<?xml version=\"1.0\"?>\n<mydata>%@</mydata>", data] UTF8String];

    NSURL *url = [NSURL URLWithString:@"http://myurl.com/script.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[NSData dataWithBytes:bytes length:strlen(bytes)]];

    NSURLResponse *response;
    NSError *err;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    NSLog(@"responseData: %@", responseData);

我的script.php就像这样简单:

And my script.php is as simple as this:

<?php
    echo $_POST['mydata'];
?>

过去这对我有用,但出于某种原因,我从<$ c获得了输出$ c> NSLog(@responseData:%@,responseData); 现在只是<>而不是theData。

This have worked for me in the past, but for some reason the output I get from NSLog(@"responseData: %@", responseData); now is just "<>" instead of "theData".

某处可能有些蹩脚的错误,但我似乎无法找到它?有什么想法?

Probably some lame mistake somewhere but I can't seem to find it? Any ideas?

推荐答案

您的数据有误。如果您希望在PHP的$ _POST数组中找到数据,它应该如下所示:

Your data is wrong. If you expect the data to be found in the $_POST array of PHP, it should look like this:

const char *bytes = "mydata=Hello%20World";

如果要发送XML数据,则需要设置不同的HTTP内容类型。例如。你可以设置

If you want to send XML Data, you need to set a different HTTP Content Type. E.g. you might set

application/xml; charset=utf-8

如果未设置HTTP内容类型,则默认类型为

If you set no HTTP Content Type, the default type

application/x-www-form-urlencoded


将使用

。此类型要求POST数据具有与GET请求中相同的格式。

will be used. And this type expects the POST data to have the same format as it would have in a GET request.

但是,如果设置不同的HTTP内容类型,例如application / xml,然后这个数据不会添加到PHP中的$ _POST数组中。您必须从输入流中读取原始数据。

However, if you set a different HTTP Content Type, like application/xml, then this data is not added to the $_POST array in PHP. You will have to read the raw from the input stream.

试试这个:

NSString * str = [NSString stringWithFormat:@"<?xml version=\"1.0\"?>\n<mydata>%@</mydata>", data];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[str dataUsingEncoding:NSUTF8StringEncoding]];

并在服务器上尝试以下PHP:

and on the server try the following PHP:

$handle = fopen("php://input", "rb");
$http_raw_post_data = '';
while (!feof($handle)) {
    $http_raw_post_data .= fread($handle, 8192);
}
fclose($handle);

请注意,这仅适用于POST的HTTP标头不是application / x-www-窗体-urlencoded。如果它是application / x-www-form-urlencoded,则PHP本身会读取所有发布数据,对其进行解码(将其拆分为键/值对),最后将其添加到$ _POST数组中。

Please note that this only works if the HTTP header of your POST is not application/x-www-form-urlencoded. If it is application/x-www-form-urlencoded then PHP itself reads all the post data, decodes it (splitting it into key/value pairs), and finally adds it to the $_POST array.

这篇关于iPhone用NSURLConnection发送POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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