简体   繁体   English

使用IOS 5发出HTTP GET请求的最简单方法是什么?

[英]What is the easiest way to make an HTTP GET request with IOS 5?

I am trying to send a suggestion from my app to a php file on my web server, I have tested the php script in my browser which sends an email to the user and stores the suggestion in my database, which all works fine.. And when running the following script I get a successful connection via IOS however i do not receive the results in my database.. 我试图从我的应用程序发送一个建议到我的网络服务器上的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.");

    }

I have checked all the strings and encoded all spaces with %20 etc.. Can anyone see any glaringly obvious reason why my script won't work? 我检查了所有的字符串并使用%20等编码了所有空格。任何人都可以看到任何明显的原因,为什么我的脚本不起作用?

What is the easiest way to make a HTTP request from my app without opening safari? 在不打开Safari的情况下,从我的应用程序发出HTTP请求的最简单方法是什么?

You problem is that you're creating the connection, but are not sending the actual "connect" request. 您的问题是您正在创建连接,但不发送实际的“连接”请求。 Instead of 代替

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

try using this piece of code: 尝试使用这段代码:

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

This is quick and dirty solution, but keep in mind that while this connection is in progress, your UI thread will appear to be frozen. 这是一个快速而又脏的解决方案,但请记住,当此连接正在进行时,您的UI线程似乎将被冻结。 The way around it is to use asynchronous connection method, which is a bit more complicated than the above. 解决它的方法是使用异步连接方法,这比上面的方法复杂一点。 Search web for NSURLConnection send asynchronous request - the answer is there. 在网上搜索NSURLConnection发送异步请求 - 答案就在那里。

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

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