2011-11-24 174 views
0

我從服務器獲取數據時遇到問題。服務器是我的本地主機。從服務器獲取數據objective-c

SMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"localhost"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0]; 

[request setHTTPMethod:@"GET"]; 
[request setHTTPBody:[NSString stringWithFormat:@"test.php"]]; 

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
if (connection) 
{ 
    NSLog(@"NSURLConnection connection==true"); 
    NSURLResponse *response; 
    NSError *err; 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; 
    NSLog(@"responseData: %@", responseData); 
} 
else 
{ 
    NSLog(@"NSURLConnection connection==false"); 
}; 

在本地主機上我有簡單的PHP腳本調用的test.php:

<?php 
echo "It works!!!" 
?> 

如果我在Safari中打開本地主機/ test.php的,我可以看到 「它的工作原理!!!」

+0

什麼問題? –

+0

NSURLConnection連接== true responseData :(空) – Oleg

回答

2

嘗試@「http:// localhost」而不是@「localhost」]

+0

是啊!!!謝謝。現在我的回覆數據是<3c3f7068 700a6563 686f2022 69742077 6f726b73 21212122 3b0a3f3e> – Oleg

+0

我該如何對它進行編碼? – Oleg

+0

並正常保存到NSString * – Oleg