2011-11-24 153 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