自己总结的ASIHTTPRequestDelegate

#import "ASIHTTPRequest.h"
#import "ASINetworkQueue.h"

@interface DomainCheckViewController : UIViewController<UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate,ASIHTTPRequestDelegate>
@property(retain,nonatomic)ASINetworkQueue*queCheck;



-(void)sendRequestByQueue
{
    //built queue
    if (!self.queCheck)
    {
        self.queCheck=[[ASINetworkQueue alloc]init];
    }
    self.queCheck.delegate=self;
    [queCheck setShouldCancelAllRequestsOnFailure:NO];
   
    //all request use params
    NSDictionary *dicDomainName=[NSDictionary dictionaryWithObjectsAndKeys:self.doNameSearch.text,@"domainname", nil];
   
    //get domain state ,domain visit state
    NSString*ss1=[NSString stringWithFormat:@"%@_%@1",TOKEN,[self timestamp]];
   
    NSDictionary *paramsState = [NSDictionary dictionaryWithObjectsAndKeys:
                                 dicDomainName,@"data",
                                 @"1.0",@"v",
                                 ss1,@"trid",
                                 @"wwwrecord",@"method",
                                 @"ios",@"client",nil];
   
    NSString*stringState=[NSString stringWithFormat:@"http://hiapp.hichina.com:8080/hiapp/json/wwwrecord/"];
    NSURL *urlState = [NSURL URLWithString:stringState];
    ASIFormDataRequest*requestState=[ASIFormDataRequest requestWithURL:urlState];
    requestState.requestMethod=@"POST";
    [requestState setPostValue:paramsState forKey:@"req"];
    //get domainname's DNS check
    NSString*ss2=[NSString stringWithFormat:@"%@_%@2",TOKEN,[self timestamp]];
    NSDictionary *paramsDNS = [NSDictionary dictionaryWithObjectsAndKeys:
                               dicDomainName,@"data",
                               @"1.0",@"v",
                               @"whois",@"method",
                               ss2,@"trid",
                               @"ios",@"client",nil];
   
   
    NSString*stringDNS=[NSString stringWithFormat:@"http://hiapp.hichina.com:8080/hiapp/json/whois/"];
    NSURL *urlDNS = [NSURL URLWithString:stringDNS];
    ASIFormDataRequest*requestDNS=[[ASIFormDataRequest alloc]initWithURL:urlDNS];
    [requestDNS setRequestMethod:@"POST"];
    [requestDNS setPostValue:paramsDNS forKey:@"req"];
   
    //get unregistered domainname
    NSString*ss3=[NSString stringWithFormat:@"%@_%@3",TOKEN,[self timestamp]];
    NSDictionary *paramsUnregis = [NSDictionary dictionaryWithObjectsAndKeys:
                                   dicDomainName,@"data",
                                   @"1.0",@"v",
                                   @"nodomainname",@"method",
                                   ss3,@"trid",
                                   @"ios",@"client",nil];
   
   
    NSString*stringUnregis=[NSString stringWithFormat:@"http://hiapp.hichina.com:8080/hiapp/json/noDomainName/"];
    NSURL *urlUnregis = [NSURL URLWithString:stringUnregis];
    ASIFormDataRequest *requestUnregis=[[ASIFormDataRequest alloc]initWithURL:urlUnregis];
    [requestUnregis setRequestMethod:@"POST"];
    [requestUnregis setPostValue:paramsUnregis forKey:@"req"];//this way set value default POST
   
    //get select domainname
    NSDictionary*dic;
    if ([self chineseOrEnglish:self.doNameSearch.text])//is english
    {
        dic=[NSDictionary dictionaryWithObjectsAndKeys:self.doNameSearch.text,@"domainnames", nil];
    }
    else
    {
        dic=[NSDictionary dictionaryWithObjectsAndKeys:[self.doNameSearch.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],@"domainnames", nil];
    }
    NSString*ss4=[NSString stringWithFormat:@"%@_%@4",TOKEN,[self timestamp]];
    NSDictionary *paramsSelect = [NSDictionary dictionaryWithObjectsAndKeys:
                            dic,@"data",
                            @"1.0",@"v",
                            @"checkdomain",@"method",
                            @"news",@"module",
                            ss4,@"trid",
                            @"ios",@"client",nil];
   
    NSString*stringSelect=[NSString stringWithFormat:@"http://hiapp.hichina.com/hiapp/json/checkdomain/"];
    NSURL *urlSelect = [NSURL URLWithString:stringSelect];
    ASIFormDataRequest*requestSelect=[[ASIFormDataRequest alloc]initWithURL:urlSelect];
    [requestSelect setPostValue:paramsSelect forKey:@"req"];

   
   
    //put all request to my queue
    [self.queCheck addOperation:requestState];
    [self.queCheck addOperation:requestDNS];
    [self.queCheck addOperation:requestUnregis];
    [self.queCheck addOperation:requestSelect];
    //assign all request uinque dictionary
    requestState.userInfo=[NSDictionary dictionaryWithObject:@"state" forKey:@"name"];
    requestDNS.userInfo=[NSDictionary dictionaryWithObject:@"dns" forKey:@"name"];
    requestUnregis.userInfo=[NSDictionary dictionaryWithObject:@"unregis" forKey:@"name"];
    requestSelect.userInfo=[NSDictionary dictionaryWithObject:@"select" forKey:@"name"];
    //set delegate method finish or fail for queue
    [queCheck setRequestDidFinishSelector:@selector(doFinish:)];
    [queCheck setRequestDidFailSelector:@selector(doFail:)];
    [queCheck setQueueDidFinishSelector:@selector(doAllQueue:)];
    [queCheck go];
}





-(void)doFinish:(ASIFormDataRequest*)sender
{
//    NSString*requestName=[[sender userInfo] objectForKey:@"name"];
    if ([[[sender userInfo] objectForKey:@"name"] isEqualToString:@"state"])//state request data
    {
        NSMutableString *receiveStr = [[NSMutableString alloc]
                                       initWithData:[sender responseData] encoding:NSUTF8StringEncoding];
        NSLog(@"dicsta====%@",receiveStr);
        NSDictionary*dicJson=[receiveStr JSONValue];
        NSDictionary*dicResault=[dicJson objectForKey:@"results"];
        //domain name state
        if ([[dicResault objectForKey:@"wwwstatus"] intValue]==1)//normarl
        {
            [self.domainState setTitle:@"域名状态:正常" forState:UIControlStateNormal];
            [self.domainState setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        }
        else
        {
            [self.domainState setTitle:@"域名状态:异常" forState:UIControlStateNormal];
            [self.domainState setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
        //domain name visit state
        NSArray*arrVisit=[dicResault objectForKey:@"infos"];
        [self.visitDetail setTitle:[dicResault objectForKey:@"infos"] forState:UIControlStateSelected];
        [self.domainVisit setTitle:@"域名访问状态:正常" forState:UIControlStateNormal];
        [self.domainVisit setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        for (int i=0; i<[arrVisit count]; i++)
        {
            NSDictionary*dicvisit=[arrVisit objectAtIndex:i];
            if ([[dicvisit objectForKey:@"status"] intValue]!=1)//all visit state must be 1
            {
                [self.domainVisit setTitle:@"域名访问状态:异常" forState:UIControlStateNormal];
                [self.domainVisit setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                break;
            }
        }
    }
    if ([[[sender userInfo] objectForKey:@"name"] isEqualToString:@"dns"])//DNS request data
    {
        NSMutableString *receiveStr = [[NSMutableString alloc]
                                       initWithData:[sender responseData] encoding:NSUTF8StringEncoding];
        NSLog(@"dicdns====%@",receiveStr);
        NSDictionary*dicJson=[receiveStr JSONValue];
        NSDictionary*dicResault=[dicJson objectForKey:@"results"];
        NSDictionary*dicWhois=[dicResault objectForKey:@"whois"];
       
        [self.stateDetail setTitle:[dicWhois objectForKey:@"domainstatus"] forState:UIControlStateSelected];
        [self.DNSDetail setTitle:[dicWhois objectForKey:@"dnsserver"] forState:UIControlStateSelected];
        if ([[dicWhois objectForKey:@"dnsserver"] isKindOfClass:[NSString class]])//is nsstring normal
        {
            [self.domainDNS setTitle:@"域名DNS状态:正常" forState:UIControlStateNormal];
            [self.domainDNS setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        }
        else
        {
            [self.domainDNS setTitle:@"域名DNS状态:异常" forState:UIControlStateNormal];
            [self.domainDNS setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
    }
    if ([[[sender userInfo] objectForKey:@"name"] isEqualToString:@"unregis"])//unregistered request data
    {
        NSMutableString *receiveStr = [[NSMutableString alloc]
                                       initWithData:[sender responseData] encoding:NSUTF8StringEncoding];
        NSLog(@"dicunresgit====%@",receiveStr);
        NSDictionary*dicresault=[[receiveStr JSONValue] objectForKey:@"results"];
        NSString*strUnregist=[dicresault objectForKey:@"notdomain"];
        NSLog(@"uuuuuuuu======%@",strUnregist);
        if ([strUnregist length])
        {
            [self.domainUnReg setTitle:[NSString stringWithFormat:@"有%d个未注册的域名",[[strUnregist componentsSeparatedByString:@","] count]] forState:UIControlStateNormal];
            [self.domainUnReg setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
            [self.regDetail setTitle:strUnregist forState:UIControlStateSelected];
        }
        else
        {
            [self.domainUnReg setTitle:@"无未注册域名" forState:UIControlStateNormal];
            [self.domainUnReg setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
    }
    if ([[[sender userInfo] objectForKey:@"name"] isEqualToString:@"select"])
    {
        NSMutableString *receiveStr = [[NSMutableString alloc]
                                       initWithData:[sender responseData] encoding:NSUTF8StringEncoding];
        NSLog(@"dicselect====%@",receiveStr);
        //move down in the screen
    [UIView animateWithDuration:0.5 animations:^{
        self.domainResault.alpha=1;
        self.btuBackImage.frame=CGRectMake(0, 200, 320, 300);
    }];
        NSDictionary*dicJson=[receiveStr JSONValue];
        NSDictionary*dicresault=[dicJson objectForKey:@"results"];
        NSArray*arrDomain=[dicresault objectForKey:@"domainnames"];
        if ([[[arrDomain objectAtIndex:0] objectForKey:@"status"] intValue]==1)//the domain name have be registed
        {
            [self.resaultOn setTitle:@"此域名已被注册" forState:UIControlStateNormal];
            self.face.image=[UIImage imageNamed:@"[email protected]"];
            [self.btu1 setImage:[UIImage imageNamed:@"detailbutton.png"] forState:UIControlStateNormal];
            [self.btu1 setTitle:self.doNameSearch.text forState:UIControlStateSelected];
            [self.btu2 setImage:[UIImage imageNamed:@"监控按钮.png"] forState:UIControlStateNormal];
        }
        else
        {
            [self.resaultOn setTitle:@"此域名未被注册" forState:UIControlStateNormal];
            self.face.image=[UIImage imageNamed:@"smile.png"];
            [self.btu1 setImage:[UIImage imageNamed:@"buycar.png"] forState:UIControlStateNormal];
            [self.btu2 setImage:[UIImage imageNamed:@"收藏.png"] forState:UIControlStateNormal];
            [self.btu2 setTitle:self.doNameSearch.text forState:UIControlStateSelected];
        }
    }
}


-(void)doFail:(ASIHTTPRequest*)sender
{
    NSLog(@"%@",[sender error]);
}

-(void)doAllQueue:(ASINetworkQueue*)sender
{
    NSLog(@"all queue finish");
}



- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    if (self.englishButton.selected) {
        NSString *match =@"^([a-z0-9]+\\.?(?:com|cn|mobi|co|net|so|org|gov|tel|tv|biz|cc|hk|name|info|asia|me)?(?:\\.?(?:cn))?)$";
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",match];
        if(![predicate evaluateWithObject:textField.text]){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"对不起,您输入的域名格式有误\n请重新输入!\n\n例如:abc/abc.com" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }

    } else {
        //[\u4e00-\u9fa5]中文正则表达式
        //@".com",@".net",@".tv",@".biz",@".cc",@".公司",@".网络",@".中国"
        NSString *match =@"^([a-z0-9]*[\u4e00-\u9fa5]+[a-z0-9]*\\.?(?:com|net|tv|biz|cc|公司|网络|中国)?)$";
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",match];
        if(![predicate evaluateWithObject:textField.text]){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"对不起,您输入的域名格式有误\n请重新输入!\n\n例如:万网/万网.com" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }
    }
    return YES;
}

你可能感兴趣的:(ios)