CHttpFile实现Get/Post - 下载网页 提交表单
来源:互联网 发布:mac pro 抹掉磁盘 编辑:程序博客网 时间:2024/06/05 05:34
一、GET 数据,下载网页,文件等,用于可下载的文件,不能用于服务端运行的程序,比如.aspx文件等,否则会返回500错误。
CString strSentence, strWriteName="1.htm";
CString strFileName="http://localhost/InDesign/" + strWriteName;
CInternetSession sess;
CHttpFile* fileGet;
try
{
fileGet=(CHttpFile*)sess.OpenURL(strFileName);
}
catch(CException* e)
{
fileGet = 0;
throw;
}
if(fileGet)
{
DWORD dwStatus;
DWORD dwBuffLen = sizeof(dwStatus);
BOOL bSuccess = fileGet->QueryInfo(HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwBuffLen);
if( bSuccess && dwStatus>= 200&& dwStatus<300 )
{
CStdioFile fileWrite;
if(fileWrite.Open(strWriteName, CFile::modeWrite|CFile::modeCreate))
{
while(fileGet->ReadString(strSentence))
{
fileWrite.WriteString(strSentence+"/n");
}
fileWrite.Close();
AfxMessageBox("下载完毕");
}
else
{
AfxMessageBox("本地文件"+strWriteName+"打开出错.");
}
}
else
{
strSentence.Format("打开网页文件出错,错误码:%d", dwStatus);
AfxMessageBox(strSentence);
}
fileGet->Close();
delete fileGet;
}
else
AfxMessageBox("不能找到网页文件!");
sess.Close();
二、POST 数据,比如用于提交注册信息等
CString strHttpName="http://localhost/TestReg/RegForm.aspx"; // 需要提交数据的页面
CString strFormData = "username=abc&password=123"; // 需要提交的数据
CInternetSession sess;
CHttpFile* fileGet;
CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded"); // 请求头
try
{
fileGet=(CHttpFile*)sess.OpenURL(strHttpName);//打开文件
}
catch(CException* e)
{
fileGet = 0;
throw;
}
CString strSentence, strGetSentence = "";
if(fileGet)
{
DWORD dwStatus;
DWORD dwBuffLen = sizeof(dwStatus);
BOOL bSuccess = fileGet->QueryInfo(HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwBuffLen);
if( bSuccess && dwStatus>= 200 &&dwStatus<300 )
{
BOOL result = fileGet->SendRequest(strHeaders, (LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
while(fileGet->ReadString(strSentence)) // 读取提交数据后的返回结果
{
strGetSentence = strGetSentence + strSentence + char(13) + char(10);
}
AfxMessageBox(strGetSentence); // 显示返回网页内容
}
else
{
strSentence.Format("POST出错,错误码:%d", dwStatus);
AfxMessageBox(strSentence);
}
fileGet->Close();
delete fileGet;
}
else
AfxMessageBox("不能找到网页文件!");
sess.Close();
补充: void xx(void)
{
CHAR szReceiveBuffer[36000];
memset(szReceiveBuffer,0,36000);
char *req="POST HTTP/1.0
"
"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/vnd.ms-powerpoint, */*
"
"Accept-Language: en-us
"
"Accept-Encoding: gzip, deflate
"
"User-Agent: Mozilla/4.0
"
"Content-Length: 34
"
"Host: 127.0.0.1
"
"Content-Type: application/x-www-form-urlencoded
";
LPSTR lpOptions="user=admin&Pwd=admin&submit=提交";
CInternetSession cInternetSession;
CHttpConnection * pHttpConnection = cInternetSession.GetHttpConnection(_T("127.0.0.1"), (INTERNET_PORT)80);
CHttpFile * pHttpFile = pHttpConnection->OpenRequest("POST", _T("/admin/test.asp"));
pHttpFile->AddRequestHeaders(req);
pHttpFile->SendRequest(0,0, (LPVOID) lpOptions, (DWORD)strlen(lpOptions));
pHttpFile->Read(szReceiveBuffer, 36000);
cInternetSession.Close();
//CString hh=szReceiveBuffer;
//if(hh.Find("您输入了错误的帐号或",0))
MessageBox(szReceiveBuffer);
return ;
}
CString strSentence, strWriteName="1.htm";
CString strFileName="http://localhost/InDesign/" + strWriteName;
CInternetSession sess;
CHttpFile* fileGet;
try
{
fileGet=(CHttpFile*)sess.OpenURL(strFileName);
}
catch(CException* e)
{
fileGet = 0;
throw;
}
if(fileGet)
{
DWORD dwStatus;
DWORD dwBuffLen = sizeof(dwStatus);
BOOL bSuccess = fileGet->QueryInfo(HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwBuffLen);
if( bSuccess && dwStatus>= 200&& dwStatus<300 )
{
CStdioFile fileWrite;
if(fileWrite.Open(strWriteName, CFile::modeWrite|CFile::modeCreate))
{
while(fileGet->ReadString(strSentence))
{
fileWrite.WriteString(strSentence+"/n");
}
fileWrite.Close();
AfxMessageBox("下载完毕");
}
else
{
AfxMessageBox("本地文件"+strWriteName+"打开出错.");
}
}
else
{
strSentence.Format("打开网页文件出错,错误码:%d", dwStatus);
AfxMessageBox(strSentence);
}
fileGet->Close();
delete fileGet;
}
else
AfxMessageBox("不能找到网页文件!");
sess.Close();
二、POST 数据,比如用于提交注册信息等
CString strHttpName="http://localhost/TestReg/RegForm.aspx"; // 需要提交数据的页面
CString strFormData = "username=abc&password=123"; // 需要提交的数据
CInternetSession sess;
CHttpFile* fileGet;
CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded"); // 请求头
try
{
fileGet=(CHttpFile*)sess.OpenURL(strHttpName);//打开文件
}
catch(CException* e)
{
fileGet = 0;
throw;
}
CString strSentence, strGetSentence = "";
if(fileGet)
{
DWORD dwStatus;
DWORD dwBuffLen = sizeof(dwStatus);
BOOL bSuccess = fileGet->QueryInfo(HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwBuffLen);
if( bSuccess && dwStatus>= 200 &&dwStatus<300 )
{
BOOL result = fileGet->SendRequest(strHeaders, (LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
while(fileGet->ReadString(strSentence)) // 读取提交数据后的返回结果
{
strGetSentence = strGetSentence + strSentence + char(13) + char(10);
}
AfxMessageBox(strGetSentence); // 显示返回网页内容
}
else
{
strSentence.Format("POST出错,错误码:%d", dwStatus);
AfxMessageBox(strSentence);
}
fileGet->Close();
delete fileGet;
}
else
AfxMessageBox("不能找到网页文件!");
sess.Close();
补充: void xx(void)
{
CHAR szReceiveBuffer[36000];
memset(szReceiveBuffer,0,36000);
char *req="POST HTTP/1.0
"
"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/vnd.ms-powerpoint, */*
"
"Accept-Language: en-us
"
"Accept-Encoding: gzip, deflate
"
"User-Agent: Mozilla/4.0
"
"Content-Length: 34
"
"Host: 127.0.0.1
"
"Content-Type: application/x-www-form-urlencoded
";
LPSTR lpOptions="user=admin&Pwd=admin&submit=提交";
CInternetSession cInternetSession;
CHttpConnection * pHttpConnection = cInternetSession.GetHttpConnection(_T("127.0.0.1"), (INTERNET_PORT)80);
CHttpFile * pHttpFile = pHttpConnection->OpenRequest("POST", _T("/admin/test.asp"));
pHttpFile->AddRequestHeaders(req);
pHttpFile->SendRequest(0,0, (LPVOID) lpOptions, (DWORD)strlen(lpOptions));
pHttpFile->Read(szReceiveBuffer, 36000);
cInternetSession.Close();
//CString hh=szReceiveBuffer;
//if(hh.Find("您输入了错误的帐号或",0))
MessageBox(szReceiveBuffer);
return ;
}
- CHttpFile实现Get/Post - 下载网页 提交表单
- CHttpFile实现Get/Post
- CHttpFile GET/POST 提交数据
- 用CHttpFile实现简单的GET/POST数据
- 用CHttpFile实现简单的GET/POST数据
- 用CHttpFile实现简单的GET/POST数据
- 用CHttpFile实现简单的GET/POST数据
- 用CHttpFile实现简单的GET/POST数据
- 用CHttpFile实现简单的GET/POST数据
- python get,post提交表单
- python get,post提交表单
- 表单提交get和post
- Android 使用三种方式获取网页(通过Post,Get进行表单的提交)
- 深入研究表单提交方式:GET/POST
- 表单提交方法post和get区别
- 表单的提交方式POST和GET
- post 和 get 提交表单是的异同
- POST与GET提交表单的区别
- 新人程序员的痛
- 使用libz和libbz2的API在项目中集成gzip和bzip2功能
- makefile文件及VC++自带的Nmake
- Post和Get的区别(兼谈页面间传值的方式)
- Linux 防止cc syn ack 攻击
- CHttpFile实现Get/Post - 下载网页 提交表单
- 使用_CRTDBG_LEAK_CHECK_DF检查VC程序的内存泄漏
- objcopy使用说明
- 用iptables来防止web服务器被CC攻击
- Qt 区分多系统
- WDM驱动程序
- .net 操作cookie
- Qt Linux下调用进程
- javascript -- 发现之前开发的paypal跳转伪装下单站的程序存在重大的BUG