zoukankan      html  css  js  c++  java
  • 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;
    }



    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    C#yield return用法示例
    C#多线程示例
    AspNetCore.Authentication
    C#委托与事件
    按值和按引用传递参数
    基于iView的无限级菜单
    Sortable By Attribute
    未能加载文件或程序集“BLL”或它的某一个依赖项。生成此程序集的运行时比当前加载的运行时新,无法加载此程序集。
    有关导出Excel特殊字符的问题
    openFileDialog的使用
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834850.html
Copyright © 2011-2022 走看看