0

I got a wrong post(php:echo $_POST) like below: Array ( [email"aa@qq_com] => -----------------------------14737809831466499882746641449 Content-Disposition: form-data; name="password" c8837b23ff8aaa8a2dde915473ce0991 )

my code:

// set header value ,   some random text that will never occur in the body  
NSString *boundary = @"---------------------------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];              //  email part         
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];        
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"email\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];   
[body appendData:[anEmail dataUsingEncoding:NSUTF8StringEncoding]];     
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];          //  password part     [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];     
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"password\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];    
[body appendData:[aPassword dataUsingEncoding:NSUTF8StringEncoding]];     
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];          //  image part     [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];        
[body appendData:[[NSString stringWithFormat: @"Content-Disposition: form-data; name=\"uploadingImage\"; filename=%@\r\n", anImageName]                        dataUsingEncoding:NSUTF8StringEncoding]];    
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];   
[body appendData:[NSData dataWithData:aFileData]];     
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];             
[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  
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
Maulik
  • 19,348
  • 14
  • 82
  • 137
anna
  • 662
  • 5
  • 28
  • What is the problem you are having? – jay May 25 '12 at 06:25
  • @Neptune. Array ( [email"aa@qq_com] => -----------------------------14737809831466499882746641449 Content-Disposition: form-data; name="password" c8837b23ff8aaa8a2dde915473ce0991 ) I want $value is aa@qq.com whilt $key is email, and $value is c8837b23ff8aaa8a2dde915473ce0991 while $key is password. – anna May 25 '12 at 06:27
  • @Neptune. I can't get my $_POST['email'] or $_POST['password']. But I can get $_FILES['uploadingImage'], and it's correct. Any suggestion? – anna May 25 '12 at 06:29

1 Answers1

2

it should be

name=\"password\"\r\n\r\n%@",password];

but now you are doing like this

name=\"password\"\r\n%@\r\n",password];

Hope you understood the mistake..

Krrish
  • 2,256
  • 18
  • 21
  • I just figured it out. `[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"email\"\r\n"]` should be like this: `[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"email\"\r\n\r\n"]` – anna May 25 '12 at 08:35
  • thanks. I wonder there is some explanation on it. It's ok to use this format, but I still want to know more. Do you have any tutorial? – anna May 25 '12 at 08:43