iOS - 多部分/表单数据发布

时间:2015-12-14 11:41:13

标签: ios iphone http-headers nsurlconnection multipartform-data

我正在开发一个使用multipart / form_data的iOS应用。但我一直坚持下去。 我所拥有的是来自Http Analyzer 7的数据结构。

Raw stream from Http Analyzer

我已经谷歌搜索并尝试了很多在iOS应用程序中完成此操作,但没有任何成功的机会。

有人可以帮助我吗?

这是我尝试过的。

- (void)signUpOnWeb:(NSString *)strStrToken {

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http//www.karelboele.com/forms/signups"] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];

[request setHTTPMethod:@"POST"];
[request setValue:@"www.karelboele.com" forHTTPHeaderField:@"Host"];
[request setValue:@"keep-alive" forHTTPHeaderField:@"Connection"];
[request setValue:@"text/javascript" forHTTPHeaderField:@"Accept"];
[request setValue:@"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0" forHTTPHeaderField:@"User-Agent"];
[request setValue:@"http://www.karelboele.com/app" forHTTPHeaderField:@"Referer"];
[request setValue:@"gzip, deflate" forHTTPHeaderField:@"Accept-Encoding"];
[request setValue:@"en-US,en;q=0.5" forHTTPHeaderField:@"Accept-Language"];
[request setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];


NSDictionary *params = @{@"authenticity_token" : strStrToken, @"page_id" : @"406", @"return_to" : @"http://www.karelboele.com/app", @"email_address" : @"", @"signup[first_name]" : @"Vuhar", @"signup[last_name]" : @"Mamedov", @"signup[email]" : @"vuharmamedov@gmail.com", @"signup[born_at_chronic]" : @"10/12/1985", @"signup[email_opt_in]" : @"0", @"signup[email_opt_in]" : @"1", @"signup[mobile_number]" : @"123456789", @"signup[mobile_opt_in]" : @"0", @"signup[mobile_opt_in]" : @"1", @"signup[phone_number]" : @"123456789", @"signup[submitted_address]" : @"1, kyiv, kyiv, kyiv, 130000", @"signup[country_code]" : @"UA", @"signup[is_volunteer]" : @"0", @"signup[activity_is_private]" : @"0", @"commit" : @"Accept Terms and Conditions and submit"};


NSString *boundary = @"---------------------------1388669659902";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];

[request setValue:contentType forHTTPHeaderField: @"Content-Type"];


NSData *httpBody = [self createBodyWithBoundary:params boundary:(NSString*)boundary];


[request setHTTPBody:httpBody];


NSString *postLength = [NSString stringWithFormat:@"%d", (int)[httpBody length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {

     NSHTTPURLResponse *responseCode = (NSHTTPURLResponse *)response;

     if([responseCode statusCode] != 200){
         NSLog(@"Error getting HTTP status code %li", (long)[responseCode statusCode]);

     }

     else {

         NSString* htmlResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
     }
 }];

}

- (NSData *)createBodyWithBoundary:(NSDictionary *)parameters boundary:(NSString*)boundary {
NSMutableData *httpBody = [NSMutableData data];

// add params (all params are strings)

[parameters enumerateKeysAndObjectsUsingBlock:^(NSString *parameterKey, NSString *parameterValue, BOOL *stop) {
    [httpBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [httpBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", parameterKey] dataUsingEncoding:NSUTF8StringEncoding]];
    [httpBody appendData:[[NSString stringWithFormat:@"%@\r\n", parameterValue] dataUsingEncoding:NSUTF8StringEncoding]];
}];

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

return httpBody;

}

我不太确定我犯了什么错误。

任何帮助都非常感谢!

谢谢,

Vuhar

1 个答案:

答案 0 :(得分:-1)

使用AFNetworking多部分表单数据。它可以节省您的时间,还可以跟踪上传的数据量,甚至是失败块中的请求失败。