2011-06-16 106 views
0

我已经尝试将POST数据发送到具有异步和同步NSURLConnection和ASIHTTPRequest的php文件,但都拒绝实际回显$ _POST中的任何内容。我s.php文件看起来像这样:使用NSURLConnection或ASIHTTPRequest发送POST数据

<?php 
echo 'Hi There'; 
echo $_POST['fname']; 
foreach ($_POST as $i => $value) { 
    echo($i); 
} 
?> 

我ASIHTTRequest是:

NSURL *url = [[NSURL alloc] initWithString:@"http://server.com/s.php"]; 
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
[request setPostValue:@"Ben" forKey:@"fname"]; 
[request setDelegate:self]; 
[request setDidFinishSelector:@selector(requestFinished:)]; 
[request startAsynchronous]; 

其中仅打印您好线,当谈到回requestFinished。

编辑: 通过使用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表单。

+0

在您的代码中添加此行; [request setHTTPMethod:@“POST”]; – 2011-06-16 06:58:01

+0

其在上面的代码中工作正常user768531.no问题。可能不是代码问题。 – Priyanka 2011-06-16 10:02:04

+0

@impossiblepriya可能是服务器端的东西? – apchait 2011-06-16 18:32:51

回答

0

我认为你必须发布数据之前还设置一些HTTP头,那么只有PHP能够识别出网页使用POST方法

尝试用这条线

[请求setHTTPMethod要求:@“POST” ]。

+0

ASIHTTP设置为自动发布,我尝试了设置为POST的方法的NSURLConnection并得到了与显示在上述问题的编辑中。我在想服务器端有什么问题? – apchait 2011-06-16 18:54:05

0

试试吧。

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"]; 

//如果这不会工作,那么你应该在appledelegate文件中看到生成的队列:link

这可能帮助你。

+0

ASIHTTPRequest没有响应'setHTTPMethod',我很确定它将它设置为POST自动 – apchait 2011-06-16 18:55:34

+0

[Asi_request setRequestMethod:@“POST”];这里Asi_request是ASIHTTPRequest的对象。我正在使用这个。 – GameLoading 2011-06-17 04:32:47