如何在iphone(ios)服务器上上传和下载数据

发布于 2024-12-03 00:19:56 字数 184 浏览 1 评论 0原文

我正在开发名为“Messenger”的应用程序。

我的任务是从服务器上传和下载数据.. 我对此很陌生,所以我不知道该怎么做。

  1. 我想与服务器共享数据(上传)
  2. 我想从服务器获取数据(下载)

谁能帮我解决这个问题? 我需要这方面的代码。

提前致谢 ..

I am working on application called "Messanger".

My task is to upload and download data from and to a server ..
I am new to this, so I don't know how to do it.

  1. I want to share data with server (upload)
  2. I want to get data from server (download)

Can anyone help me with this ??
I need code for this.

Thanks in Advance ..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪落纷纷 2024-12-10 00:19:56

可以使用官方sdk中提供的类。

0) 公共部分。

首先,您应该创建NSURLRequest。例如, NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"apple.com"]];

1) 上传数据。

当您想要发送一些数据时,您可以通过以下方式使用它(例如,发送xml):

NSString *message = [[NSString alloc] initWithFormat:@"<?xml version=\"1.0\" ?>\n<parameters></parameters>"];
NSData* msgData = [message dataUsingEncoding:NSUTF8StringEncoding];
NSString *msgLength = [NSString stringWithFormat:@"%d",[msgData length]];

[request addValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:msgLength                         forHTTPHeaderField:@"Content-Length"];
[request setValue:@"iOsApp"                         forHTTPHeaderField:@"User-agent"];

[request setHTTPMethod:@"POST"];
[request setHTTPBody:msgData];
[message release];

2)下载数据。

现在您应该开始连接:

[NSURLConnection connectionWithRequest:request delegate:self];

3)实现必要的委托方法。最后,您应该实现您需要的委托方法。例如,在方法 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 中,您将合并接收到的数据,在方法 - (void)connectionDidFinishLoading:(NSURLConnection * )connection- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 您应该处理收到的数据。

有关您可以实施以接收有关互联网连接的更多信息并处理不同流程(如身份验证、响应代码等)的所有方法的完整列表,请阅读官方文档:NSURLConnection

You can use classes provided in official sdk.

0) Common part.

First of all you should create NSURLRequest. For example, NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"apple.com"]];

1) Uploading data.

When you want to send some data you can use this in following way (for example, sending xml):

NSString *message = [[NSString alloc] initWithFormat:@"<?xml version=\"1.0\" ?>\n<parameters></parameters>"];
NSData* msgData = [message dataUsingEncoding:NSUTF8StringEncoding];
NSString *msgLength = [NSString stringWithFormat:@"%d",[msgData length]];

[request addValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:msgLength                         forHTTPHeaderField:@"Content-Length"];
[request setValue:@"iOsApp"                         forHTTPHeaderField:@"User-agent"];

[request setHTTPMethod:@"POST"];
[request setHTTPBody:msgData];
[message release];

2) Downloading data.

Now you should start connection:

[NSURLConnection connectionWithRequest:request delegate:self];

3) implement needful delegate methods. And at last you should implement that delegate methods that you would need. For example, in method - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data you will merge received data, in methods - (void)connectionDidFinishLoading:(NSURLConnection *)connection and - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error you should process received data.

For complete list of all methods that you could implement to receive more information about your internet connection and handle different process (like authentication, response code and other) read official documentation : NSURLConnection

左秋 2024-12-10 00:19:56

您可以使用 ASIHttpRequest 库。例子很多,希望对你有帮助

You can use the ASIHttpRequest library. There are a lot of examples, hope it will help you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文