如何在AFNetworking的请求中发送xml字符串作为参数

时间:2016-06-16 11:51:50

标签: ios objective-c xml parameters afnetworking

这是我的请求网址:

  NSString *checkingbaseUrl = [NSString stringWithFormat:@"http://mysonapi.ascentur.au/auMobile.asmx/FlightBooking?Authkey=%@&Reqxml=%@&infovia=%@&totalpax=%@&totalfare=%@&flightsegmentcount=%@&from=%@&destination=%@&departuredate=%@&returndate=%@&airlinecode=%@&cabinclass=%@&cabinclasses=%@",authkey,newrequstString,infovia,totalPax,totalfare,flightsegmentcount,from,destination,departuredate,returndate,airline,cabinclass,cabinClassess];

这就是我使用AFNetworking的方式

  AFHTTPSessionManager *bookingmanager = [AFHTTPSessionManager manager];
    [bookingmanager GET:basedUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
    {
        NSString *responseS = (NSString *)responseObject;
        NSLog(@"resoponseS : %@", responseS);


    }
    failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"anurdh_caldera :%@", error.localizedDescription);
    }];

这是我想通过xml作为参数传递的xml字符串。

NSString *requestXml = [NSString stringWithFormat:@"<BPC9>"
                                "<MSG_VERSION>9</MSG_VERSION>"
                                "<VARIABLE_COUNT>0002</VARIABLE_COUNT>"
                                "<SEATS>2</SEATS>"
                                "<PASSENGER_DATA>"
                                "<NAME_POSITION>1</NAME_POSITION>"
                                "<LAST_FIRST_MIDDLE>Romeo/Jayceon</LAST_FIRST_MIDDLE>"
                                "<TITLE>Mr</TITLE>"
                                "<DOB>900605</DOB>"
                                "<TYPE>ADT</TYPE>"
                                "</PASSENGER_DATA>"
                                "<PASSENGER_DATA>"
                                "<NAME_POSITION>2</NAME_POSITION>"
                                "<LAST_FIRST_MIDDLE>Romeo/Zion</LAST_FIRST_MIDDLE>"
                                "<TITLE>Mr</TITLE>"
                                "<DOB>950712</DOB>"
                                "<TYPE>ADT</TYPE>"
                                "</PASSENGER_DATA>"
                                "<PNR_DATA>"
                                "<PHONE_FIELDS>"
                                "<PHONE_FIELD>mobile api travel</PHONE_FIELD>"
                                "</PHONE_FIELDS>"
                                "<RECEIVED_FROM>traavel center mobile</RECEIVED_FROM>"
                                "<CUST_REF_NUM>166201683652250</CUST_REF_NUM>"
                                "<FOP>CA</FOP>"
                                "</PNR_DATA>"
                                "<QUEUE_DATA>"
                                "<QUEUE_NUMBER>0</QUEUE_NUMBER>"
                                "</QUEUE_DATA>"
                                "<END_OPTION>E</END_OPTION>"
                                "<PRICING_DISPLAY_OPTION>1</PRICING_DISPLAY_OPTION>"
                                "<PRICING_COMMAND>4P*FSR</PRICING_COMMAND>"
                                "<PRICING_COMMAND/>"
                                "<PRICING_COMMAND/>"
                                "<PRICING_COMMAND>#FSR</PRICING_COMMAND>"
                                "<TICKET_OPTION>7T/</TICKET_OPTION>"
                                "<LEG_COUNT>1</LEG_COUNT>"
                                "<CK_TIME_PRICE_DIF>T</CK_TIME_PRICE_DIF>"
                                "<SEGMENT_INFO>"
                                "<LEG_NUM>1</LEG_NUM>"
                                "<SEGMENT_INDICATOR>P</SEGMENT_INDICATOR>"
                                "<AVAIL_LOCATION>N</AVAIL_LOCATION>"
                                "<AVAIL_INFO>"
                                "<AVAIL_DEP_AIRPORT>"
                                "<CITY_CODE>LHR</CITY_CODE>"
                                "</AVAIL_DEP_AIRPORT>"
                                "<AVAIL_AIRLINE>AF</AVAIL_AIRLINE>"
                                "<AVAIL_DEP_CLASS>X</AVAIL_DEP_CLASS>"
                                "<AVAIL_ARRIVAL_AIRPORT>"
                                "<CITY_CODE>CDG</CITY_CODE>"
                                "</AVAIL_ARRIVAL_AIRPORT>"
                                "<AVAIL_DEP_DAY>17</AVAIL_DEP_DAY>"
                                "<AVAIL_DEP_MONTH>JAN</AVAIL_DEP_MONTH>"
                                "<AVAIL_DEP_TIME>2010</AVAIL_DEP_TIME>"
                                "<AVAIL_ARRIVAL_TIME>2220</AVAIL_ARRIVAL_TIME>"
                                "</AVAIL_INFO>"
                                "<AIRLINE_CODE>AF</AIRLINE_CODE>"
                                "<FLIGHT_NUM>1181</FLIGHT_NUM>"
                                "<DEP_CLASS>X</DEP_CLASS>"
                                "<DEP_DATE>"
                                "<DEP_DAY>17</DEP_DAY>"
                                "<DEP_MONTH>JAN</DEP_MONTH>"
                                "</DEP_DATE>"
                                "<DEP_TIME>"
                                "<DEP_HOUR>20</DEP_HOUR>"
                                "<DEP_MIN>10</DEP_MIN>"
                                "</DEP_TIME>"
                                "<DEP_AIRPORT>LHR</DEP_AIRPORT>"
                                "<ARRIVAL_DATE>"
                                "<ARRIV_DAY>17</ARRIV_DAY>"
                                "<ARRIV_MONTH>JAN</ARRIV_MONTH>"
                                "</ARRIVAL_DATE>"
                                "<ARRIVAL_TIME>"
                                "<ARRIV_HOUR>22</ARRIV_HOUR>"
                                "<ARRIV_MIN>20</ARRIV_MIN>"
                                "</ARRIVAL_TIME>"
                                "<ARRIV_AIRPORT>CDG</ARRIV_AIRPORT>"
                                "</SEGMENT_INFO>"
                                "<SEGMENT_INFO>"
                                "<LEG_NUM>1</LEG_NUM>"
                                "<SEGMENT_INDICATOR>P</SEGMENT_INDICATOR>"
                                "<AVAIL_LOCATION>N</AVAIL_LOCATION>"
                                "<AVAIL_INFO>"
                                "<AVAIL_DEP_AIRPORT>"
                                "<CITY_CODE>CDG</CITY_CODE>"
                                "</AVAIL_DEP_AIRPORT>"
                                "<AVAIL_AIRLINE>AF</AVAIL_AIRLINE>"
                                "<AVAIL_DEP_CLASS>X</AVAIL_DEP_CLASS>"
                                "<AVAIL_ARRIVAL_AIRPORT>"
                                "<CITY_CODE>LHR</CITY_CODE>"
                                "</AVAIL_ARRIVAL_AIRPORT>"
                                "<AVAIL_DEP_DAY>31</AVAIL_DEP_DAY>"
                                "<AVAIL_DEP_MONTH>JAN</AVAIL_DEP_MONTH>"
                                "<AVAIL_DEP_TIME>1805</AVAIL_DEP_TIME>"
                                "<AVAIL_ARRIVAL_TIME>1820</AVAIL_ARRIVAL_TIME>"
                                "</AVAIL_INFO>"
                                "<AIRLINE_CODE>AF</AIRLINE_CODE>"
                                "<FLIGHT_NUM>1080</FLIGHT_NUM>"
                                "<DEP_CLASS>X</DEP_CLASS>"
                                "<DEP_DATE>"
                                "<DEP_DAY>31</DEP_DAY>"
                                "<DEP_MONTH>JAN</DEP_MONTH>"
                                "</DEP_DATE>"
                                "<DEP_TIME>"
                                "<DEP_HOUR>18</DEP_HOUR>"
                                "<DEP_MIN>05</DEP_MIN>"
                                "</DEP_TIME>"
                                "<DEP_AIRPORT>CDG</DEP_AIRPORT>"
                                "<ARRIVAL_DATE>"
                                "<ARRIV_DAY>31</ARRIV_DAY>"
                                "<ARRIV_MONTH>JAN</ARRIV_MONTH>"
                                "</ARRIVAL_DATE>"
                                "<ARRIVAL_TIME>"
                                "<ARRIV_HOUR>18</ARRIV_HOUR>"
                                "<ARRIV_MIN>20</ARRIV_MIN>"
                                "</ARRIVAL_TIME>"
                                "<ARRIV_AIRPORT>LHR</ARRIV_AIRPORT>"
                                "</SEGMENT_INFO>"
                                "</BPC9>"];

我通过在浏览器中输入网址来尝试/测试。但是它说缺少一些参数。(但是就在那里)。然后我试着像下面那样清理网址。

NSString *basedUrl = [checkingbaseUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];

此时它会返回404错误。

我想知道是否可以通过url将此类型的xml字符串作为参数传递,并且可以使用AFNetworking。如果有可能我该怎么做。

如果不是,可以采取哪些替代方法。

注意:使用soap创建Web服务并将xml结果转换为json并将json作为响应返回

希望你对this.cheers的帮助!!!!

2 个答案:

答案 0 :(得分:0)

尝试以下代码。

 NSString *urn = @""; //Your URL

NSString *soapMessage = [NSString stringWithFormat:
                         @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                         "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
                         "<soap:Body>\n"
                         "<%@ %@>\n"
                         "%@"
                         "</%@>\n"
                         "</soap:Body>\n"
                         "</soap:Envelope>\n"
                         ,method, namespace, params, method];


NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
NSString *msgLength = [NSString stringWithFormat:@"%ld", (long)[soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: [NSString stringWithFormat:@"%@/%@",urn,method] forHTTPHeaderField:@"soapaction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest];

operation.responseSerializer = [AFXMLParserResponseSerializer serializer];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    //parse NSXMLParser object here if request successfull
    NSError *error;
    if ([responseObject isKindOfClass:[NSXMLParser class]]) {
        NSXMLParser *parser = (NSXMLParser *)responseObject;
        NSDictionary *dict = [XMLReader dictionaryForNSXMLParser:parser error:&error];
        success(dict);
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    failure(error);
}];

[[NSOperationQueue mainQueue] addOperation:operation];

根据您的要求编辑肥皂信息。

答案 1 :(得分:0)

使用此:

let xmlString = "<regNumber>123</regNumber><pinNumber>234</pinNumber>"
let xmlData = xmlString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let request = NSMutableURLRequest(URL: NSURL(string: "serviceurl")!)
request.HTTPMethod = "POST"
request.HTTPBody = xmlData

let operation = AFHTTPRequestOperation(request: request);

operation.setCompletionBlockWithSuccess({ (AFHTTPRequestOperation operation, AnyObject response) -> Void in
    //Response

}, failure: { (AFHTTPRequestOperation operation, NSError    error) -> Void in
    println(error.localizedDescription)
})
operation.start()