我從服務器獲取數據時遇到問題。服務器是我的本地主機。從服務器獲取數據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的,我可以看到 「它的工作原理!!!」
什麼問題? –
NSURLConnection連接== true responseData :(空) – Oleg