两个问题,一个300分,不够另开贴给分
1、如何不通过浏览器编程从网络中取得网页源码(不要求取得服务器端代码)?
2、如何编程从浏览器取得网页源码?
比如:我想自己写浏览器。
1、如何不通过浏览器编程从网络中取得网页源码(不要求取得服务器端代码)?
2、如何编程从浏览器取得网页源码?
比如:我想自己写浏览器。
解决方案 »
- 请教如何修改已有的ActiveX,使新控件和旧控件可以同时使用
- GetLastError12150,HttpSendRepuest总是读头错误,这么简单竟然一个包都发不出去,崩溃中。。。。
- 请教一个问题
- 三个星星了,散分咯
- 数据变量大小的问题
- 乒乓球女单结束好长时间了,没看nlstone上来散分,我来散好了,不过等级低只能散100.
- 一个关于CAsyncSocket很菜的问题,在线结贴
- 如何用DirectX抓下屏幕内容,并直接将数据保留在一块内存中?
- 各路高手请留步----擂台编程
- 请教关于开发桌面应用,实现查询淘宝交易记录,买家访问记录等...
- 如何获得拨号连接时的事件通知(关于RasDial函数的使用方法)(在线等)
- 程序员,35岁以后做什么?
http://www.vckbase.com/document/viewdoc/?id=892
BOOL GetSourceHtml(CString theUrl,CString Filename)
{
CInternetSession session;
CInternetFile* file = NULL;
try
{
// 试着连接到指定URL
file = (CInternetFile*) session.OpenURL(theUrl);
}
catch (CInternetException* m_pException)
{
// 如果有错误的话,置文件为空
file = NULL;
m_pException->Delete();
return FALSE;
} // 用dataStore来保存读取的网页文件
CStdioFile dataStore;
if (file)
{
CString somecode; //也可采用LPTSTR类型,将不会删除文本中的\n回车符
BOOL bIsOk = dataStore.Open(strPath+"\\"+Filename,
CFile::modeCreate
| CFile::modeWrite
| CFile::shareDenyWrite
| CFile::typeText);
if (!bIsOk)
return FALSE;
// 读写网页文件,直到为空
while (file->ReadString(somecode) != NULL) //如果采用LPTSTR类型,读取最大个数nMax置0,使它遇空字符时结束
{
dataStore.WriteString(somecode);
dataStore.WriteString("\n"); //如果somecode采用LPTSTR类型,可不用此句
}
file->Close();
delete file;
}
else
{
dataStore.WriteString(_T("到指定服务器的连接建立失败..."));
return FALSE;
}
return TRUE;
}
bool SaveUrl(LPCTSTR url, LPCTSTR filename)
{
HINTERNET hNet = ::InternetOpen("Outlook",
PRE_CONFIG_INTERNET_ACCESS,
NULL,
INTERNET_INVALID_PORT_NUMBER,
0) ;HINTERNET hUrlFile = ::InternetOpenUrl(hNet,
url,
NULL,
0,
INTERNET_FLAG_RELOAD,
0) ;char buffer[10*1024] ;
DWORD dwBytesRead = 1;
BOOL bRead=TRUE;
CFile file;
file.Open(filename,CFile::modeCreate|CFile::modeWrite);
while(bRead&&dwBytesRead>0)
{
bRead = ::InternetReadFile(hUrlFile,
buffer,
sizeof(buffer),
&dwBytesRead);
if(dwBytesRead>0)
file.Write(buffer,dwBytesRead);
}
::InternetCloseHandle(hUrlFile) ;
::InternetCloseHandle(hNet) ;
file.Close();
AfxMessageBox("finished");
return bRead;
}
void CC02021101Dlg::OnOK()
{
// TODO: Add extra validation here
bool bret=SaveUrl("http://club.pchome.net/bbs2.php?topic=40&lanmuid=2","C:\\temp\\test.html");
if(bret)
AfxMessageBox("true");
else
AfxMessageBox("false");
}
#import <cdosys.dll> no_namespace rename("EOF", "EndOfFile")
............
void CSavemhtDlg::OnOK()
{
// save url as a single file, in fact I don't know if it is mht file, but it can be opened by IE, can someone tell me?
CoInitialize(NULL);
{
IMessagePtr iMsg(__uuidof(Message));
IConfigurationPtr iConf(__uuidof(Configuration));
iMsg->Configuration = iConf;
try
{
iMsg->CreateMHTMLBody(
"http://example.microsoft.com",
cdoSuppressNone,
"domain\\username",
"password");
}
catch(_com_error err)
{
// handle exception
}
_StreamPtr pStream=iMsg->GetStream();
pStream->SaveToFile("test.mht",adSaveCreateOverWrite);
}
CoUninitialize();
}
JS翻译了下:
http://dev.csdn.net/develop/article/18/18465.shtm
IHTMLDocument2* pHtmlDoc2 = (IHTMLDocument2*)GetHtmlDocument();
//check if HtmlDocument initialized
if( pHtmlDoc2 != NULL)
{IHTMLDocument3* pHTMLDoc3 = NULL;
HRESULT hr = pHtmlDoc2->QueryInterface(IID_IHTMLDocument3,
(LPVOID*)&pHTMLDoc3);
// ASSERT(SUCCEEDED(hr));
// {
if(pHTMLDoc3)
{
IHTMLElement* pDocElem=NULL;
hr = pHTMLDoc3->get_documentElement(&pDocElem);
if(pDocElem)
{// ASSERT(SUCCEEDED(hr));
BSTR bstrHTML;
pDocElem->get_outerHTML(&bstrHTML);
pDocElem->Release();USES_CONVERSION;
MessageBox(OLE2T(bstrHTML), _T("源文件"));
SysFreeString(bstrHTML);
}
pHTMLDoc3->Release();
}pHtmlDoc2->Release();
pHtmlDoc2=NULL;
}
#import "msxml2.dll"using namespace MSXML2;
int main(int argc, char* argv[])
{
printf("Test of XMLHTTP by masterz!\n");
CoInitialize(NULL);
try
{
IXMLHTTPRequestPtr xmlrequest;
xmlrequest.CreateInstance("Msxml2.XMLHTTP");
_variant_t varp(false);
xmlrequest->open(_bstr_t("GET"),_bstr_t("http://www.csdn.net/expert/topic/855/855052.xml?temp=.176037"),varp);
xmlrequest->send();
BSTR bstrbody;
xmlrequest->get_responseText(&bstrbody);
_bstr_t bstrtbody(bstrbody);
printf("%s\n",(LPCTSTR)bstrtbody);}
catch (_com_error &e)
{
printf("Description = '%s'\n", (char*) e.Description());
}
CoUninitialize();
printf("program end\n");
return 0;
}
要记得包含
#include "afxinet.h" CInternetSession mySession(NULL,0);
CHttpFile* myHttpFile=NULL; try
{
CString m_SiteInfo="";//页面源码 CString myData;
myHttpFile=(CHttpFile*)mySession.OpenURL("http://www.xxx.xxx/index.html");
while(myHttpFile->ReadString(myData))
{
m_SiteInfo=m_SiteInfo+"\r\n";
m_SiteInfo+=myData;
}
AfxMessageBox(m_SiteInfo); //显示页面源码
myHttpFile->Close();
mySession.Close();
}
catch (...)
{
myHttpFile->Close();
mySession.Close();
}
==============
///////////////////////////////////////////////////////////////////////////////////////
//click submit button of IE window
//If it works, it is written by masterz,otherwise I don't
//know who writes it^_^
///////////////////////////////////////////////////////////////////////////////////////
void CGetIESrcDlg::NavigateToUrl()
{
// Import the following files in your stdafx.h
// #import <mshtml.tlb> // Internet Explorer 5
// #import <shdocvw.dll>
// Refer to "Connect to Internet Explorer Instances, From your own Process. " in www.codeguru.com
SHDocVw::IShellWindowsPtr m_spSHWinds;
CoInitialize(NULL);
if(m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) == S_OK)
{
IDispatchPtr spDisp;
long nCount = m_spSHWinds->GetCount();
for (long i = 0; i < nCount; i++)
{
_variant_t va(i, VT_I4);
spDisp = m_spSHWinds->Item(va);
SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
if (spBrowser != NULL)
{
IDispatchPtr spDisp;
if(spBrowser->get_Document(&spDisp) == S_OK && spDisp!= 0 )
{
MSHTML::IHTMLDocument2Ptr spHtmlDocument(spDisp);
MSHTML::IHTMLElementPtr spHtmlElement;
if(spHtmlDocument==NULL)
continue;
spHtmlDocument->get_body(&spHtmlElement);
if(spHtmlDocument==NULL)
continue;
HRESULT hr;
MSHTML::IHTMLElementCollection* pColl=NULL;
hr=spHtmlDocument->get_all(&pColl);
if(pColl!=NULL&&SUCCEEDED(hr))
{
MSHTML::IHTMLElement* pElem=NULL;
_variant_t index;
index.vt=VT_I4;
index.intVal=0;
_variant_t name("Submit");
IDispatchPtr disp;
disp=pColl->item(name,index);
if(disp==NULL)
hr=E_FAIL;
else
{
hr=disp->QueryInterface(&pElem);
}
if (SUCCEEDED(hr)&& pElem != NULL)
{
//
BSTR bstrhtml;
pElem->get_outerHTML(&bstrhtml);
CString str(bstrhtml);
AfxMessageBox(str);
pElem->click();
pElem->Release();
}
pColl->Release();
}
}}
}}
else {
AfxMessageBox("Shell Windows interface is not avilable");
}
CoUninitialize();
}
///////////////////////////////////////////////////////////////////////////
//SDK post
///////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "winsock.h"
#pragma comment(lib,"ws2_32.lib")
#define winsock_version 0x0101
void main()
{
//I create C:\Inetpub\wwwroot\test\test.asp ,start the web service
//start my program, the result is OK.
//If it works,it is written by masterz,otherwise I don't know who write it.
SOCKADDR_IN saServer;
LPHOSTENT lphostent;
WSADATA wsadata;
SOCKET hsocket;
int nRet;
const char* host_name="127.0.0.1";
char* req="POST /test/test.asp HTTP/1.0\r\n"
"From: local\r\n"
"User-Agent: post_test/1.0\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Content-Length: 20\r\n\r\n"
"type=12345&name=aaaa";
if(WSAStartup(winsock_version,&wsadata))
printf("can't initial socket");
lphostent=gethostbyname(host_name);
if(lphostent==NULL)
printf("lphostent is null");
hsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
saServer.sin_family = AF_INET;
// Use def. now, need to handle general case
saServer.sin_port = htons(80);
saServer.sin_addr = *((LPIN_ADDR)*lphostent->h_addr_list);
nRet = connect(hsocket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN));
if (nRet == SOCKET_ERROR)
{
printf("can't connect");
closesocket(hsocket);
return;
}
else
printf("connected with %s\n",host_name);
nRet = send(hsocket, req, strlen(req), 0);
if (nRet == SOCKET_ERROR)
{
printf("send() failed");
closesocket(hsocket);}
else
printf("send() OK\n");
char dest[1000];
nRet=1;
while(nRet>0)
{
nRet=recv(hsocket,(LPSTR)dest,sizeof(dest),0);
if(nRet>0)
dest[nRet]=0;
else
dest[0]=0;
printf("\nReceived bytes:%d\n",nRet);
printf("Result:\n%s",dest);
}
}
比如:
1、如何联接网页/取得网页数据(这个楼上各位已经回答了)
2、如何发送交互信息,比如:按某键后怎么向服务端通信,在文本框中输入了信息怎么向服务端通信,怎么导向一个新的超级链接
3、怎么实现previous和next
4、怎么处理COOKIES
非常感谢楼上的各位网络大狼继续教诲。
再次感谢楼上热情回复的各位!
#include <stdio.h>
#include <afxinet.h>#define MAXBLOCKSIZE 1024void download(const char*);int main(int argc, char* argv[]) {
if(argc > 1) {
download((const char*)argv[1]);
} else {
printf("Usage: auto-Update url");
}
return 0;
}
/**
* 执行 文件下载 操作
Url
*
*/
void download(const char *Url)
{
printf("\n开始下载: %s\n",Url);
HINTERNET hSession = InternetOpen("RookIE/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession != NULL)
{
HINTERNET handle2 = InternetOpenUrl(hSession, Url, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
if (handle2 != NULL)
{
printf("%s\n",Url);
BYTE Temp[MAXBLOCKSIZE];
ULONG Number = 1;
FILE *stream;
if( (stream = fopen( "dow/My.exe", "wb" )) != NULL )
{
while (Number > 0)
{
InternetReadFile(handle2, Temp, MAXBLOCKSIZE - 1, &Number);
printf("%s",Temp);
fwrite(Temp, sizeof (char), Number , stream);
}
fclose( stream );
printf("\n下载完成\n\n");
}
InternetCloseHandle(handle2);
handle2 = NULL;
}
InternetCloseHandle(hSession);
hSession = NULL;
}
}