#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;
}

最新文章

  1. SQL SERVER全面优化-------Expert for SQL Server 诊断系列
  2. java打包文件夹为zip文件
  3. 正则表达式常用用法汇总 __西科大C语言
  4. AngularJS 最常用的功能
  5. Redis缓存数据库详解
  6. HRESULT 0x80131515 解决方法
  7. android - python 自动化测试 移动互联网 - SegmentFault
  8. cf C. George and Number
  9. tomcat : Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException:
  10. Git &amp; Github 一页简明笔记(转)main
  11. cocos2d-x 3.10 显示Box2d 调试视图
  12. thinkphp apicloud 下拉刷新 。。。由于新人里面导入了vue.js
  13. wap网站的优化建设
  14. multiset与set
  15. 敏捷冲刺每日报告四(Java-Team)
  16. springboot注解@SpringBootApplication分析
  17. MT【151】传球问题
  18. 使用js/jquery查找iframe中的
  19. Cloudera Manager安装之Cloudera Manager 5.3.X安装(三)(tar方式、rpm方式和yum方式)
  20. nyoj 737 石子合并 http://blog.csdn.net/wangdan11111/article/details/45032519

热门文章

  1. Linux系统之运行状态分析及问题排查思路
  2. keuectl命令
  3. 2020 年 Java 程序员应该学习什么?
  4. 二分查找LintcodeNo14
  5. TensorFlow——MNIST手写数据集
  6. Spring(一)开篇
  7. 【一起学源码-微服务】Hystrix 源码三:Hystrix核心流程:Hystix降级、熔断等原理剖析
  8. 暑假提高组集训Day1 T1
  9. 7.JavaSE之类型转换
  10. 工作笔记-- 源码安装nginx