异步POST到服务器 [英] Asynchronous POST to server

查看:153
本文介绍了异步POST到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的目标是通过查询数据库做一个简单的用户名/密码认证。直到连接工作体面我的PHP目标文件只是有以下code:

The goal is to do a simple username/password authentication by querying a database. Until the connection is working decently my php destination file simply has following code:

echo "Posted: " . $_POST['email'];

在code做这个同步是这样的:

The code to do this synchronously is this:

NSString *post = [[NSString alloc] initWithFormat:@"email=%@&password=%@", self.email.text, ..]; // .. simplified keychainItem
NSData *postEncoded = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [postEncoded length]];

NSURL *url = [NSURL URLWithString:@"http://eng.studev.groept.be/web2.0/a11_web02/improver/app/testPost"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postEncoded];

NSError *error = nil;
NSURLResponse *response = nil;
NSData *encodedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *data=[[NSString alloc]initWithData:encodedData encoding:NSUTF8StringEncoding];
NSLog(@"Data? %@",data);

的正确值显示为回声。但是,试图以异步方式做到这一点的时候,我得到下面的PHP错误:未定义指数:电子邮件。
我试着开始与该行的异步请求:

The correct value is shown as an echo. But when trying to do this asynchronously, I get following php error: "Undefined index: email". I try to start the asynchronous request with this line:

[[NSURLConnection connectionWithRequest:request delegate:self] start];

然后,我委托方法连接:didReceiveResponse,但我似乎无法获取数据了......或者我需要另一个委托方法?此外,如何'安全'是只用回声检查您的查询的结果(我需要/想流也许?)??

Then, I have the delegate method connection:didReceiveResponse, but there I cannot seem to get the data out... Or do I need another delegate method? Also, how 'safe' is it to check the result of your query by using just an echo (do I need/want a stream maybe?) ??

蒂亚

修改

问题涉及到服务器,而不是客观-C code。问了一个新的问题,以达到正确的观众: $ _ POST剩余空

Problem related to the server, not to objective-C code. Asked a new question to reach the correct audience: $_POST remaining empty

推荐答案

@ott是在正确的轨道上,我会尽力澄清。

@ott is on the right track, I'll try to clarify.


  1. 您不需要启动他说。这是良性的作为连接将自动启动。

  1. You don't need start as he says. It's benign as the connection will start automatically.

initWithRequest:委托 connectionWithRequest:委托:是除了新的保留状态相当于连接对象。

initWithRequest:delegate and connectionWithRequest:delegate: are equivalent except for the retain state of the new connection object.

真正的问题是B / C您使用 connectionWithRequest:委托返回的连接在运行循环结束时自动释放,你是不是保留它在属性。因此,连接永远不会发生。

The real problem is b/c you are using connectionWithRequest:delegate the returned connection is autoreleased at the end of the run loop and you are not retaining it in a property. Therefore, the connection never starts.

该解决方案是增加一个属性 @属性(非原子,保留)NSURLConnection的*从到您的类并将此属性设置为连接>连接​​:withRequest:

The solution is to add a property @property (nonatomic, retain) NSURLConnection *connection to your class and set this property to the connection returned from connection:withRequest:

您再松开在完井方式连接连接:didFinishLoading 连接:didFailWithError:

You then release the connection in the completion methods connection:didFinishLoading and connection:didFailWithError:.

这篇关于异步POST到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆