计划在服务器站点配置数据库,并需要将用户从iphone/ipad提交的信息发送到服务器。在xcode中做这件事的最好方法是什么?iphone/ipad开发,发送信息到服务器
0
A
回答
0
它取决于您的服务器上的页面(例如php)应如何发送信息,但我使用NSURLConnection
发送信息并使用$_POST['variable']
来读取信息服务器端。
我会看看我能否找到一个我如何使用它的例子。
这是我的Objective-C代码将信息发送到服务器:
NSData *data = [NSData dataWithContentsOfFile:localFile];
// setting up the URL to post to
NSString *urlString = [[NSString alloc] initWithFormat:@"%@upload.php", ROOT_URL];
// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[urlString release];
/*
add some header info now
we always need a boundary when we post a file
also we need to set the content type
You might want to generate a random boundary.. this is just the same
as my output from wireshark on a valid html post
*/
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"ddMMyyyyHHmmss"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
[format release];
[now release];
NSString *filename = [NSString stringWithFormat:@"recording%@%@.mov", [[UIDevice currentDevice] uniqueIdentifier], dateString];
filename = [filename stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: video/quicktime\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
1
ASIHTTPRequest可能是一个很好&简单的解决方案。它可以让你上传和下载文件到服务器或从服务器上下载文件......它实现了超级简单,几行代码......只需看“设置/安装”和“如何使用”页面here。
相关问题
- 1. 发送反馈信息给服务器?
- 2. Web服务发送信息到iPhone
- 3. UNet不发送消息到服务器
- 4. Node.js发送消息到GCM服务器
- 5. 发送错误信息,从服务到控制器
- 6. 发送img到服务器
- 7. 发送UIImage到服务器
- 8. 发送xml到服务器
- 9. 发送LatLong到服务器
- 10. 将发布APK发送到服务器
- 11. 发送信息到Asana
- 12. 发送图像信息到接收器
- 13. 在iOS应用上加密信用卡信息并发送到服务器
- 14. 发送服务器多条消息? C#
- 15. 从NIO服务器发送消息
- 16. Android C2DM服务器发送消息
- 17. 的Node.js:发送消息至服务器
- 18. Android。如何将数据发送到开发人员服务器
- 19. 发送日期到wcf休息服务
- 20. 发送Windows消息到Windows服务
- 21. 发送信息给客户端服务器事件
- 22. 如何从Web服务器发送/接收信息?
- 23. 如何在发送PHP邮件时隐藏服务器信息?
- 24. 由ozeki消息服务器通过PHP发送短信错误?
- 25. 使用AJAX发送和接收来自服务器的信息
- 26. 服务器不发送回一个确认信息给客户
- 27. 通过手机发送信息给网络服务器
- 28. 从服务器发送动态错误/失败结果信息
- 29. Android:如何定期向服务器发送位置信息
- 30. 通过服务器发送变量信息(c#)
感谢James提供的详细信息。服务器网站将是基于Java的Google App Engine。我的问题主要是关于iPhone编程部分,最好的实现方式,考虑可能的慢速WiFi连接传递给服务器站点。 – user908264
自从我使用JSP以来已经有一段时间了,所以不确定如何将信息专门传递给Java,但我认为它有一些可以获取POST信息的东西? 如果您发现我的答案有用,请不要忘记以正确方式upvote并接受它。 ;) –