一点建议,可以看看下面几个类,HTTP操作比WinSock方便多了。 CInternetSession CHttpConnection CHttpFile/******下面是一个大概的例子***************/ CInternetSession *m_pInnetSession; CHttpConnection *m_pHttpConnection; CHttpFile *m_pHttpFile;//Step1:初始化 int CMyGetWebData::InitWebSession(CString szServerName, int iPort) { //create a new internet session m_pInnetSession = new CMyInterSession();
//enable call back function, so can handle some message //m_pInnetSession->EnableStatusCallback();
//init m_pInnetSession, tell it my instance //m_pInnetSession->Init(this); try { //then open to a server,and return a http connection m_pHttpConnection = m_pInnetSession->GetHttpConnection((LPCTSTR)szServerName, INTERNET_FLAG_RELOAD, (INTERNET_PORT)iPort); if(m_pHttpConnection == NULL) { //have some error return -1; } } catch(CInternetException *pExcp) { //have some exception, handle it, then delete, waiwai pExcp->Delete(); return -1; } return 0; }//Step2:发送请求,我当时写的是用GET方法。可以携带任何Request头。 /**** 采用GET方式获得一个网页数据 szGetStr: The Get URL string(not include 'get') ****/ int CMyGetWebData::SendGetRequest(CString szGetStr) { try { m_pHttpFile = m_pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, (LPCTSTR)szGetStr, NULL, 1, NULL, NULL, INTERNET_FLAG_RELOAD);
然后把SESSION保存下来。
CHttpConnection
CHttpFile/******下面是一个大概的例子***************/
CInternetSession *m_pInnetSession;
CHttpConnection *m_pHttpConnection;
CHttpFile *m_pHttpFile;//Step1:初始化
int CMyGetWebData::InitWebSession(CString szServerName, int iPort)
{
//create a new internet session
m_pInnetSession = new CMyInterSession();
//enable call back function, so can handle some message
//m_pInnetSession->EnableStatusCallback();
//init m_pInnetSession, tell it my instance
//m_pInnetSession->Init(this); try
{
//then open to a server,and return a http connection
m_pHttpConnection = m_pInnetSession->GetHttpConnection((LPCTSTR)szServerName, INTERNET_FLAG_RELOAD, (INTERNET_PORT)iPort);
if(m_pHttpConnection == NULL)
{
//have some error
return -1;
}
}
catch(CInternetException *pExcp)
{
//have some exception, handle it, then delete, waiwai
pExcp->Delete();
return -1;
} return 0;
}//Step2:发送请求,我当时写的是用GET方法。可以携带任何Request头。
/****
采用GET方式获得一个网页数据
szGetStr: The Get URL string(not include 'get')
****/
int CMyGetWebData::SendGetRequest(CString szGetStr)
{
try
{
m_pHttpFile = m_pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, (LPCTSTR)szGetStr, NULL, 1, NULL, NULL, INTERNET_FLAG_RELOAD);
//增加各种Request Header
m_pHttpFile->AddRequestHeaders( theApp.m_constAccept);
m_pHttpFile->AddRequestHeaders( theApp.m_constAcceptLang);
m_pHttpFile->AddRequestHeaders( theApp.m_constAcceptEncode);
m_pHttpFile->AddRequestHeaders( theApp.m_constUserAgent);
m_pHttpFile->SendRequest();
#if 0
//handle data
if(HandleWebFileFirst() != 0)
{
return -1;
}
#endif
}
catch(CInternetException *pExcp)
{
//have some exception, handle it, then delete, waiwai
pExcp->Delete();
return -1;
} return 0;
}//Step3:收到HTML文件之后处理。
/*对HTML文件进行处理*/
int CMyGetWebData::HandleWebFile(void)
{ //check return code
if(m_pHttpFile->QueryInfo(HTTP_QUERY_STATUS_CODE, iRetCode) == 0)
{
//have some error
iErrCode = GetLastError();
goto Err_Exit;
}
/*
else
{
iRetCode = 0;
}
*/
if(iRetCode != 200)
{
CString cRetText;
//not OK , can record some reason
m_pHttpFile->QueryInfo(HTTP_QUERY_STATUS_TEXT, cRetText);
goto Err_Exit;
} //success begin handle //获得文件长度
m_pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, dwHttpFileSize);
if( dwHttpFileSize == 0)
{
//查询得到的页面不对,没有查询到相应的内容
ASSERT(FALSE);
goto Err_Exit;
}
...
}
比如最讨厌的wininet超时问题
比如绕过http-only等等
最近用C#重写 cookie太麻烦了...