//接口地址
NSString*path = @"URLString";
//boundary
NSString*theBoundary =@"myBoundary";
//访问请求
NSMutableURLRequest*request = [[NSMutableURLRequestalloc]initWithURL:[NSURLURLWithString:path]];
request.HTTPMethod=@"POST";
//用来拼接参数
NSMutableData*data = [NSMutableDatadata];
//拼接第一个参数
[dataappendData:[[NSStringstringWithFormat:@"--%@\\r\\n", theBoundary]dataUsingEncoding:NSUTF8StringEncoding]];
//拼接参数名
[data appendData:[@"Content-Disposition:form-data;name=\\"uid\\"\\r\\n"dataUsingEncoding:NSUTF8StringEncoding]];
[dataappendData:[@"\\r\\n"dataUsingEncoding:NSUTF8StringEncoding]];
//拼接参数值
[dataappendData:[@"11230953"dataUsingEncoding:NSUTF8StringEncoding]];
[dataappendData:[@"\\r\\n"dataUsingEncoding:NSUTF8StringEncoding]];
//拼接第二个参数
[dataappendData:[[NSStringstringWithFormat:@"--%@\\r\\n", theBoundary]dataUsingEncoding:NSUTF8StringEncoding]];
//拼接参数名
[data appendData:[@"Content-Disposition:form-data;name=\\"file\\";filename=\\"myText.txt\\"\\r\\n"dataUsingEncoding:NSUTF8StringEncoding]];
//拼接文件类型
[dataappendData:[@"Content-Type:text/plain"dataUsingEncoding:NSUTF8StringEncoding]];
[dataappendData:[@"\\r\\n"dataUsingEncoding:NSUTF8StringEncoding]];
//拼接参数值
[dataappendData:[NSDatadataWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"myText"ofType:@"txt"]]];
[dataappendData:[@"\\r\\n"dataUsingEncoding:NSUTF8StringEncoding]];
//拼接结束标志
[dataappendData:[[NSStringstringWithFormat:@"--%@--", theBoundary]dataUsingEncoding:NSUTF8StringEncoding]];
request.HTTPBody= data;
[requestsetValue:[NSStringstringWithFormat:@"multipart/form-data;boundary=%@", theBoundary]forHTTPHeaderField:@"Content-Type"];
[requestsetValue:[NSStringstringWithFormat:@"%ld", data.length]forHTTPHeaderField:@"Content-Length"];
NSURLSession*session = [NSURLSessionsharedSession];
NSURLSessionDataTask*dataTask = [sessiondataTaskWithRequest:requestcompletionHandler:^(NSData*_Nullabledata,NSURLResponse*_Nullableresponse,NSError*_Nullableerror) {
NSDictionary*dic = [NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingAllowFragmentserror:nil];
NSLog(@"%@", dic);
}];
[dataTaskresume];