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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值