我已經嘗試將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