我使用 Rest Kit 完成了相同的请求。Rest 套件允许发送 MTOM 规范中的附件。
您需要的第一件事是下载 Restkit。
以下是使用 RestKit 的 MTOM 的代码片段。
abv.h
#import "RestKit/RestKit.h"
RKObjectManager *man;
RKObjectLoader *loader;
abc.m
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
//Start Progress bar
RKParams * params = [[RKParams alloc] init];
UIImage *image=[UIImage imageNamed:@"zbar-samples.png"];
RKObjectManager *man;
NSData * fileData=UIImageJPEGRepresentation(image,0.7);
[params setValue:@"1234" forParam:@"encryptedToken"];
[params setValue:modelObj.docNameTobeSent
forParam:@"documentName"];
RKParamsAttachment * attachments = [params setData:fileData forParam:@"file"];
[attachments setMIMEType:@"image/jpeg"];
[attachments setFileName:[NSString stringWithFormat:@"%@.jpeg",modelObj.name]];
self.man = [RKObjectManager objectManagerWithBaseURL:YOUR URL];
self.loader = [self.man loadObjectsAtResourcePathUsingPOSTRPC:@"upload.form" objectMapping:nil PostParams:params delegate:self];
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error
{
//Handle fail error
//stop Progress bar
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects
{
NSLog(@"%@",@"didLoadObjects");
}
- (void)objectLoaderDidFinishLoading:(RKObjectLoader*)objectLoader
{
NSLog(@"%@",@"objectLoaderDidFinishLoading");
//stop Progress bar
}
- (void)objectLoaderDidLoadUnexpectedResponse:(RKObjectLoader*)objectLoader
{
NSLog(@"%@",@"objectLoaderDidLoadUnexpectedResponse");
//stop Progress bar
}