http协议:
国内大陆优化(支持ipv6)http://api.ipshudi.com/ipdata/
国际各洲覆盖(部份ipv6)http://api.ip138.com/ipdata/
https协议:
国内大陆优化(支持ipv6)https://api.ipshudi.com/ipdata/
国际各洲覆盖(部份ipv6)https://api.ip138.com/ipdata/
* API接口可能会因为各种网络原因和攻击都可能产生阻断,请开发时做好冗余和异常处理
* 当HTTP请求返回的状态码非200时,请做异常处理,比如 202 状态码造成的原因可能是无效Token、余额不足、格式错误
Object-C(ios)语言调用iP查询接口示例:
//
// ViewController.m
// ApiTest
//
// Created by administrator on 16/9/8.
// Copyright © 2016年 star. All rights reserved.
//
#import "ViewController.h"
#define Host @"https://api.ip138.com/ipdata/"
@interface ViewController ()<NSXMLParserDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString * token = @"859476648b3de65d7680494506dd1a1c6a";
[self executeNetworkWithIP:@"8.8.8.8" dateType:@"xml" callBack:@"" token:token];
}
/**
*示例https://api.ip138.com/ipdata/?ip=8.8.8.8&datatype=jsonp&callback=&token=859476648b3de65d7680494506dd1a1c6a
*参数说明:
*1. ip string ip地址 例如 117.25.13.123(可选,默认为请求者iP)
*2. datatype string txt|jsonp|xml(可选,默认为jsonp)
*3. callback string 回调函数 当前参数仅为jsonp格式数据提供(可选,默认为空)
*4. token string 购买服务后会提供(必填)
*/
- (void)executeNetworkWithIP:(NSString *)ip dateType:(NSString *)dateType callBack:(NSString *)callBack token:(NSString *)token{
NSString * urlString =[NSString stringWithFormat:@"%@?ip=%@&datatype=%@&callback=%@&token=%@",Host,ip,dateType,callBack,token];
//准备网络请求
NSString *newStr = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:newStr];
NSURLRequest *requst = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
//请求异步链接
[NSURLConnection sendAsynchronousRequest:requst queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
NSLog(@"jsonError = %@",connectionError);
return ;
}
//请求格式txt
if ([dateType isEqualToString:@"txt"]) {
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"result = %@",result);
}
//datatype xml
else if ([dateType isEqualToString:@"xml"]){
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"result = %@",result);
}
else{
NSError *jsonError = nil;
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
/*如果设置了回调函数,则可截取后再在转化为NSDictionary,也可直接转化为NSString*/
if (callBack&&[callBack length]>0) {
NSRange range = [jsonString rangeOfString:@"("];
range.location++;
range.length = [jsonString length] - range.location - 1;
jsonString = [jsonString substringWithRange:range ];
}
NSDictionary *jsonResponse =
[NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
options:0
error:&jsonError];
if (jsonError) {
NSLog(@"jsonError = %@",jsonError);
return ;
}
NSLog(@"jsonResponse = %@",jsonResponse);
}
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end