VC实现快递查询
#include <iostream>
#include <string>
#include <cstdlib>
#include <afxinet.h>
#include "tinyxml.h"
#pragma comment(lib, "tinyxml.lib")
#pragma comment(lib, "tinyxmlSTL.lib")
using namespace std; void ConvertUTF8ToANSI(CString strUTF8,CString &strANSI) //
{
int nLen = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, (LPCTSTR)strUTF8, -1, NULL, 0);
//返回需要的unicode长度
WCHAR *wszANSI = new WCHAR[nLen+1];
memset(wszANSI, 0, nLen * 2 + 2);
nLen = MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8, -1, wszANSI, nLen); //把utf8转成unicode
nLen = WideCharToMultiByte(CP_ACP, 0, wszANSI, -1, NULL, 0, NULL, NULL); //得到要的ansi长度
char *szANSI=new char[nLen + 1];
memset(szANSI, 0, nLen + 1);
WideCharToMultiByte (CP_ACP, 0, wszANSI, -1, szANSI, nLen, NULL,NULL); //把unicode转成ansi
strANSI = szANSI;
delete wszANSI;
delete szANSI;
} void getXml(LPCTSTR url)
{
CFile file((TEXT("temp.xml")), CFile::modeCreate|CFile::modeWrite);
CString content;
CString data, temp;
DWORD dwStatusCode;
CInternetSession session(TEXT("HttpClient")); CHttpFile* pfile = (CHttpFile *)session.OpenURL(url);
pfile -> QueryInfoStatusCode(dwStatusCode);
if(dwStatusCode == HTTP_STATUS_OK)
{
while (pfile -> ReadString(data))
{
temp += data;
}
}
pfile -> Close();
delete pfile;
session.Close();
ConvertUTF8ToANSI(temp, content);
file.Write(content, content.GetLength());
file.Close();
} void readXml()
{
TiXmlDocument doc("temp.xml");
doc.LoadFile();
//doc.Print();
TiXmlElement* rootElement = doc.RootElement(); //xml TiXmlElement* dataElement = rootElement->FirstChildElement(); //data
for (; dataElement != NULL; dataElement = dataElement->NextSiblingElement())
{
TiXmlElement* Element = dataElement->FirstChildElement();
for (; Element != NULL; Element = Element->NextSiblingElement())
{
string Type = Element->Value();
string Value = Element->GetText();
cout << Type << " : " << Value << endl;
}
}
} int main()
{
LPCTSTR str = TEXT("http://api.kuaidi100.com/api?id=a78e61062aabe452&com=yuantong&nu=9100493541&show=1");
getXml(str);
readXml();
//system("del temp.xml");
//system("pause");
return 0;
}
需要将返回的UTF-8数据转换为string,char等都支持的ansi
void ConvertUTF8ToANSI(CString strUTF8,CString &strANSI) //
{
int nLen = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, (LPCTSTR)strUTF8, -1, NULL, 0);
//返回需要的unicode长度
WCHAR *wszANSI = new WCHAR[nLen+1];
memset(wszANSI, 0, nLen * 2 + 2);
nLen = MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8, -1, wszANSI, nLen); //把utf8转成unicode
nLen = WideCharToMultiByte(CP_ACP, 0, wszANSI, -1, NULL, 0, NULL, NULL); //得到要的ansi长度
char *szANSI=new char[nLen + 1];
memset(szANSI, 0, nLen + 1);
WideCharToMultiByte (CP_ACP, 0, wszANSI, -1, szANSI, nLen, NULL,NULL); //把unicode转成ansi
strANSI = szANSI;
delete wszANSI;
delete szANSI;
}
或者利用C++11
void ConvertUTF8ToANSI()
{
auto LocUtf8 = std::locale(std::locale(""), new std::codecvt_utf8<wchar_t>);
std::wifstream wfin("temp1.xml");
std::wstring wstr, content;
wfin.imbue(LocUtf8);
while(getline(wfin, wstr))
{
content += wstr;
}
wfin.close();
system("del temp1.xml");
//std::wcout.imbue(std::locale(""));
//std::wcout << content << std::endl; std::locale::global(std::locale("Chinese-simplified"));
std::wofstream wfout("temp.xml");
wfout << content;
wfout.close();
}
寻得一个不用解码的API
http://api.ickd.cn/?id=102530&secret=dff7b12be1ae97433b2b57c74633a98d&com=shentong&nu=668456861017&type=xml
支持的常用的快递公司更多,果断换了。
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>
#include <cstdlib>
#include <map>
#include <fstream>
#include <afxinet.h>
#include "tinyxml.h"
#pragma comment(lib, "tinyxml.lib")
#pragma comment(lib, "tinyxmlSTL.lib")
using namespace std; void getXml(LPCTSTR url)
{
CFile file((TEXT("temp.xml")), CFile::modeCreate|CFile::modeWrite);
CString content;
CString data;
DWORD dwStatusCode;
CInternetSession session(TEXT("HttpClient")); CHttpFile* pfile = (CHttpFile *)session.OpenURL(url);
pfile -> QueryInfoStatusCode(dwStatusCode);
if(dwStatusCode == HTTP_STATUS_OK)
{
while (pfile -> ReadString(data))
{
content += data;
}
}
pfile -> Close();
delete pfile;
session.Close();
file.Write(content, content.GetLength());
file.Close();
} void readXml(void)
{
TiXmlDocument doc("temp.xml");
doc.LoadFile();
//doc.Print();
TiXmlElement* rootElement = doc.RootElement(); //response
TiXmlElement* dataElement = rootElement->FirstChildElement("data"); //item
TiXmlElement* itemElement = dataElement->FirstChildElement(); for (; itemElement != NULL; itemElement = itemElement->NextSiblingElement())
{
TiXmlElement* Element = itemElement->FirstChildElement();
for (; Element != NULL; Element = Element->NextSiblingElement())
{
string Type = Element->Value();
string Value = Element->GetText();
cout << Type << " : " << Value << endl;
}
}
} int scan(void)
{
int n;
cout << "--------+++++++++++++++++++---------" << endl;
cout << " 请选择快递公司 " << endl;
cout << " 1.申通快递 2.圆通快递 " << endl;
cout << " 3.天天快递 4.顺丰快递 " << endl;
cout << " 5.韵达快递 6.中通快递 " << endl;
cout << " 7.EMS快递 8.宅急送 " << endl;
cout << "--------+++++++++++++++++++---------" << endl;
cin >> n;
return n;
} int main(void)
{
string nu;
string url;
int flag;
bool quit = false;
const string key("http://api.ickd.cn/?id=102530&secret=dff7b12be1ae97433b2b57c74633a98d");
map<int, string>Map;
Map[1] = "&com=shentong";
Map[2] = "&com=yuantong";
Map[3] = "&com=tiantian";
Map[4] = "&com=shunfeng";
Map[5] = "&com=yunda";
Map[6] = "&com=zhongtong";
Map[7] = "&com=ems";
Map[8] = "&com=zhaijisong"; while (!quit)
{
flag = scan();
if(flag >= 1 && flag <= 8)
{
cout << "请输入运单号:" << endl;
cin >> nu;
url = key + Map[flag] + "&nu=" + nu + "&type=xml";
//cout << url << endl;
getXml(url.c_str());
readXml();
system("del temp.xml");
}
else
{
quit = true;
}
}
return 0;
}
VC实现快递查询的更多相关文章
- 快递查询SDK
简介: 快递查询的SDK,使用的是快递100的智能查询,此SDK只是中间包装了一层而已,单对于普通的快递业务查询已经足够,也省去开发者研究的时间,拿来即用. 用途: 1.对接微信公众平台 2.对接需要 ...
- Windows Phone7 快递查询
(1)API去友商100里申请 布局代码: Exp.xaml <phone:PhoneApplicationPage x:Class="WindowsPhone_Express ...
- baidu 快递查询API
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 快递查询API接口(trackingmore)
快递查询接口 目前提供快递查询的接口平台有: Trackingmore 快递100 快递网 不同接口的区别: (1)Trackingmore支持380家快递公司,其中有55家为国内的快递,其余325家 ...
- 常用免费快递查询API对接案例
现在许多电商公司和ERP都会寻找比较适用的集成快递查询接口,减少对接难度,现在整理一下常用的免费快递查询接口,并附上调用案例,如果有觉得不对的地方,望能够一起沟通探讨! 一.快递查询接口 目前有提供免 ...
- 快递查询API接口对接方法
各类接口 快递查询API有即时查询和订阅查询两种,即时是请求即返回数据,订阅则是订阅快递单号到接口,有物流轨迹更新则全量返回数据.目前常用的有快递鸟.快递100.快递网等. 快递鸟即时API可以查询3 ...
- 快递查询api(多接口方案)
/** 本环境使用php+smarty,结合两种快递api调取快递数据 * 说明,先快递鸟调取数据,失败后再调取快递网的数据* 快递鸟 http://www.kdniao.com 快递网 http:/ ...
- 各种快递查询--Api接口
授权成功我的密钥 爱查快递API使用说明文档 API地址: 以前:http://api.ickd.cn/?com=[]&nu=[]&id=[]&type=[]&enco ...
- Android项目---快递查询
快递查询,快递100上有更多接口信息 1.快递查询的接口是 快递公司的code值+快递单号 进行的网络查询.第一步,怎么将快递公司的名字转换成code值,传递给接口.下面是快递公司以及对应的code值 ...
随机推荐
- GraphicsLab Project之Diffuse Irradiance Environment Map
作者:i_dovelemon 日期:2020-01-04 主题:Rendering Equation,Irradiance Environment Map,Spherical Harmonic 引言 ...
- spring cloud 微服务之 -- 配置文件拆分之道
0-前言 在spring cloud微服务架构中,基本上每个拆分的微服务都会部署多个运行实例,这些运行实例,配置基本都是一样的,不同的是少数配置,比如端口,而这些不同的配置又是必不可少的 那我们怎么来 ...
- spring-boot序章:打造博客系统
blog 使用spring-boot打造一个博客系统,在项目中学习! 项目功能 文章 游览 创建 编辑 删除 评论 用户 游客 注册用户 关注 被关注 后台统计 注册用户数 在线人数 文章总数 评论总 ...
- OAuth2.0概念以及实现思路简介
一.什么是OAuth? OAuth是一个授权规范,可以使A应用在受限的情况下访问B应用中用户的资源(前提是经过了该用户的授权,而A应用并不需要也无法知道用户在B应用中的账号和密码),资源通常以REST ...
- springboot多环境(dev,test,prod)配置
前情提要 在我们开发工作中,常常因为配置的问题,搞得头昏脑大.开发环境.测试环境.配置各不相同,数据库.redis.注册中心等等参数都不一致,如果放在同一个配置文件,就会发现诸多注释,发布不同的环境, ...
- python文档字符串(函数使用说明)
关键字: 函数说明.help()函数 1.效果图: 2.代码: # 文档字符串( doc str) 是 函数使用说明 # 用法: 在函数第一行写一个字符串 def fn(*nums): ''' 函数的 ...
- 如何实施DevOps
对于长期在孤立的架构下工作的组织来说,转移到协作式DevOps系统似乎是难以成功的.为了进一步提高效率,必须改变观念,并进行团队文化改变.例如:许多人认为只有自动化工具才能解决DevOps,其实这是不 ...
- 7.Java帝国的诞生
1972年,C诞生,而Java是1995年诞生的.它贴近硬件,有汇编语言的特性,运行极快,效率极高.早期,用在操作系统.编译器.数据库.网络系统等.但它有两把沉重的枷锁一直在程序员身上,那就是指针和内 ...
- FindBugs报错
FindBugs是基于Bug Patterns概念,查找javabytecode(.class文件)中的潜在bug,主要检查bytecode中的bug patterns,如NullPoint空指针检查 ...
- .net Core Autofac稍微高级一点的方法
前情摘要 前段时间写了autofac的注入但是每次都需要去修改startup这应该不是大家想要的. 至少不是我想要的. 网上有朋友说可以创建一个基础类来时间. 好了吹牛时间结束我们开始干点正事. 创建 ...