POST提交的时候,+号变成空格了。刚开始以为是服务器端有问题,加密出现问题,后端说不用想,接口绝对没问题。我说我也没问题,我用方法把base64转了一下的,但后面发现还是有+号变空格了,我默默的走开了。
改换用提交表单的方法转base64。
- (void)postFormData:(NSString *)url parameters:(NSDictionary *)params complete:(void(^)(id info,NSError *error))complete
{
NSURL *nsurl = [NSURL URLWithString:url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nsurl];
request.HTTPMethod = @"POST";
[request setValue:@"multipart/form-data;boundary=boundary" forHTTPHeaderField:@"Content-Type"];
[request setTimeoutInterval:30];
[body appendData:[self getDataWithString:@"--BOUNDARY\r\n" ]];
NSDictionary *dicData = params;
NSMutableString *bodyContent = [NSMutableString string];
for(NSString *key in dicData.allKeys)
{
id value = [dicData objectForKey:key];
[bodyContent appendFormat:@"--BOUNDARY\r\n"];
[bodyContent appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];
[bodyContent appendFormat:@"%@\r\n",value];
}
NSData *bodyData=[bodyContent dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = bodyData;
[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)body.length] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"multipart/form-data; boundary=BOUNDARY" forHTTPHeaderField:@"Content-Type"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue currentQueue]];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
{
[session finishTasksAndInvalidate];
if (error)
{
}
else
{
}
}];
[dataTask resume];
}
-(NSData *)getDataWithString:(NSString *)string
{
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
return data;
}