【问题标题】:Raw XML Post using AFHTTPSessionManager使用 AFHTTPSessionManager 的原始 XML Post
【发布时间】:2017-01-18 10:45:56
【问题描述】:

如何使用AFHTTPSessionManager 将原始 XML 帖子发送到 SOAP 接口?以下代码是来自https://github.com/wymsee/cordova-HTTP/blob/master/src/ios/CordovaHttpPlugin.m#L69的fork:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.securityPolicy = securityPolicy;
NSString *url = [command.arguments objectAtIndex:0];
NSDictionary *parameters = [command.arguments objectAtIndex:1];
NSDictionary *headers = [command.arguments objectAtIndex:2];
[self setRequestHeaders: headers forManager: manager];

CordovaHttpPlugin* __weak weakSelf = self;
manager.responseSerializer = [TextResponseSerializer serializer];
[manager POST:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    [self setResults: dictionary withTask: task];
    [dictionary setObject:responseObject forKey:@"data"];
    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
    [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
} failure:^(NSURLSessionTask *task, NSError *error) {
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    [self setResults: dictionary withTask: task];
    NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
    [dictionary setObject:errResponse forKey:@"error"];
    CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
    [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];

这会将multipart/form-data 发布到给定的(soap)网址。是否可以更改请求以发送带有原始正文的text/xml

谢谢!

【问题讨论】:

    标签: objective-c cordova afnetworking afhttpsessionmanager


    【解决方案1】:

    我想出了以下解决问题的方法:

    NSString *url = [command.arguments objectAtIndex:0]; NSData *body = [[command.arguments objectAtIndex:2] dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *headers = [command.arguments objectAtIndex:3];

    CordovaHttpPlugin* __weak weakSelf = self;
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSMutableURLRequest *req = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:url parameters:nil error:nil];
    [req setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
    [req setValue:@"text/xml" forHTTPHeaderField:@"Accept"];
    [req setHTTPBody:body];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    // Append headers
    for (NSString* key in headers) {
        NSString *value = [headers objectForKey:key];
        [req setValue:value forHTTPHeaderField:key];
    }
    
    [[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
    
        NSDictionary *responseHeaders = [(NSHTTPURLResponse *)response allHeaderFields];
        long status = (long) [(NSHTTPURLResponse *) response statusCode];
        NSLog(@"Status: %ld", status);
    
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
        [dictionary setObject:[NSNumber numberWithInteger:status] forKey:@"status"];
        [dictionary setObject:responseHeaders forKey:@"headers"];
    
        if (!error) {
            NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            NSLog(@"POST SUCCESS: %@", responseString);
            //[self setResults: dictionary withTask: task];
    
            [dictionary setObject:responseString forKey:@"data"];
    
            CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary];
            [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        } else {
            //[self setResults: dictionary withTask: task];
            NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
    
            [dictionary setObject:errResponse forKey:@"error"];
    
            CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary];
            [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }
    }] resume];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      • 2011-10-31
      • 1970-01-01
      • 2014-12-05
      相关资源
      最近更新 更多