[VC6 console]调用API获取手机归属地
为了完成作业,就偷个懒糊了个获取手机归属地的程序,。我原本写的是MFC版本的,但是由于MFC的代码不是很通用,加上我没有学MFC的时候看别人MFC代码只能干瞪眼,看不懂,所以便改成控制台版本的了。但这API还害得我找了老半天,不是功能少就是根本用不了(例如youdao)文中所用的API地址是 http://api.showji.com/Locating/www.showji.co.m.aspx,有两个参数:
m | 手机号,由11位数字组成 |
output | 可以为xml或json |
注:浏览器显示可能有些问题,毕竟<Mobile>等标签浏览器是无法识别的,可以在“查看源文件”中查看
我使用的是xml格式。json解析可以用jsoncpp,就是不知道怎么在VC6下使用,若您了解,还请你回复讲解。
思路很简单,URLDownLoadToFile()下载文件,再写个函数解析,好,开始!
// 头文件
#include <stdio.h>
#include <windows.h>
#include <urlmon.h>
#pragma comment( lib, "urlmon.lib" )
// 为了复制粘贴方便,写了2个宏
#define GETEND( str ) (SreachStr( MobileInfo, str ) + strlen( str ))
#define GETSTART( str ) (SreachStr( MobileInfo, str ) - 1)
// 函数Utf8_to_ascii,API大多都是UTF8格式的,要用此函数转换成ascii
char *Utf8_to_ascii( char utf8[] )
{
int len = MultiByteToWideChar( CP_UTF8, 0, utf8, -1, NULL, 0);
wchar_t *wstr = new wchar_t[len+1];
memset( wstr, 0, len + 1);
MultiByteToWideChar( CP_UTF8, 0, utf8, -1, wstr, len);
len = WideCharToMultiByte( CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
char *str = new char[len+1];
memset(str, 0, len+1);
WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
if( wstr )
{
delete[] wstr;
}
return str;
}
// 函数SreachStr,寻找API中间<...>...</...>的数据
int SreachStr( const char str1[], const char str2[] )
{
int i = 0;
int j = 0;
int len1 = strlen( str1 );
int len2 = strlen( str2 );
for( i = 0; i < len1; i++ )
{
bool Findit = true;
for( j = i; j < len2 + i; j++ )
{
if( str2[j-i] != str1[j] )
{
Findit = false;
}
}
if( Findit )
{
return i;
}
}
return -1;
}
主函数似乎太长了点……
int main( int argc, char *argv[] )
{
char Mobilenum[11];
HRESULT hDownToFile; // 文件下载句柄
HANDLE hMobileFile; // 手机信息句柄
char chUrl[100];
unsigned long nReadSize = 0;
memset( Mobilenum, 0, 11 );
memset( chUrl, 0, 100 );
scanf( "%s", Mobilenum ); strcpy( chUrl, "http://api.showji.com/Locating/www.showji.co.m.aspx?m=");
strcat( chUrl, Mobilenum );
strcat( chUrl, "&output=xml" );
hDownToFile = URLDownloadToFile( 0, chUrl, TEXT("Temp File.tmp"), NULL, NULL );
if( hDownToFile != S_OK )
{
printf( "下载失败!\n" );
return 0;
}
hMobileFile = CreateFile( "Temp File.tmp", // 文件名
GENERIC_READ, // 读文件操作
NULL,
NULL,
OPEN_EXISTING, // 打开文件
FILE_FLAG_DELETE_ON_CLOSE, // 作为临时文件
NULL);
if ( hMobileFile == INVALID_HANDLE_VALUE )
{
printf( "打开文件失败!\n" );
return 0;
}
char ReadData_UTF8[400];
char MobileInfo[400];
memset( ReadData_UTF8, 0, 400);
if ( ReadFile( hMobileFile, ReadData_UTF8, 400, &nReadSize, NULL ) == FALSE )
{
printf( "读取文件失败!\n%d", GetLastError() );
return 0;
}
strcpy( MobileInfo, Utf8_to_ascii(ReadData_UTF8) );
CloseHandle( hMobileFile );
/************************** 读取工作结束 *************************/
/************************** 开始解析数据 *************************/
char temp[30];
int i = 0;
int start = GETEND( "<QueryResult>" );
int end = GETSTART( "</QueryResult>" );
memset( temp, 0, 30);
for( i = start; i <= end; i ++ )
{
temp[i - start] = MobileInfo[i];
}
if( !strcmp( temp, "False") )
{
printf( "号码不存在或暂无数据!\n" );
return 0;
}
start = GETEND( "<City>" );
end = GETSTART( "</City>" );
memset( temp, 0, 30);
for( i = start; i <= end; i ++ )
{
temp[i - start] = MobileInfo[i];
}
printf( "所 在 地: %s\n", temp );
start = GETEND( "<AreaCode>" );
end = GETSTART( "</AreaCode>" );
memset( temp, 0, 30);
for( i = start; i <= end; i ++ )
{
temp[i - start] = MobileInfo[i];
}
printf( "区 号: %s\n", temp );
start = GETEND( "<PostCode>" );
end = GETSTART( "</PostCode>" );
memset( temp, 0, 30);
for( i = start; i <= end; i ++ )
{
temp[i - start] = MobileInfo[i];
}
printf( "邮政编码: %s\n", temp );
start = GETEND( "<Corp>" );
end = GETSTART( "</Corp>" );
memset( temp, 0, 30);
for( i = start; i <= end; i ++ )
{
temp[i - start] = MobileInfo[i];
}
printf( "运 营 商: %s\n", temp );
start = GETEND( "<Province>" );
end = GETSTART( "</Province>" );
memset( temp, 0, 30);
for( i = start; i <= end; i ++ )
{
temp[i - start] = MobileInfo[i];
}
printf( "省 份: %s\n", temp );
return 0;
}
总算完成了。运行界面如图:
[VC6 console]调用API获取手机归属地的更多相关文章
- 在C#中调用API获取网络信息和流量
原文 在C#中调用API获取网络信息和流量 最近一项目中要求显示网络流量,而且必须使用C#. 事实上,调用 IpHlpApi.dll 的 GetIfTable API 可以轻易获得网络信息和网络流量. ...
- [VB.NET]调用API获取/设置键盘按键状态
1.调用GetAsyncKeyState()获取指定按键的状态,GetActiveKey()检索指定范围内的按键状态 2.调用keybd_event()可合成一次击键事件,通常两次击键事件间需要设定时 ...
- [VB.NET][C#]调用API获取或设置键盘按键状态
前言 通过 C# 或 VB.NET,你只需编写少量的代码即可实现一个按键精灵. 第一节 接口 调用系统 API 实现获取或设置指定的按键状态. 获取按键状态 调用 GetAsyncKeyState() ...
- java 获取手机归属地,引起net.UnknownHostException错误
这个问题是请求,重定向了,跟入源码.修改了地址,变成302 Connection connect = Jsoup.connect(url); connect.header("Host&quo ...
- android调用JPush获取手机的注册码(Cordova环境)
JPushInterface.addLocalNotification(cordova.getActivity().getApplication().getApplicationContext(), ...
- JS 新浪API获取IP归属地
http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 返回值数据格式:var remote_ip_info = {“ret”:1,” ...
- C++调用API获取当前时间
#include <string> #include<iostream> #include<windows.h> #include <sstream> ...
- PHP通过Zabbix API获取服务器监控信息
开源监控系统Zabbix提供了丰富的API,供第三方系统调用. 基本步骤如下: 1.获取合法认证:连接对应Zabbix URL,并提供用户名和密码,HTTP方法为"POST",HT ...
- 高德地图API获取天气
1.建立行政区规划清单表 use edw; drop table if exists dim_prov_city_adcode; create table if not exists dim_prov ...
随机推荐
- OC语法7——内存管理之@property参数
@property的参数: 我们已经知道为了给开发者提供便捷,OC提供了@porperty关键字,它可以自动生成属性的set和get方法. 但是我们又知道,在OC中还面临者对象内存管理的问题,而且我们 ...
- Android RadioGroup/RadioButton
RadioGroup RadioButton的集合,提供多选一的机制 属性: android:orientation="horizontal/vertical&quo ...
- hadoop笔记之Hive的数据存储(内部表)
Hive的数据存储(内部表) Hive的数据存储(内部表) 基于HDFS 可使用hadoop给我们提供的web管理工具查看数据.打开管理工具localhost:9000–>Utilities下的 ...
- gets和fgets函数的区别
1. gets与fgets gets函数原型:char*gets(char*buffer);//读取字符到数组:gets(str);str为数组名. gets函数功能:从键盘上输入字符,直至接受到换行 ...
- Cortex-M3 动态加载一(地址无关代码实现)
这篇文章是自己疑惑究竟地址无关性是如何实现,然后查看汇编和CPU指令手册,最后分析解除自己疑惑的,高手不要鄙视,哈哈. 编译C代码时候需要制定--acps/ropi选项,如下例子: void Syst ...
- Cortex-M3 .s启动文件分析
1. 基本概念(CMSIS): Cortex Micro-controller Software Interface Standard,微控制器软件接口标准. 2. CMSIS标准的文件结构: a) ...
- jQuery Pagination Plugin ajax分页控件
<html> <body> <div id="datagrid"> </div> <div id="paginati ...
- Qt编写端口扫描工具
Qt提供了QTcpSocket类,可以方便地建立TCP连接.利用这一功能,Maxiee编写了一个简单地端口扫描工具. 软件的功能就是,扫描某一网段的固定端口,如80端口,若目的地址开放了这一端口,那么 ...
- svn 问题汇总
1.当删除了原来的仓库时,再次新建,更新版本时会出现这个问题:
- 面向对象程序设计-C++_课时19const_课时20不可修改的
error C2131: 表达式的计算结果不是常数 #include <iostream> using namespace std; void main() { ; int finalGr ...