Advertisement
Guest User

Untitled

a guest
Apr 15th, 2014
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1.  
  2. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  3.  
  4. //Set Params
  5. [request setHTTPShouldHandleCookies:NO];
  6. [request setTimeoutInterval:60];
  7. [request setHTTPMethod:@"POST"];
  8.  
  9. //Create boundary, it can be anything
  10. NSString *boundary = @"------fafafafafafafafafafaVohpleBoundary4QuqLuM1cE5lMwCy";
  11.  
  12. // set Content-Type in HTTP header
  13. NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
  14. [request setValue:contentType forHTTPHeaderField: @"Content-Type"];
  15.  
  16. // post body
  17. NSMutableData *body = [NSMutableData data];
  18.  
  19. //Populate a dictionary with all the regular values you would like to send.
  20. NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
  21.  
  22. [parameters setValue:[NSString stringWithFormat:@"hello.png"] forKey:@"/ProfImage"];
  23.  
  24.  
  25.  
  26.  
  27. // add params (all params are strings)
  28. for (NSString *param in parameters) {
  29. [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  30. [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
  31. [body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
  32. }
  33.  
  34. NSString *FileParamConstant = @"imageParamName";
  35.  
  36. NSData *imageData = UIImageJPEGRepresentation(imgProfile.image, 1.0);
  37.  
  38. //Assuming data is not nil we add this to the multipart form
  39. if (imageData)
  40. {
  41. [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  42. [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
  43. [body appendData:[@"Content-Type:image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  44. [body appendData:imageData];
  45. [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  46. }
  47.  
  48. //Close off the request with the boundary
  49. [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  50.  
  51. // setting the body of the post to the request
  52. [request setHTTPBody:body];
  53.  
  54. // set URL
  55. [request setURL:[NSURL URLWithString:@"http://72.9.100.234/pixitchphotosvc/Service1.svc"]];
  56.  
  57. [NSURLConnection sendAsynchronousRequest:request
  58. queue:[NSOperationQueue mainQueue]
  59. completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  60.  
  61. NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
  62.  
  63.  
  64. NSLog(@"%d",[httpResponse statusCode]);
  65. if ([httpResponse statusCode] == 200) {
  66.  
  67. NSLog(@"success");
  68. }
  69.  
  70. }];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement