我已经尝试将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表单。
在您的代码中添加此行; [request setHTTPMethod:@“POST”]; – 2011-06-16 06:58:01
其在上面的代码中工作正常user768531.no问题。可能不是代码问题。 – Priyanka 2011-06-16 10:02:04
@impossiblepriya可能是服务器端的东西? – apchait 2011-06-16 18:32:51