NSURLRequest setHttpBody

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

我的代码:

// 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];
0
额外 编辑
意见: 1
你有什么问题?
额外 作者 Vaquita,
@海王星。 Array([email] aa @ qq_com] => ----------------------------- 14737809831466499882746641449 Content-Disposition:form-data; name =“password”c8837b23ff8aaa8a2dde915473ce0991)我希望$ value是[email protected] whilt $ key是email,$ value是c8837b23ff8aaa8a2dde915473ce0991,而$ key是密码。
额外 作者 anna,
@海王星。我无法获取我的$ _POST ['email']或$ _POST ['password']。但我可以得到$ _FILES ['uploadingImage'],这是正确的。任何建议?
额外 作者 anna,
@Maulik谢谢〜
额外 作者 anna,
头痛.......
额外 作者 anna,

1 答案

它应该是

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

但现在你是这样做的

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

希望你明白错误..

2
额外
我只是想出了它。 <body appendData:[[NSString stringWithString:@“Content-Disposition:form-data; name = \”email \“\ r \ n”] 应该是这样的:<body appendData :[[NSString stringWithString:@“Content-Disposition:form-data; name = \”email \“\ r \ n \ r \ n”]
额外 作者 anna,
谢谢。我想知道有关于它的一些解释。可以使用这种格式,但我仍然想知道更多。你有任何教程吗?
额外 作者 anna,