Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 获取Web服务的内容_Iphone_Nsurlconnection_Nsurlrequest - Fatal编程技术网

Iphone 获取Web服务的内容

Iphone 获取Web服务的内容,iphone,nsurlconnection,nsurlrequest,Iphone,Nsurlconnection,Nsurlrequest,我有一个类似的URL。当我把它输入Safari的地址栏时,我会看到一个类似“Error”或“OK”的结果 那么,如何从代码中正确调用该URL并将结果作为字符串获取呢 我用NSURLConnection、nsurrequest和nsurresponse尝试过,但我的响应对象总是nil 这些类中的“响应”指的是协议响应(HTTP头等),而不是内容 要获取内容,您有几个选项: 在异步模式下使用NSURLConnection: 在同步模式下使用NSURLConnection: // Error chec

我有一个类似的URL。当我把它输入Safari的地址栏时,我会看到一个类似“Error”或“OK”的结果

那么,如何从代码中正确调用该URL并将结果作为字符串获取呢

我用
NSURLConnection
nsurrequest
nsurresponse
尝试过,但我的响应对象总是
nil

这些类中的“响应”指的是协议响应(HTTP头等),而不是内容

要获取内容,您有几个选项:

  • 在异步模式下使用NSURLConnection:
  • 在同步模式下使用NSURLConnection:

    // Error checks omitted
    NSURL *URL = [NSURL URLwithString:@"http://www.myserver.com/myservice.php?param=foobar"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    NSData *data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:nil
                                                     error:nil];
    
  • 使用
    [NSString stringWithContentsOfURL:

    NSURL *URL = [NSURL URLwithString:@"http://www.myserver.com/myservice.php?param=foobar"];
    NSString *content = [NSString stringWithContentsOfURL:URL];
    

  • 当然,只有当您的内容非常小时,才应使用选项2和3,以保持响应能力。

    谢谢!二号为我做的。:)