ASIFormDataRequest 網絡請求的簡單封裝之soap協議

DataRequest.h文件

/  Copyright (c) 2013年 dong. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface DataRequest : NSObject
+(void)dataWithArr:(NSMutableArray *)arr andRequestType:(NSString *)requestType andRequestName:(NSString *)requestName andBlock:(void(^)(NSString *requestStr))block;
+(NSMutableDictionary *)jsonValue :(NSString *)str;
@end


DataRequest.m 文件

//  Copyright (c) 2013年 dong. All rights reserved.
//
//add into  .pch  這裏是隨便寫的請求地址,
#define WEBSERVER @"https://XXXXXXXXXXXXXX"
#define KEYVALUE @"XXXXXXXXXXXXXX"

#import "DataRequest.h"
#import "ASIFormDataRequest.h"
#import "GDataXMLNode.h"
@implementation DataRequest
+(void)dataWithArr:(NSMutableArray *)arr andRequestType:(NSString *)requestType andRequestName:(NSString *)requestName andBlock:(void(^)(NSString *requestStr))block{
    
    NSString * str = @"";
    for (int i = 0; i< [arr count]; i++) {
        str = [str stringByAppendingString:[NSString stringWithFormat:@"<request%d>%@</request%d>",i,[arr objectAtIndex:i],i]];
    }
    NSString *soapMsg = [NSString stringWithFormat:
                         @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                         "<soap12:Envelope "
                         "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
                         "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
                         "xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"
                         "<soap12:Body>"
                         "<%@ xmlns=\"http://WebXml.com.cn/\">"
                         "<key>%@</key>"
                         "%@"
                         "</%@>"
                         "</soap12:Body>"
                         "</soap12:Envelope>",requestName,KEYVALUE,str,requestName];
    NSURL *url = [NSURL URLWithString: WEBSERVER];
    ASIFormDataRequest * request=[ASIFormDataRequest requestWithURL:url];
    [request setRequestMethod:requestType];
    [request appendPostData:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
    [request addRequestHeader:@"Content-Type" value:@"application/soap+xml; charset=utf-8"];
    NSLog(@"%@",soapMsg);

    [request setRequestMethod:requestType];
    
    [request setCompletionBlock:^{
        //xml解析
        GDataXMLDocument * doc=[[GDataXMLDocument alloc]initWithData:[request responseData] options:0 error:nil];
        //節點路徑
        NSString *path=[NSString stringWithFormat:@"/env:Envelope/env:Body/ns1:%@Response",requestName];
        //獲得指定條件的所有子節點
        NSArray *array=[doc nodesForXPath:path error:nil];
        NSString *responseStr = nil;
        for (GDataXMLElement *element in array) {
            //取得return節點的內容
            NSArray * titleArray=[element elementsForName:@"return"];
            if ([titleArray count] != 0) {
                GDataXMLElement * ele=[titleArray objectAtIndex:0];
                //節點內容字符串
                responseStr=[ele stringValue];
            }
        }
        block(responseStr);
    }];
    
    [request setFailedBlock:^{
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"請求失敗" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil];
        [alert show];
        [alert release];
        block(@"failed");
    }];
    
    [request startAsynchronous];
}
+(NSMutableDictionary *)jsonValue :(NSString *)str{
    //json解析
    NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];
    return [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
}

@end

調用:

 NSMutableArray *muArr=[[NSMutableArray alloc] initWithObjects:@"X",@"XX",@"XX", nil];
    [DataRequest dataWithArr:muArr andRequestType:@"POST" andRequestName:@"XX" andBlock:^(NSString *requestStr) {
        NSLog(@"%@",[DataRequest jsonValue:requestStr]);
    }];


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章