如何实现网页中的图片另存? 分析出图片的链接地址,通过http把此链接地址的文件下载下来,保存成文件,注意此图象文件的扩展名。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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); //增加各种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; } ... }对于这种重定向,也可以获取,因此你只要解析出那个src="http://count.koubei.com/showphone/showphone.php?f=jpg&w=230&h=50&bc=255,255,255&fc=0,0,0&fs=20&fn=arial&phone=LTcxNjgxNDQyNA%3D%3D%23qDE%2BGVmf5uZkpJQhmw%3D%3D"这个字符串是一个GET请求。 直接把它下载到本地!CInternetSession session; CHttpFile *file = NULL; CString strURL = _T("http://www.csdn.net"); CString strHtml = _T(""); //存放网页数据 try { file = (CHttpFile*)session.OpenURL(strURL); } catch(CInternetException * m_pException) { file = NULL; m_pException->m_dwError; m_pException->Delete(); session.Close(); AfxMessageBox(_T("CInternetException")); } CString strLine; CString strHtmlgb2312; char sRecived[1024]; if(file != NULL) { while(file->ReadString((LPTSTR)sRecived,1024) != NULL) { strHtmlgb2312 = ConvertUTF8toGB2312(sRecived,strlen(sRecived)); strHtml += strHtmlgb2312; } } else { AfxMessageBox(_T("fail")); } CFile MyFile; MyFile.Open("F:\\自己写的程序\\练习用程序设计\\NetTest\\Debug\\test.html",CFile::modeWrite | CFile::modeCreate,NULL); MyFile.Write(strHtml,strHtml.GetLength()); MyFile.Close(); session.Close(); file->Close(); delete file; file = NULL; 打开这个URL后,源代码里面不就有图片的地址么,解析出来进行下载不就好了 VC调试模式问题 本人新手OCX,请教标准时间的响应函数写在哪里啊~ VC 动态创建 Picture 控件 请教一个问题activeX? 怎么可以动态改变嵌入对话框中的对话框的大小? flashGet通过使用多线程提高下载速度,求教原因。 有关IP HELPER 函数的使用 创建有数据库支持项目时,为什么我的DAO不能用(在 appwizard 向导中)? 编译问题——天之奇! CEditView的背景色怎样改?从CView派生的窗口背景色又改怎么改? 大家给个建议? 探讨一下对话框主窗口时什么时候弹出来的
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;
}
...
}对于这种重定向,也可以获取,因此你只要解析出那个
src="http://count.koubei.com/showphone/showphone.php?f=jpg&w=230&h=50&bc=255,255,255&fc=0,0,0&fs=20&fn=arial&phone=LTcxNjgxNDQyNA%3D%3D%23qDE%2BGVmf5uZkpJQhmw%3D%3D"
这个字符串是一个GET请求。
CInternetSession session;
CHttpFile *file = NULL;
CString strURL = _T("http://www.csdn.net");
CString strHtml = _T(""); //存放网页数据 try
{
file = (CHttpFile*)session.OpenURL(strURL);
}
catch(CInternetException * m_pException)
{ file = NULL;
m_pException->m_dwError;
m_pException->Delete();
session.Close();
AfxMessageBox(_T("CInternetException"));
} CString strLine;
CString strHtmlgb2312;
char sRecived[1024];
if(file != NULL)
{
while(file->ReadString((LPTSTR)sRecived,1024) != NULL)
{
strHtmlgb2312 = ConvertUTF8toGB2312(sRecived,strlen(sRecived));
strHtml += strHtmlgb2312;
}
}
else
{ AfxMessageBox(_T("fail"));
}
CFile MyFile;
MyFile.Open("F:\\自己写的程序\\练习用程序设计\\NetTest\\Debug\\test.html",CFile::modeWrite | CFile::modeCreate,NULL);
MyFile.Write(strHtml,strHtml.GetLength());
MyFile.Close();
session.Close();
file->Close();
delete file;
file = NULL;