如何从iphone应用程序将数据写入Web服务器? [英] How to write data to the web server from iphone application?

查看:157
本文介绍了如何从iphone应用程序将数据写入Web服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待通过我的iphone应用程序在Web服务器上发布一些数据和信息。这样做需要你的帮助。我没有办法从iphone sdk向web服务器发布数据。

I am looking forward for posting some data and information on the web server through my iphone application. need your help in doing so. I am not getting the way to post data to the web server from iphone sdk.

推荐答案

这取决于你想要的方式将数据发送到Web服务器。如果您只想使用HTTP POST方法,则至少有两个选项。您可以使用同步或异步NSURLRequest。如果您只想发布数据而不需要等待来自服务器的响应,我强烈建议使用异步数据,因为它不会阻止用户界面。即它在后台运行,用户可以继续使用(与之交互)你的应用程序。异步请求使用委托来告诉应用程序已发送,取消,完成请求等。如果需要,您还可以通过委托方法获取响应。

It depends in what way you want to send data to the web server. If you want to just use the HTTP POST method, there are (at least) two options. You can use a synchronous or an asynchronous NSURLRequest. If you only want to post data and do not need to wait for a response from the server, I strongly recommend the asynchronous one, because it does not block the user interface. I.e. it runs "in the background" and the user can go on using (that is interacting with) your app. Asynchronous requests use delegation to tell the app that a request was sent, cancelled, completed, etc. You can also get the response via delegate methods if needed.

这是一个异步HTTP POST请求的示例:

Here is an example for an asynchronous HTTP POST request:

// define your form fields here:
NSString *content = @"field1=42&field2=Hello";

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.example.com/form.php"]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[content dataUsingEncoding:NSISOLatin1StringEncoding]];

// generates an autoreleased NSURLConnection
[NSURLConnection connectionWithRequest:request delegate:self];

请参阅 NSURLConnection类参考,了解委托方法的详细信息。

Please refer to the NSURLConnection Class Reference for details on the delegate methods.

您还可以在生成请求后发送同步请求:

You can also send a synchronous request after generating the request:

[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

如果您传递 NSURLResponse ** 为返回响应,您将在指针指向的对象中找到服务器的响应。请记住,UI将在处理同步请求时阻止。

If you pass a NSURLResponse ** as returning response, you will find the server's response in the object that pointer points to. Keep in mind that the UI will block while the synchronous request is processed.

这篇关于如何从iphone应用程序将数据写入Web服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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