2011-09-04 97 views

回答

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]; 
+0

感谢James提供的详细信息。服务器网站将是基于Java的Google App Engine。我的问题主要是关于iPhone编程部分,最好的实现方式,考虑可能的慢速WiFi连接传递给服务器站点。 – user908264

+0

自从我使用JSP以来已经有一段时间了,所以不确定如何将信息专门传递给Java,但我认为它有一些可以获取POST信息的东西? 如果您发现我的答案有用,请不要忘记以正确方式upvote并接受它。 ;) –

1

ASIHTTPRequest可能是一个很好&简单的解决方案。它可以让你上传和下载文件到服务器或从服务器上下载文件......它实现了超级简单,几行代码......只需看“设置/安装”和“如何使用”页面here