2011-09-04 3 views

Répondre

0

C'est aux pages (php par exemple) sur votre serveur comment les informations doivent être envoyées, mais j'utilise NSURLConnection pour envoyer des informations et $_POST['variable'] pour lire le côté serveur d'informations.

Je vais voir si je peux trouver un exemple de comment je l'ai utilisé.

C'est mon code objectif-c pour envoyer des informations à un serveur:

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

Merci James pour des informations détaillées. Le site du serveur sera Google App Engine, basé sur Java. Ma question concerne principalement la partie de la programmation de l'iphone, la meilleure façon d'implémenter, en considérant la connexion wifi lente possible pour la passer au site du serveur. – user908264

+0

Cela fait un moment que j'ai utilisé JSP, donc je ne sais pas comment passer l'information spécifiquement à Java, mais je suppose qu'il a quelque chose à obtenir des informations POST? Si vous trouvez ma réponse utile, s'il vous plaît n'oubliez pas de upvote et l'accepter à la bonne. ;) –

1

ASIHTTPRequest pourrait être une bonne solution & facile. Il vous permet de télécharger et télécharger des fichiers vers et à partir d'un serveur ... Il implémente super-facile, quelques lignes de code ... il suffit de voir les pages "setup/install" et "comment utiliser" here.

Questions connexes