2010-02-12 6 views

Répondre

1

Cela dépend de ce que vous voulez envoyer au serveur. par exemple: Fichier image ou juste du texte (comme celui d'un formulaire http).

Pour un fichier d'image qui est au format UIImage:

NSData *imageData = [NSData dataWithContentsOfFile:ImagePath]; 

ou

NSData *imageData = UIImageJPEGRepresentation(myImage.image, 1.0); 

Définir vos URL à distance (par exemple si vous utilisez php)

NSString *urlString = "http://yourdomain.com/yourphpfile.php" 

Le reste du code:

NSMutableURLRequest *request2 = [[[NSMutableURLRequest alloc] init] autorelease]; 
      [request2 setURL:[NSURL URLWithString:urlString]]; 
      [request2 setHTTPMethod:@"POST"]; 

      NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
      NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
      [request2 addValue:contentType forHTTPHeaderField: @"Content-Type"]; 


NSMutableData *body = [NSMutableData data]; 

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  

[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name='userfile'; filename='"] dataUsingEncoding:NSUTF8StringEncoding]]; 
//give any name for your file(this case image file example)   
[body appendData:[[NSString stringWithString:pic_filename.jpg] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[[NSString stringWithString:@"'\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 


//add the file data to the http body    
    [body appendData:[NSData dataWithData:imageData]]; 

[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

// setting the body of the post to the reqeust 
[request2 setHTTPBody:body]; 

Le cas de Just texte (similaire à la _GET $ en php)

Préparer les données que vous souhaitez envoyer. Pour le premier nom d'une forme par exemple

//You have your first name stored in the_first_name_to_send variable 

NSString *f_name = nil; 
f_name = [[NSString alloc] initWithFormat:@"%@", the_first_name_to_send]; 

//Encoding conversion 
//This is optional but it ensures that every character encoding type is sent without issues 
NSString* escapedUrlStringFirstName =[f_name stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; 

NSString *string1 = @"firstName="; 

NSString *myRequestString; 
     myRequestString = [string1 stringByAppendingString:escapedUrlStringFirstName]; 
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ]; 

NSMutableURLRequest *request2 = [[[NSMutableURLRequest alloc] init] autorelease]; 

[request2 setHTTPBody: myRequestData ]; 

[request2 setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 

NSData *returnData2 = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ]; 

returnData2 = nil; 

Uploading fichier image (côté serveur yourphpfile.php)

$file_tmp = $_FILES['userfile']['tmp_name']; 
$filename = $_FILES['userfile']['name']; 

Envoi du texte en utilisant HTTP POST (côté serveur yourphpfile.php)

$fname = $_POST["firstName"]; 
+0

Merci d'avoir répondu rapidement. J'essaye de signaler des données de texte de 10000 octets au serveur. Est-ce peu différent de ce qui précède? – Ferdinand

+0

Merci. Je l'ai. – Ferdinand

+0

Il ne devrait pas y avoir de différence. Notez que les données envoyées sont au format NSData pour le texte et l'image. J'ai travaillé avec des images qui contiennent plus de 500000 octets :). Ces lignes: [NSData dataWithData: imageData] et [NSData dataWithBytes: [myRequestString UTF8String] longueur: [longueur myRequestString]]; – erastusnjuki