【问题标题】:What is the easiest way to make an HTTP GET request with IOS 5?使用 IOS 5 发出 HTTP GET 请求的最简单方法是什么?
【发布时间】:2012-02-06 15:53:49
【问题描述】:

我正在尝试从我的应用程序向我的网络服务器上的 php 文件发送建议,我已经在浏览器中测试了 php 脚本,该脚本向用户发送电子邮件并将建议存储在我的数据库中,一切正常.. 运行以下脚本时,我通过 IOS 成功连接,但我的数据库中没有收到结果..

NSString *post = [NSString stringWithFormat:@"http://blahblah.com/suggest.php?s=%@&n=%@&e=%@", suggestion, name, email];

    // Create the request.
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:post]
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:60.0];

    // create the connection with the request
    // and start loading the data
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection) {

        NSLog(@"Connection establisted successfully");

    } else {

        NSLog(@"Connection failed.");

    }

我检查了所有字符串并使用 %20 等对所有空格进行了编码。任何人都可以看出我的脚本无法运行的明显原因吗?

在不打开 Safari 的情况下从我的应用发出 HTTP 请求的最简单方法是什么?

【问题讨论】:

    标签: ios http post get


    【解决方案1】:

    您的问题是您正在创建连接,但没有发送实际的“连接”请求。而不是

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    

    尝试使用这段代码:

    NSURLResponse* response = nil;
    NSData* data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil]
    

    这是一个快速而肮脏的解决方案,但请记住,当此连接正在进行时,您的 UI 线程将似乎被冻结。绕过它的方法是使用异步连接方法,这比上面的要复杂一些。在网上搜索 NSURLConnection 发送异步请求 - 答案就在那里。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-30
      • 2010-09-23
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多