更新 'CWeatherDataSpider.cpp'

master
JZFamily 2017-11-03 19:50:10 +08:00
parent 68c83d7542
commit 5776fd3dcf
1 changed files with 12 additions and 122 deletions

View File

@ -12,7 +12,7 @@
#define WEATHER_FORECAST 1 #define WEATHER_FORECAST 1
CWeatherDataSpider::CWeatherDataSpider() CWeatherDataSpider::CWeatherDataSpider()
{ {
m_strAreaCode = "天津"; m_strAreaCode = "天津";
} }
@ -22,7 +22,7 @@ CWeatherDataSpider::~CWeatherDataSpider()
bool CWeatherDataSpider::GetWeatherDataXml() bool CWeatherDataSpider::GetWeatherDataXml()
{ {
//------------------创建描述符---------------------- //------------------创建描述符----------------------
WSADATA wsadata; WSADATA wsadata;
int ierror = -1; int ierror = -1;
if (ierror = WSAStartup(MAKEWORD(2,2), &wsadata)) if (ierror = WSAStartup(MAKEWORD(2,2), &wsadata))
@ -49,12 +49,12 @@ bool CWeatherDataSpider::GetWeatherDataXml()
//} //}
//if(connect(sock, (SOCKADDR*)&sa, sizeof(sa))) //if(connect(sock, (SOCKADDR*)&sa, sizeof(sa)))
//连接主机 //连接主机
if (!ConnectHost()) { goto bad; } if (!ConnectHost()) { goto bad; }
//发送请求 //发送请求
if (!SendReq()) { goto bad; } if (!SendReq()) { goto bad; }
//接收回应 //接收回应
if (!HandleAck()) { goto bad; } if (!HandleAck()) { goto bad; }
else{ CloseConnect(); return true; } else{ CloseConnect(); return true; }
@ -86,14 +86,14 @@ bool CWeatherDataSpider::CheckIfRecSucceed(char * buf, int nTotalRecv, int nFlag
return false; return false;
} }
//解析回复是否是200 //解析回复是否是200
//截取xml //截取xml
// //
std::string strHeadFind =buf; std::string strHeadFind =buf;
int n =strHeadFind.find("\r\n"); int n =strHeadFind.find("\r\n");
if (-1 != (n = strHeadFind.rfind("200", n))) { return true; } if (-1 != (n = strHeadFind.rfind("200", n))) { return true; }
PosStart = strHeadFind.find("content-length: ");//该标签 加空格16个字符 PosStart = strHeadFind.find("content-length: ");//该标签 加空格16个字符
PosStart =PosStart + 16; PosStart =PosStart + 16;
PosEnd = strHeadFind.find("\r\n", PosStart); PosEnd = strHeadFind.find("\r\n", PosStart);
std::string strLen =strHeadFind.substr(PosStart, PosEnd-PosStart); std::string strLen =strHeadFind.substr(PosStart, PosEnd-PosStart);
@ -106,116 +106,6 @@ bool CWeatherDataSpider::CheckIfRecSucceed(char * buf, int nTotalRecv, int nFlag
int iTotalRead = 0; int iTotalRead = 0;
inflate_read((char*)buf + PosStart, nlen, &Output, 1, iTotalRead); inflate_read((char*)buf + PosStart, nlen, &Output, 1, iTotalRead);
// add by lei.zhao 2015-11-03, Bug[12533]
//bool bNewPacket = false;
//if (nFlag == WEATHER_FORECAST)
//{
// char bufTemp[RECV_BUFF_LENGTH] = { 0 };
// memcpy_s(bufTemp, RECV_BUFF_LENGTH, buf, nTotalRecv);
// bufTemp[nTotalRecv] = 0;
//
// CString strTemp;
// strTemp = bufTemp;
// strTemp = strTemp.MakeLower();
// int nFindPos = strTemp.Find(L"content-length: ");
// if (nFindPos != -1)
// {
// int nLengthStart = nFindPos + 16;
// int nLengthEnd = strTemp.Find(L"\r\n", nLengthStart);
// if (nLengthEnd == -1)
// {
// return false;
// }
// int nLength = _tstoi(strTemp.Mid(nLengthStart, nLengthEnd - nLengthStart));
// int nHearderLength = strTemp.Find(L"\r\n\r\n") + 4;
// if (nHearderLength == -1)
// {
// return false;
// }
// if (nHearderLength + nLength > nTotalRec)
// {
// return false;
// }
// memset(m_pbyXmlBuf, 0, 10000);
// char* Output = NULL;//= new char[10000];
// int iTotalRead = 0;
// inflate_read((char*)buf + nHearderLength, nLength, &Output, 1, iTotalRead);
// memcpy_s(m_pbyXmlBuf, 10000, Output, iTotalRead);
// delete[]Output;
// bNewPacket = true;
// }
//}
//if (!bNewPacket)
//{
// int nIndex = -1;
// for (int i = 0; i< nTotalRec; i++)
// {
// if (buf[i] == '\r'
// && buf[i + 1] == '\n'
// && buf[i + 2] == '\r'
// && buf[i + 3] == '\n'
// )
// {
// nIndex = i + 4;
// break;
// }
// }
// if (nIndex <0)
// {
// return false;
// }
// memset(m_pbyXmlBuf, 0, 10000);
// if (nFlag == WEATHER_REALTIME)
// {
// memcpy_s(m_pbyXmlBuf, 10000, buf + nIndex, nTotalRec - nIndex);
// }
// else if (nFlag == WEATHER_FORECAST)
// {
// int nIndexR = nIndex;
// bool bFindCount = false;
// do
// {
// if (buf[nIndexR] == '\r' && buf[nIndexR + 1] == '\n')
// {
// bFindCount = true;
// break;
// }
// ++nIndexR;
// } while (nIndexR<nTotalRec);
// if (!bFindCount)
// return false;
// char chTmp[10] = { 0 };
// memcpy_s(chTmp, sizeof chTmp, buf + nIndex, nIndexR - nIndex);
// int nTmp = strtol(chTmp, NULL, 16);
// if (!(nTmp + nIndexR + 9 == nTotalRec))
// {
// return false;
// }
// // FILE* file = fopen("D:\\Forecast_html.gz", "w");
// // if (file == NULL)
// // {
// // return false;
// // }
// char* Output = NULL;//= new char[10000];
// int iTotalRead = 0;
// inflate_read((char*)buf + nIndexR + 2, nTmp, &Output, 1, iTotalRead);
// memcpy_s(m_pbyXmlBuf, 10000, Output, iTotalRead);
// delete[]Output;
// }
//}
//TiXmlDocument xmlDoc; //TiXmlDocument xmlDoc;
//xmlDoc.Parse(m_pbyXmlBuf); //xmlDoc.Parse(m_pbyXmlBuf);
@ -239,8 +129,8 @@ bool CWeatherDataSpider::CheckIfRecSucceed(char * buf, int nTotalRecv, int nFlag
bool CWeatherDataSpider::SendReq() bool CWeatherDataSpider::SendReq()
{ {
//将 http://wthrcdn.etouch.cn/WeatherApi?city=天津 中文utf编码 //将 http://wthrcdn.etouch.cn/WeatherApi?city=天津 中文utf编码
//转换为 http://wthrcdn.etouch.cn/WeatherApi?city=%E5%A4%A9%E6%B4%A5 //转换为 http://wthrcdn.etouch.cn/WeatherApi?city=%E5%A4%A9%E6%B4%A5
std::string SendTmp = MBCStoUTF8(m_strAreaCode.c_str()); std::string SendTmp = MBCStoUTF8(m_strAreaCode.c_str());
std::string SendTmp2 = UrlEncode(SendTmp); std::string SendTmp2 = UrlEncode(SendTmp);
std::string strSnd = "GET /WeatherApi?city=" + SendTmp2; std::string strSnd = "GET /WeatherApi?city=" + SendTmp2;
@ -256,7 +146,7 @@ bool CWeatherDataSpider::SendReq()
} }
return true; return true;
} }
//突然懒了,一次接收算了,整这么多幺蛾子木用 //突然懒了,一次接收算了,整这么多幺蛾子木用
bool CWeatherDataSpider::HandleAck() bool CWeatherDataSpider::HandleAck()
{ {
char* pRecvBuff = new char[RECV_BUFF_LENGTH];//10kBbuff char* pRecvBuff = new char[RECV_BUFF_LENGTH];//10kBbuff
@ -320,7 +210,7 @@ bool CWeatherDataSpider::ConnectHost()
return false; return false;
} }
//成功返回0 //成功返回0
int nRet = connect(m_socket, (SOCKADDR*)&sa, sizeof(sa)); int nRet = connect(m_socket, (SOCKADDR*)&sa, sizeof(sa));
if (!nRet) if (!nRet)