Php 使用NSURLConnection或ASIHTTPRequest发送POST数据

Php 使用NSURLConnection或ASIHTTPRequest发送POST数据,php,iphone,post,nsurlconnection,asihttprequest,Php,Iphone,Post,Nsurlconnection,Asihttprequest,我曾尝试使用asynch和synch NSURLConnection以及ASIHTTPRequest将POST数据发送到php文件,但两者都拒绝在$\u POST中实际回显任何内容。我的s.php文件如下所示: <?php echo 'Hi There'; echo $_POST['fname']; foreach ($_POST as $i => $value) { echo($i); } ?> 它只在返回到requestFinished时打印Hi-There行

我曾尝试使用asynch和synch NSURLConnection以及ASIHTTPRequest将POST数据发送到php文件,但两者都拒绝在$\u POST中实际回显任何内容。我的s.php文件如下所示:

<?php
echo 'Hi There';
echo $_POST['fname'];
foreach ($_POST as $i => $value) {
     echo($i);
}
?>
它只在返回到requestFinished时打印Hi-There行

编辑: 通过使用ASIFormDataRequest,我非常确定请求已经设置为POST,至少似乎没有手动设置的方法。当我尝试NSURLConnection时,我使用了:

NSURL *url = [NSURL URLWithString:@"http://server.com/s.php"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[[NSString stringWithFormat:@"fname=carl"] dataUsingEncoding:NSUTF8StringEncoding]];
NSURLResponse *response;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString *stringResponse = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding]; 
NSLog(@"%@",stringResponse);

当我使用html表单时,仍然只能得到Hi-There行,也可以得到正常的回复。

我认为在发布数据之前,您还必须设置一些http头,这样只有php才能识别使用post方法请求的页面

试试这条线

[请求设置HttpMethod:@“POST”]

试试看

    NSURL *url = [[NSURL alloc] initWithString:@"http://server.com/s.php"];
   ASIHTTPRequest *Asi_request = [ASIHTTPRequest requestWithURL:url];
    [request setPostValue:@"Ben" forKey:@"fname"];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestFinished:)];
    [request startAsynchronous];
[request setHTTPMethod: @"POST"];
//如果这不起作用,则应在AppleLegate文件中生成队列请参阅:


希望这对您有所帮助。

在代码中添加这一行;[请求设置HttpMethod:@“POST”];用户768531工作正常。上述代码中没有问题。可能不是代码问题。@impossiblepriya可能是服务器端的问题?php文件在哪里?@impossiblepriya是的,我在上面发布的php文件位于该地址。ASIHTTP设置为自动发布,我尝试了一个NSURLConnection,方法设置为POST,结果与上面问题的编辑中所示的结果相同。我认为服务器端有问题?因为HttpRequest没有响应“setHTTPMethod”,我很确定它会自动将其设置为POST[Asi_request setRequestMethod:@“POST”];这里Asi_请求是ASIHTTPRequest的对象。我正在用这个。
    NSURL *url = [[NSURL alloc] initWithString:@"http://server.com/s.php"];
   ASIHTTPRequest *Asi_request = [ASIHTTPRequest requestWithURL:url];
    [request setPostValue:@"Ben" forKey:@"fname"];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestFinished:)];
    [request startAsynchronous];
[request setHTTPMethod: @"POST"];