如何将XML字符串作为afnetworking中的请求中的参数发送



这是我的请求url:

  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>"];

我通过在浏览器中输入URL尝试/测试。但它说有些参数缺少。(但在那里)。然后我试图清洁下面的URL。

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

当时它返回404错误。

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

如果不是,那是什么替代方法。

注意:Web服务是使用SOAP创建的,并将XML结果转换为JSON,并将JSON返回为响应

希望您对此有帮助!!!

尝试以下代码。

 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];

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

使用以下:

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()

相关内容

  • 没有找到相关文章

最新更新