📄 ahttpforce.cpp
字号:
#include "StdAfx.h"
#include <afxinet.h>
#include <WinInet.h>
#include "CharConvert.h"
#include "AHttpForCE.h"
CAHttpForCE::CAHttpForCE(void)
{
}
CAHttpForCE::~CAHttpForCE(void)
{
}
// 获取html文本
bool CAHttpForCE::GetHtml(LPCTSTR url,CString& result)
{
CInternetSession mySession(NULL,0,INTERNET_OPEN_TYPE_DIRECT);//建立会话
CHttpFile* myHttpFile=NULL;
CString myData;
try
{
myHttpFile=(CHttpFile*)mySession.OpenURL(url);
}
catch(CInternetException *e)
{
e->Delete();
return false;
}
while(myHttpFile->ReadString(myData))
{
result+=myData;
}
//result=CCharConvert::Ansi2Unicode((char*)result.GetBuffer());
myHttpFile->Close();
return true;
}
BOOL CAHttpForCE::Post(CString& csResponse,//反馈,这个就是你要的
const TCHAR *szServer,//服务器
WORD& nPort,//端口
const TCHAR* szObject,//URI
const TCHAR *szData, //正文内容
DWORD& dwHttpStatus,//状态码
BOOL bAutoRedirect)//是否自动转向
{
CInternetSession* pSession = NULL;
CHttpConnection* pConnection = NULL;
CHttpFile* pHttpFile = NULL;
try
{
pSession = new CInternetSession(NULL,1,INTERNET_OPEN_TYPE_PRECONFIG);
pConnection = pSession->GetHttpConnection(szServer, nPort,NULL,NULL);
DWORD dwFlag=INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE;
if(!bAutoRedirect)
{
dwFlag = dwFlag| INTERNET_FLAG_NO_AUTO_REDIRECT;
}
pHttpFile = pConnection->OpenRequest(
CHttpConnection::HTTP_VERB_POST,
szObject,
szServer,
1,
NULL,
NULL,
dwFlag);
pHttpFile->AddRequestHeaders(_T("Content-Type: application/x-www-form-urlencoded"),HTTP_ADDREQ_FLAG_ADD_IF_NEW,-1L);
pHttpFile->SendRequest(NULL,0,(void*)szData,strlen(szData));
if (pHttpFile)
{
if (pHttpFile->QueryInfoStatusCode(dwHttpStatus)!=0)
{// dwHttpStatus = 200;
if (dwHttpStatus < 400)
{
int nRead = 0;
LPSTR pBuffer = new char[1024];
do
{
nRead = pHttpFile->Read(pBuffer, 1023);
if (nRead != 0)
{
pBuffer[nRead] = 0;
csResponse += pBuffer;
}
} while (nRead != 0);
if(pBuffer)
{
delete pBuffer;
pBuffer = NULL;
}
}
}
}
}
catch (CInternetException* e)
{
e->Delete();
return FALSE;
}
catch (...)
{
return FALSE;
}
if (pHttpFile != NULL)
{
pHttpFile->Close();
delete pHttpFile;
}
if (pConnection != NULL)
{
pConnection->Close();
delete pConnection;
}
if (pSession != NULL)
{
pSession->Close();
delete pSession;
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -