Objective c AFNetworking上载进程发送SOAP NSData
我正在使用Objective c AFNetworking上载进程发送SOAP NSData,objective-c,ios,afnetworking,nsmutableurlrequest,Objective C,Ios,Afnetworking,Nsmutableurlrequest,我正在使用AFNetworking,希望将NSData对象上载到我的服务器。我试图上传到我们的SOAP服务器,所以我有XML,我已经转换成了一个NSData对象。由于某些原因,以下操作不起作用: // Back to NSData NSData *convertedFile = [xml dataUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:siteConfiguration.soa
AFNetworking
,希望将NSData
对象上载到我的服务器。我试图上传到我们的SOAP服务器,所以我有XML,我已经转换成了一个NSData
对象。由于某些原因,以下操作不起作用:
// Back to NSData
NSData *convertedFile = [xml dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:siteConfiguration.soapURL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFormData:convertedFile name:assetCreation.name];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
// Progress
float totalProgress = (float)totalBytesWritten/(float)totalBytesExpectedToWrite;
progress(totalProgress, totalBytesWritten, totalBytesExpectedToWrite);
if (totalBytesExpectedToWrite == totalBytesWritten) {
completion();
}
}];
[operation start];
似乎我的服务器真的想用我的
NSData
对象发送HTTPBody
。是否可以对AFNetworking进程回调执行相同的操作?好的,我已经让它工作了。看来我做错了:
// Setup the connection
NSString *wsdlURL = [NSString stringWithFormat:@"%@?WSDL", siteConfiguration.soapURL];
NSURL *serviceUrl = [NSURL URLWithString:wsdlURL];
NSMutableURLRequest *serviceRequest = [NSMutableURLRequest requestWithURL:serviceUrl];
[serviceRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-type"];
[serviceRequest setHTTPMethod:@"POST"];
[serviceRequest setHTTPBody:[xml dataUsingEncoding:NSUTF8StringEncoding]];
// Operation
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:serviceRequest];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
// Progress
float totalProgress = (float)totalBytesWritten/(float)totalBytesExpectedToWrite;
progress(totalProgress, totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success");
MatrixAsset *asset = [[MatrixAsset alloc] init];
completion(asset);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", error.localizedDescription);
NSLog(@"error: %@", error.localizedFailureReason);
}];
[operation start];
您需要SOAPAction才能继续:
[serviceRequest setValue:@"SOAP_ACTION_URL" forHTTPHeaderField:@"SOAPAction"];
您是否尝试过
[formData appendPartWithFileData:convertedFile name:assetCreation.name]
而不是[formData appendPartWithFormData:convertedFile name:assetCreation.name]代码>?我还将mimeType:
设置为text/xml
至少在最新版本的AFNetworking中没有名为[formData appendPartWithFileData:convertedFile name:assetCreation.name]的方法。我想我需要以表单的形式发送它,因为它是发送到我们的SOAP服务器的,并且还有其他字段数据正在发送。将内容类型设置为text/xml没有帮助。我使用了:[请求setValue:@“text/xml”forHTTPHeaderField:@“Content type”];它停止了进程回调的工作。
[serviceRequest setValue:@"SOAP_ACTION_URL" forHTTPHeaderField:@"SOAPAction"];