...">
美文网首页
号码归属地查询

号码归属地查询

作者: 法库德 | 来源:发表于2017-10-25 14:19 被阅读0次

    #import "ViewController.h"

    <NSURLConnectionDataDelegate>

    {

    //接受服务器端返回的数据

    NSMutableData *myData;

    }

    //手机号码输入框

    @property (weak, nonatomic) IBOutlet UITextField *myNumberTF;

    //查询归属地后的信息展示框

    @property (weak, nonatomic) IBOutlet UITextView *myMessageTV;

    //服务器连接地址

    #define TEST_URL @"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=%@&userID="

    #define POST_URL @"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo"

    //get请求

    - (IBAction)getPhoneNumberMessage:(id)sender {

    //准备好连接字符串

    NSString *str = [NSString stringWithFormat:TEST_URL,self.myNumberTF.text];

    //将字符串类型封装一个连接类型

    NSURL *url = [NSURL URLWithString:str];

    //将连接封装成请求对象

    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];

    #if 0

    //get 请求 同步

    //第一个参数 请求连接对象 客户端-服务器

    //第二个参数 响应连接对象 服务器端的信息 nil

    //第三个参数 错误信息  nil

    NSURLResponse *response = [[NSURLResponse alloc]init];

    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

    //展示信息

    self.myMessageTV.text = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

    #elif 0

    //    //get 请求

    //    //第一个参数 请求连接对象 客户端-服务器

    //    //第二个参数 队列

    //    //第三个参数 代码块(响应连接对象 服务器端的信息 )

    //

    //    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

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

    //

    //        NSLog(@"====%@",response.MIMEType);

    //

    //

    //        //回到主线程更新控件

    //        dispatch_async(dispatch_get_main_queue(), ^{

    //            //展示信息

    //            self.myMessageTV.text = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

    //

    //        });

    //

    //    }];

    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

    NSLog(@"文件类型:%@",response.MIMEType);

    NSLog(@"预计内容长度:%lld",response.expectedContentLength);

    //获取全部字符串

    NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

    //获取输入电话号的位置

    NSRange tmpRange = [str rangeOfString:self.textIF.text];

    //截取掉电话号前的所有内容

    NSString *newStr = [str substringFromIndex:tmpRange.location];

    NSRange newRange = [newStr rangeOfString:@"<"];

    NSString *lastStr = [newStr substringToIndex:newRange.location];

    //回到主线程

    dispatch_async(dispatch_get_main_queue(), ^{

    self.textTV.text = lastStr;

    });

    }];

    #elif 1

    //get 请求 代理

    [NSURLConnection connectionWithRequest:request delegate:self];

    #endif

    }

    //post请求

    - (IBAction)postPhoneNumberMessage:(id)sender {

    NSURL *url = [NSURL URLWithString:POST_URL];

    //封装一个请求对象

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];

    //设置请求方式

    [request setHTTPMethod:@"POST"];

    //设置需要传递的参数

    //mobileCode=string&userID=string

    //参数

    NSString *str = [NSString stringWithFormat:@"mobileCode=%@&userID=",self.myNumberTF.text];

    //转换格式

    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

    //设置需要传递的参数

    [request setHTTPBody:data];

    NSData *myDatas = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    //展示信息

    self.myMessageTV.text = [[NSString alloc]initWithData:myDatas encoding:NSUTF8StringEncoding];

    }

    //请求服务器成功后自动回调 执行一次

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    //初始化

    myData  = [[NSMutableData alloc]init];

    }

    //成功读取服务器端数据后自动回调 执行多次

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    //拼接数据

    [myData appendData:data];

    }

    //请求结束后自动回调

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    //展示信息

    self.myMessageTV.text = [[NSString alloc]initWithData:myData encoding:NSUTF8StringEncoding];

    }

    相关文章

      网友评论

          本文标题:号码归属地查询

          本文链接:https://www.haomeiwen.com/subject/ghyqpxtx.html