简体   繁体   English

从服务器objective-c获取数据

[英]get data from server objective-c

I have a problem getting data from the server. 我从服务器获取数据时遇到问题。 server is my localhost. 服务器是我的本地主机。

    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");
};

on localhost I have simple php script called test.php: 在localhost上我有一个名为test.php的简单php脚本:

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

If I'm opening localhost/test.php in safari I can see "It works!!!" 如果我在safari中打开localhost / test.php,我可以看到“它有效!!!”

试试@“http:// localhost”而不是@“localhost”]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM