我正在尝试从我的应用程序向我的 Web 服务器上的 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 请求的最简单方法是什么?
最佳答案
您的问题是您正在创建连接,但没有发送实际的“连接”请求。而不是
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
尝试使用这段代码:
NSURLResponse* response = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil]
这是一个快速而肮脏的解决方案,但请记住,当此连接正在进行时,您的 UI 线程将看起来被卡住。绕过它的方法是使用异步连接的方法,它比上面的要复杂一点。在网络上搜索 NSURLConnection send asynchronous request - 答案就在那里。
关于ios - 使用 IOS 5 发出 HTTP GET 请求的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9162910/