afnetworking设置请求头代码

来源:互联网 发布:免费下载苹果软件 编辑:程序博客网 时间:2024/06/02 02:09

 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", WebURL]]];


    [request setCachePolicy:NSURLRequestReloadIgnoringCacheData];


    [request setTimeoutInterval30];


    NSString*msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];


    [request addValue@"text/xml; charset=utf-8"forHTTPHeaderField:@"Content-Type"];


    [request addValue:@"IOS App (power by elliott)" forHTTPHeaderField:@"User-Agent"];


    [request addValue:soapActionURL forHTTPHeaderField:@"SOAPAction"];


    [request addValue: msgLength forHTTPHeaderField:@"Content-Length"];


    [request setHTTPMethod:@"POST"];


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


AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation allocinitWithRequest:request];


    // Make sure to set the responseSerializer correctly


    //operation.responseSerializer = [AFXMLParserResponseSerializer serializer];


    //


    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {


        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)operation.response;


        if (httpResponse.statusCode == 200) {


            dispatch_async(dispatch_get_main_queue(), ^{


                NSXMLParser *xmlParser = [[NSXMLParser allocinitWithData:responseObject];


                


                //[xmlParser setShouldProcessNamespaces:NO];


                //[xmlParser setShouldReportNamespacePrefixes:NO];


                //[xmlParser setShouldResolveExternalEntities:NO];


                [xmlParser setDelegate:myhttp];


                [xmlParser parse];


                [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;


            });


        }


        


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


        DLog(@"errror: %@", error);


        dispatch_async(dispatch_get_main_queue(), ^{


            if (_delegate && [_delegate respondsToSelector:@selector(updateVCwithNetError: method:)]) {


                [_delegate updateVCwithNetError:@"请求出错" method:myhttp.soapMethod];


            }


            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;


        });


        




    }];

1 0
原创粉丝点击