使用 Amazon PreSigned URL 上传图片每次都会给出状态码 400 iOS
Upload image using Amazon PreSigned URL gives status code 400 everytime iOS
点数:
- 我正在努力在我的 iOS 应用程序
中使用亚马逊预签名 URL 上传图片
- 现在的问题是我每次上传时都会收到状态码 400
- 如果正常的话应该会给出204状态码
- Pre-Signed URL 在 Postman 上测试时效果很好,但在 App 端不起作用。
实现如下:
- (void)doUploadURL {
UIImage *myimage = [UIImage imageNamed:@"testimage.png"];
NSData *imageData = UIImagePNGRepresentation(myimage);
NSString *boundary = @"---011000010111000001101001";
NSMutableData *body = [NSMutableData data];
// AcL parameter
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"acl\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strAcL dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Content-Type parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Content-Type\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strContentType dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// key parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"key\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strKey dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// x-amz-meta-filename parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"x-amz-meta-filename\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strMetaFilename dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// X-Amz-Credential parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"X-Amz-Credential\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strCredential dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// X-Amz-Algorithm parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"X-Amz-Algorithm\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strAlgorithm dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// X-Amz-Date parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"X-Amz-Date\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strAmzDate dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Policy parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Policy\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strPolicy dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// X-Amz-Signature parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"X-Amz-Signature\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strSignature dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// file
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: attachment; name=\"file\"; filename=\"testimage.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSDictionary *headers = @{ @"content-type": @"multipart/form-data"};
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://visual-showcase.s3-ap-southeast-2.amazonaws.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:3600.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:body];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSLog(@" Final Response : %d",httpResponse.statusCode);
if (error) {
NSLog(@"Error : %@", error);
dispatch_async(dispatch_get_main_queue(), ^{
[[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Failed to upload" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];
return;
});
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *err = nil;
NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
}
}];
[dataTask resume];
}
响应码:
<NSHTTPURLResponse: 0x7d10e2c0> { status code: 400, headers {
Connection = close;
"Content-Type" = "application/xml";
Date = "Thu, 02 Feb 2017 07:20:18 GMT";
Server = AmazonS3;
"Transfer-Encoding" = Identity;
"x-amz-id-2" = "HIFhwBaj/ejjGoCgD2vacG2AtoxEFDXxSr6eulZtzUbuLoTcLT+AA9k6093Jyv5OIuK2wpi7+9c=";
"x-amz-request-id" = C221CD8DA2D80BCD;
} }
问题:
- 我的问题是,使用 Amazon Pre-Signed URL 上传文件时,我们需要设置什么吗?或者我们可以像正常的 API 调用一样实现它 ?
- 据我所知,它应该像正常一样以类似的方式实现 APIs
- 请让我得到任何提示,或者如果我在请求格式中做错了什么
提前致谢。
终于通过更改请求参数格式解决了问题。下面是工作代码。
- (void)doUploadURL {
UIImage *myimage = [UIImage imageNamed:@"testimage.png"];
NSData *imageData = UIImagePNGRepresentation(myimage);
NSString *boundary = @"------VohpleBoundary4QuqLuM1cE5lMwCy";
NSMutableData *body = [NSMutableData data];
//Populate a dictionary with all the regular values you would like to send.
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:strAcL forKey:@"acl"];
[parameters setValue:strContentType forKey:@"Content-Type"];
[parameters setValue:strKey forKey:@"key"];
[parameters setValue:strMetaFilename forKey:@"x-amz-meta-filename"];
[parameters setValue:strCredential forKey:@"X-Amz-Credential"];
[parameters setValue:strAlgorithm forKey:@"X-Amz-Algorithm"];
[parameters setValue:strAmzDate forKey:@"X-Amz-Date"];
[parameters setValue:strPolicy forKey:@"Policy"];
[parameters setValue:strSignature forKey:@"X-Amz-Signature"];
// add params (all params are strings)
for (NSString *param in parameters) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
// Upload image
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"testimage.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type:image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//Close off the request with the boundary
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"APIURL"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:3600.0];
[request setHTTPMethod:@"POST"];
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
[request setHTTPBody:body];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
if (error) {
NSLog(@"Error : %@", error);
dispatch_async(dispatch_get_main_queue(), ^{
[[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Failed to upload" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];
return;
});
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *err = nil;
NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
}
}];
[dataTask resume];
}
希望对大家有所帮助。
点数:
- 我正在努力在我的 iOS 应用程序 中使用亚马逊预签名 URL 上传图片
- 现在的问题是我每次上传时都会收到状态码 400
- 如果正常的话应该会给出204状态码
- Pre-Signed URL 在 Postman 上测试时效果很好,但在 App 端不起作用。
实现如下:
- (void)doUploadURL {
UIImage *myimage = [UIImage imageNamed:@"testimage.png"];
NSData *imageData = UIImagePNGRepresentation(myimage);
NSString *boundary = @"---011000010111000001101001";
NSMutableData *body = [NSMutableData data];
// AcL parameter
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"acl\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strAcL dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Content-Type parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Content-Type\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strContentType dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// key parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"key\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strKey dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// x-amz-meta-filename parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"x-amz-meta-filename\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strMetaFilename dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// X-Amz-Credential parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"X-Amz-Credential\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strCredential dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// X-Amz-Algorithm parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"X-Amz-Algorithm\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strAlgorithm dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// X-Amz-Date parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"X-Amz-Date\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strAmzDate dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Policy parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Policy\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strPolicy dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// X-Amz-Signature parameter
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"X-Amz-Signature\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strSignature dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// file
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: attachment; name=\"file\"; filename=\"testimage.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSDictionary *headers = @{ @"content-type": @"multipart/form-data"};
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://visual-showcase.s3-ap-southeast-2.amazonaws.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:3600.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:body];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSLog(@" Final Response : %d",httpResponse.statusCode);
if (error) {
NSLog(@"Error : %@", error);
dispatch_async(dispatch_get_main_queue(), ^{
[[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Failed to upload" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];
return;
});
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *err = nil;
NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
}
}];
[dataTask resume];
}
响应码:
<NSHTTPURLResponse: 0x7d10e2c0> { status code: 400, headers {
Connection = close;
"Content-Type" = "application/xml";
Date = "Thu, 02 Feb 2017 07:20:18 GMT";
Server = AmazonS3;
"Transfer-Encoding" = Identity;
"x-amz-id-2" = "HIFhwBaj/ejjGoCgD2vacG2AtoxEFDXxSr6eulZtzUbuLoTcLT+AA9k6093Jyv5OIuK2wpi7+9c=";
"x-amz-request-id" = C221CD8DA2D80BCD;
} }
问题:
- 我的问题是,使用 Amazon Pre-Signed URL 上传文件时,我们需要设置什么吗?或者我们可以像正常的 API 调用一样实现它 ?
- 据我所知,它应该像正常一样以类似的方式实现 APIs
- 请让我得到任何提示,或者如果我在请求格式中做错了什么
提前致谢。
终于通过更改请求参数格式解决了问题。下面是工作代码。
- (void)doUploadURL {
UIImage *myimage = [UIImage imageNamed:@"testimage.png"];
NSData *imageData = UIImagePNGRepresentation(myimage);
NSString *boundary = @"------VohpleBoundary4QuqLuM1cE5lMwCy";
NSMutableData *body = [NSMutableData data];
//Populate a dictionary with all the regular values you would like to send.
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:strAcL forKey:@"acl"];
[parameters setValue:strContentType forKey:@"Content-Type"];
[parameters setValue:strKey forKey:@"key"];
[parameters setValue:strMetaFilename forKey:@"x-amz-meta-filename"];
[parameters setValue:strCredential forKey:@"X-Amz-Credential"];
[parameters setValue:strAlgorithm forKey:@"X-Amz-Algorithm"];
[parameters setValue:strAmzDate forKey:@"X-Amz-Date"];
[parameters setValue:strPolicy forKey:@"Policy"];
[parameters setValue:strSignature forKey:@"X-Amz-Signature"];
// add params (all params are strings)
for (NSString *param in parameters) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
// Upload image
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"testimage.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type:image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//Close off the request with the boundary
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"APIURL"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:3600.0];
[request setHTTPMethod:@"POST"];
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
[request setHTTPBody:body];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
if (error) {
NSLog(@"Error : %@", error);
dispatch_async(dispatch_get_main_queue(), ^{
[[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Failed to upload" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] show];
return;
});
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *err = nil;
NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
}
}];
[dataTask resume];
}
希望对大家有所帮助。