1
我正在開發一個使用multipart/form_data的iOS應用程序。但我陷入了困境。 什麼我是從http分析器7iOS - multipart/form-data post
數據結構我GOOGLE和嘗試了很多在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]" : @"[email protected]", @"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
第一個問題:你得到了什麼錯誤/意外行爲? 「我卡住了」有點寬泛。 – dan
錯誤\t NSURLError * \t domain:@「NSURLErrorDomain」 - code:18446744073709550614 \t 0x00007ffe83d9c960 –
剛剛解決了我使用AFNetworking的問題。優秀! –