`
zjjzmw1
  • 浏览: 1366162 次
  • 性别: Icon_minigender_1
  • 来自: 开封
社区版块
存档分类
最新评论

自己总结的ASIHTTPRequestDelegate

    博客分类:
  • iOS
ios 
阅读更多
#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:@"018@2x.png"];
            [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;
}
分享到:
评论

相关推荐

    httprequest

    - `ASIHTTPRequestDelegate.h`: 委托协议。 - `ASIProgressDelegate.h`: 进度监听协议。 - `ASICacheDelegate.h`: 缓存协议。 - `ASIHTTPRequest.h`: 主要接口类。 - `ASIHTTPRequest.m`: 实现文件。 - `...

    ASIHttpRequest库API的介绍和使用

    为了更复杂的任务管理,如定义下载队列或控制并发数,开发者需要创建自己的`NSOperationQueue`或使用`ASINetworkQueue`。这些高级特性使得ASIHttpRequest库非常适合处理大规模的数据交换和复杂的网络通信场景。 ...

    ASIHttpRequest

    通过实现ASIHTTPRequestDelegate协议,开发者可以监听请求的状态变化,如开始、结束、失败等,并做出相应的处理。 除了基本的HTTP请求功能,ASIHttpRequest还支持文件上传和下载。例如,你可以使用它来上传图片、...

    ASIHTTP资源包

    4. **处理响应**:可以监听请求的状态,例如通过实现`ASIHTTPRequestDelegate`中的方法`requestDidFinish:`和`requestDidFail:`来处理成功和失败的情况。 5. **进度反馈**:可以使用`setDownloadProgressDelegate:`...

    iOS系统同步异步下载 ASI同步异步下载图片源代码

    extension YourViewController: ASIHTTPRequestDelegate { func requestDidFinish(_ request: ASIHTTPRequest) { if let imageData = request.responseData { // 下载成功,处理数据 } } func request(_ ...

    ASIHTTPRequest第三方下载工具

    - 通过实现`ASIHTTPRequestDelegate`协议的方法,如`requestDidFinish:`和`requestDidFail:`,可以处理成功和失败的响应。 - 使用`appendData:`或`setPostBody:`发送POST请求的数据。 5. **进度追踪**: ...

    ASIHTTP demo

    同时,通过实现`ASIHTTPRequestDelegate`协议,可以监听请求的状态,如成功、失败、取消等。 由于ASIHTTP库已不再更新,开发者现在更倾向于使用AFNetworking或Alamofire等现代网络库,它们提供了更强大的功能和更好...

    ASIHTTPRequest使用介绍

    1. **ASIHTTPRequestDelegate**:定义了ASIHTTPRequest代理可能需要实现的方法,如请求完成、失败等回调。 2. **ASIProgressDelegate**:规定了上传和下载进度委托所需实现的方法,用于监听上传和下载进度。 3. **...

    IOS应用源码——pokeb-asi-http-request-v1.6.2-0-g3cbd8d6.rar

    在实际使用中,开发者可以通过继承或扩展ASIHTTPRequest类来自定义行为,或者实现ASIHTTPRequestDelegate协议来监听请求状态,如开始、完成、失败等事件。 ASIHTTPRequest的亮点在于其灵活性和易用性,但也有其局限...

    ASIHttpRequest.rar

    @interface YourViewController () &lt;ASIHTTPRequestDelegate&gt; @end - (void)requestFinished:(ASIHTTPRequest *)request { // 请求成功处理 } - (void)requestFailed:(ASIHTTPRequest *)request { NSError *...

    ASIHTTPRequest常用类包

    当请求完成时,可以实现`ASIHTTPRequestDelegate`协议的方法来处理响应,例如: ```objective-c - (void)requestFinished:(ASIHTTPRequest *)request { NSString *responseString = [request responseString]; ...

    ASIHTTPRequest

    5. **处理响应**:实现`ASIHTTPRequestDelegate`协议中的方法,如`requestDidFinish:`和`requestDidFail:`,以便在请求完成或失败时得到通知。 6. **获取数据**:在请求成功后,可以使用`[request responseData]`...

    ASIHTTPRequest使用指南(中文版)

    - **概念**:通过实现ASIHTTPRequestDelegate协议的方法来处理多个请求的结果。 - **示例代码**:实现`requestFinished:`或`requestFailed:`方法。 - **优点**:更灵活地处理请求结果。 ##### 2.6 关于...

    asihttprequest使用指南

    @property (weak) id&lt;ASIHTTPRequestDelegate&gt; delegate; ``` #### 三、发送数据 ##### 2.1 发送请求头 可以通过`setRequestHeader`方法添加自定义的请求头。 ```objective-c [request setRequestHeader:@...

Global site tag (gtag.js) - Google Analytics