使用afnetworking和网络服务接口及soap发送并接收http/https请求:

来源:互联网 发布:cpa软件 编辑:程序博客网 时间:2024/05/01 21:42

使用afnetworking和网络服务接口及soap发送并接收http/https请求:

  //1.创建请求体(格式和服务器端要求发送的一致,将参数替换成自己要传的参数,name,psw)

    NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><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/\"><soap:Body><GetUserInfo xmlns=\"http://tempuri.org/\"><username>%@</username><pwd>%@</pwd></GetUserInfo></soap:Body></soap:Envelope>",name,psw];

    //2.获取请求体的长度

    NSString *soapLength = [NSString stringWithFormat:@"%ld",[soapMessage length]];

    

    

    

//    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:[NSURL URLWithString:@"http://tempuri.org"]];


    //3.创建一个AFHTTPRequestOperationManager对象,设置属性

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    //3.1设置返回的格式为http(data数据)

    manager.responseSerializer = [[AFHTTPResponseSerializer alloc]init];

    

    

    //4.设置请求头信息

    [manager.requestSerializer setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    [manager.requestSerializer setValue:soapLength forHTTPHeaderField:@"Content-Length"];

    //创建一个nsurlrequest对象

    NSMutableURLRequest *request = [manager.requestSerializer requestWithMethod:@"POST" URLString:@"http://192.168.0.10:8018/webservice/WebService.asmx" parameters:nil];

    //设置request对象的请求体编码格式

    [request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    //创建一个队列

    AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {

    //发送成功并接收到的数据信息—->responseObject

        NSString *response = [[NSString alloc] initWithData:(NSData *)responseObject encoding:NSUTF8StringEncoding];

        NSLog(@"%@, ------%@", operation, response);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSString *response = [[NSString alloc] initWithData:(NSData *)[operation responseObject] encoding:NSUTF8StringEncoding];

        NSLog(@"%@, %@", response, error);

    }];

    [manager.operationQueue addOperation:operation];

0 0
原创粉丝点击