iphone/ipad开发,向服务器发送信息
计划在服务器站点上配置数据库,需要将用户提交的信息从iphone/ipad发送到服务器。在xcode中最好的方式是什么?信息应该如何发送取决于服务器上的页面(例如php),但我使用iphone/ipad开发,向服务器发送信息,iphone,Iphone,计划在服务器站点上配置数据库,需要将用户提交的信息从iphone/ipad发送到服务器。在xcode中最好的方式是什么?信息应该如何发送取决于服务器上的页面(例如php),但我使用NSURLConnection发送信息,使用$\u POST['variable']读取信息服务器端 我看看能不能找到一个我如何使用它的例子 这是我向服务器发送信息的objective-c代码: NSData *data = [NSData dataWithContentsOfFile:localFile];
NSURLConnection
发送信息,使用$\u 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];
ASIHTTPRequest可能是一个好的、简单的解决方案。它可以让你上传和下载文件到服务器和从服务器。。。它实现起来非常简单,只需几行代码。。。只需查看“设置/安装”和“如何使用”页面。感谢James提供详细信息。服务器站点将是基于java的Google App Engine。我的问题主要是关于iphone编程部分,这是最好的实现方式,考虑到可能需要缓慢的wifi连接才能将其传递到服务器站点。我已经有一段时间没有使用JSP了,所以不确定如何专门将信息传递给Java,但我假设它可以获取帖子信息?如果你觉得我的答案有用,请别忘了投赞成票,并在正确的时候接受它