1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <afxinet.h>
  5. #include "tinyxml.h"
  6. #pragma comment(lib, "tinyxml.lib")
  7. #pragma comment(lib, "tinyxmlSTL.lib")
  8. using namespace std;
  9.  
  10. void ConvertUTF8ToANSI(CString strUTF8,CString &strANSI) //
  11. {
  12. int nLen = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, (LPCTSTR)strUTF8, -1, NULL, 0);
  13. //返回需要的unicode长度
  14. WCHAR *wszANSI = new WCHAR[nLen+1];
  15. memset(wszANSI, 0, nLen * 2 + 2);
  16. nLen = MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8, -1, wszANSI, nLen); //把utf8转成unicode
  17. nLen = WideCharToMultiByte(CP_ACP, 0, wszANSI, -1, NULL, 0, NULL, NULL); //得到要的ansi长度
  18. char *szANSI=new char[nLen + 1];
  19. memset(szANSI, 0, nLen + 1);
  20. WideCharToMultiByte (CP_ACP, 0, wszANSI, -1, szANSI, nLen, NULL,NULL); //把unicode转成ansi
  21. strANSI = szANSI;
  22. delete wszANSI;
  23. delete szANSI;
  24. }
  25.  
  26. void getXml(LPCTSTR url)
  27. {
  28. CFile file((TEXT("temp.xml")), CFile::modeCreate|CFile::modeWrite);
  29. CString content;
  30. CString data, temp;
  31. DWORD dwStatusCode;
  32. CInternetSession session(TEXT("HttpClient"));
  33.  
  34. CHttpFile* pfile = (CHttpFile *)session.OpenURL(url);
  35. pfile -> QueryInfoStatusCode(dwStatusCode);
  36. if(dwStatusCode == HTTP_STATUS_OK)
  37. {
  38. while (pfile -> ReadString(data))
  39. {
  40. temp += data;
  41. }
  42. }
  43. pfile -> Close();
  44. delete pfile;
  45. session.Close();
  46. ConvertUTF8ToANSI(temp, content);
  47. file.Write(content, content.GetLength());
  48. file.Close();
  49. }
  50.  
  51. void readXml()
  52. {
  53. TiXmlDocument doc("temp.xml");
  54. doc.LoadFile();
  55. //doc.Print();
  56. TiXmlElement* rootElement = doc.RootElement(); //xml
  57.  
  58. TiXmlElement* dataElement = rootElement->FirstChildElement(); //data
  59. for (; dataElement != NULL; dataElement = dataElement->NextSiblingElement())
  60. {
  61. TiXmlElement* Element = dataElement->FirstChildElement();
  62. for (; Element != NULL; Element = Element->NextSiblingElement())
  63. {
  64. string Type = Element->Value();
  65. string Value = Element->GetText();
  66. cout << Type << " : " << Value << endl;
  67. }
  68. }
  69. }
  70.  
  71. int main()
  72. {
  73. LPCTSTR str = TEXT("http://api.kuaidi100.com/api?id=a78e61062aabe452&com=yuantong&nu=9100493541&show=1");
  74. getXml(str);
  75. readXml();
  76. //system("del temp.xml");
  77. //system("pause");
  78. return 0;
  79. }

需要将返回的UTF-8数据转换为string,char等都支持的ansi

  1. void ConvertUTF8ToANSI(CString strUTF8,CString &strANSI) //
  2. {
  3. int nLen = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, (LPCTSTR)strUTF8, -1, NULL, 0);
  4. //返回需要的unicode长度
  5. WCHAR *wszANSI = new WCHAR[nLen+1];
  6. memset(wszANSI, 0, nLen * 2 + 2);
  7. nLen = MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8, -1, wszANSI, nLen); //把utf8转成unicode
  8. nLen = WideCharToMultiByte(CP_ACP, 0, wszANSI, -1, NULL, 0, NULL, NULL); //得到要的ansi长度
  9. char *szANSI=new char[nLen + 1];
  10. memset(szANSI, 0, nLen + 1);
  11. WideCharToMultiByte (CP_ACP, 0, wszANSI, -1, szANSI, nLen, NULL,NULL); //把unicode转成ansi
  12. strANSI = szANSI;
  13. delete wszANSI;
  14. delete szANSI;
  15. }

或者利用C++11

  1. void ConvertUTF8ToANSI()
  2. {
  3. auto LocUtf8 = std::locale(std::locale(""), new std::codecvt_utf8<wchar_t>);
  4. std::wifstream wfin("temp1.xml");
  5. std::wstring wstr, content;
  6. wfin.imbue(LocUtf8);
  7. while(getline(wfin, wstr))
  8. {
  9. content += wstr;
  10. }
  11. wfin.close();
  12. system("del temp1.xml");
  13. //std::wcout.imbue(std::locale(""));
  14. //std::wcout << content << std::endl;
  15.  
  16. std::locale::global(std::locale("Chinese-simplified"));
  17. std::wofstream wfout("temp.xml");
  18. wfout << content;
  19. wfout.close();
  20. }

寻得一个不用解码的API

http://api.ickd.cn/?id=102530&secret=dff7b12be1ae97433b2b57c74633a98d&com=shentong&nu=668456861017&type=xml

支持的常用的快递公司更多,果断换了。

  1. #include <iostream>
  2. #include <string>
  3. #include <locale>
  4. #include <codecvt>
  5. #include <cstdlib>
  6. #include <map>
  7. #include <fstream>
  8. #include <afxinet.h>
  9. #include "tinyxml.h"
  10. #pragma comment(lib, "tinyxml.lib")
  11. #pragma comment(lib, "tinyxmlSTL.lib")
  12. using namespace std;
  13.  
  14. void getXml(LPCTSTR url)
  15. {
  16. CFile file((TEXT("temp.xml")), CFile::modeCreate|CFile::modeWrite);
  17. CString content;
  18. CString data;
  19. DWORD dwStatusCode;
  20. CInternetSession session(TEXT("HttpClient"));
  21.  
  22. CHttpFile* pfile = (CHttpFile *)session.OpenURL(url);
  23. pfile -> QueryInfoStatusCode(dwStatusCode);
  24. if(dwStatusCode == HTTP_STATUS_OK)
  25. {
  26. while (pfile -> ReadString(data))
  27. {
  28. content += data;
  29. }
  30. }
  31. pfile -> Close();
  32. delete pfile;
  33. session.Close();
  34. file.Write(content, content.GetLength());
  35. file.Close();
  36. }
  37.  
  38. void readXml(void)
  39. {
  40. TiXmlDocument doc("temp.xml");
  41. doc.LoadFile();
  42. //doc.Print();
  43. TiXmlElement* rootElement = doc.RootElement(); //response
  44. TiXmlElement* dataElement = rootElement->FirstChildElement("data"); //item
  45. TiXmlElement* itemElement = dataElement->FirstChildElement();
  46.  
  47. for (; itemElement != NULL; itemElement = itemElement->NextSiblingElement())
  48. {
  49. TiXmlElement* Element = itemElement->FirstChildElement();
  50. for (; Element != NULL; Element = Element->NextSiblingElement())
  51. {
  52. string Type = Element->Value();
  53. string Value = Element->GetText();
  54. cout << Type << " : " << Value << endl;
  55. }
  56. }
  57. }
  58.  
  59. int scan(void)
  60. {
  61. int n;
  62. cout << "--------+++++++++++++++++++---------" << endl;
  63. cout << " 请选择快递公司 " << endl;
  64. cout << " 1.申通快递 2.圆通快递 " << endl;
  65. cout << " 3.天天快递 4.顺丰快递 " << endl;
  66. cout << " 5.韵达快递 6.中通快递 " << endl;
  67. cout << " 7.EMS快递 8.宅急送 " << endl;
  68. cout << "--------+++++++++++++++++++---------" << endl;
  69. cin >> n;
  70. return n;
  71. }
  72.  
  73. int main(void)
  74. {
  75. string nu;
  76. string url;
  77. int flag;
  78. bool quit = false;
  79. const string key("http://api.ickd.cn/?id=102530&secret=dff7b12be1ae97433b2b57c74633a98d");
  80. map<int, string>Map;
  81. Map[1] = "&com=shentong";
  82. Map[2] = "&com=yuantong";
  83. Map[3] = "&com=tiantian";
  84. Map[4] = "&com=shunfeng";
  85. Map[5] = "&com=yunda";
  86. Map[6] = "&com=zhongtong";
  87. Map[7] = "&com=ems";
  88. Map[8] = "&com=zhaijisong";
  89.  
  90. while (!quit)
  91. {
  92. flag = scan();
  93. if(flag >= 1 && flag <= 8)
  94. {
  95. cout << "请输入运单号:" << endl;
  96. cin >> nu;
  97. url = key + Map[flag] + "&nu=" + nu + "&type=xml";
  98. //cout << url << endl;
  99. getXml(url.c_str());
  100. readXml();
  101. system("del temp.xml");
  102. }
  103. else
  104. {
  105. quit = true;
  106. }
  107. }
  108. return 0;
  109. }

VC实现快递查询的更多相关文章

  1. 快递查询SDK

    简介: 快递查询的SDK,使用的是快递100的智能查询,此SDK只是中间包装了一层而已,单对于普通的快递业务查询已经足够,也省去开发者研究的时间,拿来即用. 用途: 1.对接微信公众平台 2.对接需要 ...

  2. Windows Phone7 快递查询

        (1)API去友商100里申请 布局代码: Exp.xaml <phone:PhoneApplicationPage x:Class="WindowsPhone_Express ...

  3. baidu 快递查询API

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  4. 快递查询API接口(trackingmore)

    快递查询接口 目前提供快递查询的接口平台有: Trackingmore 快递100 快递网 不同接口的区别: (1)Trackingmore支持380家快递公司,其中有55家为国内的快递,其余325家 ...

  5. 常用免费快递查询API对接案例

    现在许多电商公司和ERP都会寻找比较适用的集成快递查询接口,减少对接难度,现在整理一下常用的免费快递查询接口,并附上调用案例,如果有觉得不对的地方,望能够一起沟通探讨! 一.快递查询接口 目前有提供免 ...

  6. 快递查询API接口对接方法

    各类接口 快递查询API有即时查询和订阅查询两种,即时是请求即返回数据,订阅则是订阅快递单号到接口,有物流轨迹更新则全量返回数据.目前常用的有快递鸟.快递100.快递网等. 快递鸟即时API可以查询3 ...

  7. 快递查询api(多接口方案)

    /** 本环境使用php+smarty,结合两种快递api调取快递数据 * 说明,先快递鸟调取数据,失败后再调取快递网的数据* 快递鸟 http://www.kdniao.com 快递网 http:/ ...

  8. 各种快递查询--Api接口

    授权成功我的密钥 爱查快递API使用说明文档 API地址: 以前:http://api.ickd.cn/?com=[]&nu=[]&id=[]&type=[]&enco ...

  9. Android项目---快递查询

    快递查询,快递100上有更多接口信息 1.快递查询的接口是 快递公司的code值+快递单号 进行的网络查询.第一步,怎么将快递公司的名字转换成code值,传递给接口.下面是快递公司以及对应的code值 ...

随机推荐

  1. 人生苦短,我用Python(6)

    1.分隔.合并字符串 分隔字符串是把字符串分隔为列表,而合并字符串是把列表合并为字符串,分割字符串和合并字符串可以看作是互逆操作. (1)分隔字符串 字符串对象得split()方法可以实现字符串分隔, ...

  2. 解决阿里云专有网络ftp无法远程链接

    配置好ftp后本机测试可用但无法远程连接 网络上找了很多方法,配置防火墙出入站规则均无效 提交阿里云工单,给出解决方法,测试后可用

  3. C# 将Word转为PDF、XPS、Epub、RTF(基于Spire.Cloud.Word.SDK)

    本文介绍通过调用Spire.Cloud.Word.SDK提供的ConvertApi接口将Word转换为PDF.XPS.Epub.RTF以及将Docx转为Doc格式等.调用接口方法及步骤参考以下步骤: ...

  4. C# 调用R语言

    在.net项目中需要调用Matlab生成的DLL,但是在调用过程中报错,截图如下: 在网上搜索一下资料,看到该博客:https://cn.mathworks.com/matlabcentral/new ...

  5. TCP 协议详解

    TCP 协议是 更靠近应用层,因此在应用程序中具有更强可操作性,一些重要 socket 选项都和 TCP 协议相关. TCP 头部信息:TCP 头部信息出现在每个 TCP 报文段中,用于指定通信的源端 ...

  6. python命名空间(namespace)

    命名空间: 每一个作用域变量存储的位置,或者解释为 存储作用域中变量的字典. 作用: 获取想查看某个作用域中的变量名.变量值. 使用方法: locals()  #当前命名空间 1. 效果图: 2. 代 ...

  7. 切蛋糕(贪心 or 优先队列)

    链接:https://www.nowcoder.com/acm/contest/80/D来源:牛客网 最可爱的applese生日啦,他准备了许多个质量不同的蛋糕,想请一些同学来参加他的派对为他庆生,为 ...

  8. Nginx的一理解(2)

    1.静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML.图片)通过HTTP协议展现给客户端. 配置:

  9. Hibernate定制Tuplizer

    Hibernate定制Tuplizer,使用@Tuplizer注解 本文转自http://www.521100.net/hibernate-tuplizer.html 首先,第一步在Hinbernat ...

  10. 一图胜千言elasticsearch(lucene)的内存管理