2011-06-16 53 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