用MASM32编程获取网站首页文件名并读取首页代码

用MASM32编程读取网站首页代码

记得在某个汇编论坛上有网友讨论如何获取一个网站的首页文件名,是index.htm,index.html,index.asp,还是……
于是动手写了这个程序读取网站的返回信息来做测试,发现有些网站的返回信息中的Content-Location值包含了首页文件名,如www.hcny.gov.cn
/---
HTTP/1.1 200 OK
Content-Length: 34432
Content-Type: text/html
Content-Location: http://www.hcny.gov.cn/index.htm
Last-Modified: Wed, 05 Dec 2007 02:59:18 GMT
Accept-Ranges: bytes
ETag: "244d2d3ea36c81:80a"
Server: Microsoft-IIS/6.0
Date: Wed, 05 Dec 2007 15:05:19 GMT
---/


有些网站则没有,如www.163.com
/---
HTTP/1.0 200 OK
Date: Wed, 05 Dec 2007 15:01:13 GMT
Server: Apache/2.0.59 (Unix)
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Length: 127476
Content-Type: text/html; charset=GB2312
Age: 265
X-Cache: HIT from www.163.com
Connection: keep-alive
---/

另外 IE 从 SP2 开始不支持 view-source 了,为了使用 view-source 而装一个 FireFox 似乎有点麻烦,于是加了读取网页代码的功能。目前是读取网站的首页的代码。
其中的一些代码参考了MASM32官方论坛的贴子:
http://www.masm32.com/board/index.php?topic=8197.0

增加了SearchStr()来搜索网页代码结束位置后,程序运行时间要多久一些,改进的方法是参考网站的返回信息中的Content-Length的值,在最后一次从Sock读取数据时进行进行搜索……

;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;FileName:WebHome.asm
;Author:PurpleEndurer
;Functiion:Readawebhomepagecontent
;DevEnv:WinXPSP2+MASM32v8

;log
;------------------------------
;2007-12-05AddedSearchStr()tosearchthewebpagecodeendmarkstring
;2007-12-04Canreadwebhomepagecontent
;2007-12-03Created!
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
.386
.model flat, stdcall
option casemap: none

include/masm32/ include/windows.inc

include/masm32/ include/kernel32.inc
includelib/masm32/ lib/kernel32.lib

include/masm32/ include/user32.inc
includelib/masm32/ lib/user32.lib

include/masm32/ include/shell32.inc
includelib/masm32/ lib/shell32.lib

include/masm32/ include/wsock32.inc
includelib/masm32/ lib/wsock32.lib


WinMain PROTO: DWORD,: DWORD,: DWORD,: DWORD
btnShow_Click PROTO
fInitInternet proto: dword
fConnect protolpszHostName: dword,nPortNumber: dword
SendHttpHead proto:LPSTR,:LPSTR
ReadSockData proto:LPSTR,: dword
ResizeConctrol proto: DWORD
SearchStr proto:LPSTR,:LPSTR


m_GotoEnd MACRO
invokeSendMessage,g_hEditVerInfo,EM_SETSEL,0,-1
invokeSendMessage,g_hEditVerInfo,EM_SETSEL,-1,0
ENDM

m_InsStr MACROlpszStr:REQ
invokeSendMessage,g_hEditVerInfo,EM_REPLACESEL, FALSE,lpszStr
ENDM

m_GoNextLine MACRO
m_InsStr addrg_szCR
ENDM

m_InsCrStr MACROlpszStr:REQ
m_GoNextLine
m_InsStrlpszStr
ENDM


WM_FSOCKET equWM_USER+0fh
IDC_BtnRead equ103
IDC_EdtURL equ105
IDC_EdtHTML equ107

c_BufLen equ5000h
c_Resize equ1
d_TestData equ1

c_EditURLLeft equ2
c_EditURLTop equ5
c_EditURLWidth equ500
c_EditURLHeight equ20

c_BtnReadTop equc_EditURLTop
c_BtnReadLeft equ(c_EditURLLeft+c_EditURLWidth+10)
c_BtnReadWidth equ70
c_BtnReadHeight equc_EditURLHeight

c_EdtHTMLLeft equ2
c_EdtHTMLTop equ30
c_EdtHTMLWidth equc_BtnReadLeft+c_BtnReadWidth+2
c_EdtHTMLHeight equ300

c_WinWidth equc_EdtHTMLLeft+c_EdtHTMLWidth+10
c_WinHeight equc_EdtHTMLTop+c_EdtHTMLHeight+30

c_RichEditStyle equWS_CHILD orWS_VISIBLE orES_MULTILINE orWS_VSCROLL orES_AUTOVSCROLL orWS_HSCROLL orES_NOHIDESEL orES_SAVESEL orES_SELECTIONBAR

MAX_STRING_LEN equ8192 ;2000h
SYSINFO_RET_OK equ1
SYSINFO_RET_FAIL equ2

;sssssssssss
.data
;sssssssssss
g_szClsNamelabel byte
g_szAppName db "HttpDemo",0
ifd_TestDataeq1
g_szTestURL db "www.hcny.gov.cn",0
endif ;d_TestData
g_szFailIni db "Failinitializeinternetconnection!",0
g_szFailGetHostName db "Failtogethostname!",0
g_szConnect db "Failtoconnect!",0
g_szFailWSAStartup db "FailtoWSAStartup",0
g_szEnterURL db "请先输入URL!",0

g_szEditCls db "EDIT",0
g_szBtnCls db "button",0

g_szNoRichEdit db "无法载入"
g_szRichEditDLL db "RICHED20.DLL",0
g_szRichEditClass db "RichEdit20A",0

g_szBtnReadText db "&R读取",0
g_szFmt1 db "GET/%sHTTP/1.1",0dh,0ah
db "Host:%s",0dh,0ah
db "Accept:*/*",0dh,0ah
db "User-Agent:Mozilla/4.0"
db "(compatible;MSIE6.00;Windows2000)",0dh,0ah
db "Connection:Keep-Alive",0dh,0ah
db0dh,0ah
g_szCR db0dh,0ah,0

g_szHTTP400 db "HTTP/1.1400BadRequest",0
g_szFmt2 db "WSACleanupfailedwitherror%d",0

;sssssssssss
.data?
;sssssssssss
g_hInstanceHINSTANCE?
g_hWndMainHANDLE?
g_hEditURLHANDLE?
g_hBtnReadHANDLE?
g_hEditVerInfoHANDLE?
g_hRichEditDLLHANDLE?

wsadataWSADATA<>
sinsockaddr_in<>
sock dd?
error_code dd?
g_szURL db256dup(?)
g_buf bytec_BufLendup(?)


;sssssssssss
.code
;sssssssssss
start:
invokeGetModuleHandle,NULL
movg_hInstance, eax
invokeLoadLibrary, OFFSETg_szRichEditDLL
.if eax!=0
movg_hRichEditDLL, eax
invokeWinMain,g_hInstance,NULL,NULL,SW_SHOWDEFAULT
invokeFreeLibrary,g_hRichEditDLL
.else
invokeMessageBox,0, OFFSETg_szNoRichEdit,/
OFFSETg_szAppName,MB_OK orMB_ICONERROR
.endif

invokeExitProcess, eax

WinMain prochInst: DWORD,hPrevInst: DWORD,CmdLine: DWORD,CmdShow: DWORD
LOCALwc:WNDCLASSEX
LOCALmsg:MSG
LOCALhwnd:HWND

movwc.cbSize,SIZEOFWNDCLASSEX
movwc.style,CS_HREDRAW orCS_VREDRAW
movwc.lpfnWndProc, OFFSETWndProc
movwc.cbClsExtra,NULL
movwc.cbWndExtra,NULL
mov eax,g_hInstance
movwc.hInstance, eax
movwc.hbrBackground,COLOR_APPWORKSPACE
movwc.lpszMenuName,NULL
movwc.lpszClassName, OFFSETg_szClsName
invokeLoadIcon,NULL,IDI_APPLICATION
movwc.hIcon, eax
movwc.hIconSm, eax
invokeLoadCursor,NULL,IDC_ARROW
movwc.hCursor, eax
invokeRegisterClassEx, addrwc
invokeCreateWindowEx,NULL, ADDRg_szClsName, ADDRg_szAppName,/
WS_OVERLAPPEDWINDOW+WS_VISIBLE,CW_USEDEFAULT,CW_USEDEFAULT,/
c_WinWidth,c_WinHeight,NULL,NULL,hInst,NULL
movhwnd, eax
.while TRUE
invokeGetMessage, ADDRmsg,NULL,0,0
.BREAK .IF(! eax)

;---processkeystrokesdirectlyinthemessageloop
.ifmsg.message==WM_SYSKEYUP
.ifmsg.wParam==VK_R ;Alt+R
invokePostMessage,hwnd,WM_COMMAND,IDC_BtnRead,BM_CLICK
.endif
.endif
;------------------------------------------------
invokeTranslateMessage, ADDRmsg
invokeDispatchMessage, ADDRmsg
.endw
mov eax,msg.wParam
ret
WinMain endp


WndProc prochWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
LOCALrect:RECT
LOCALhdc: DWORD

.ifuMsg==WM_CREATE
mov eax,hWnd
movg_hWndMain, eax

;---CreateURLeditbox
invokeCreateWindowEx,NULL, offsetg_szEditCls,NULL,
WS_CHILD+WS_VISIBLE+ES_AUTOHSCROLL+WS_BORDER,/
c_EditURLLeft,c_EditURLTop,c_EditURLWidth,c_EditURLHeight,/
hWnd,IDC_EdtURL,g_hInstance,NULL
movg_hEditURL, eax

;---Createreadbutton
invokeCreateWindowEx,NULL, offsetg_szBtnCls, offsetg_szBtnReadText,/
WS_CHILD+WS_VISIBLE,c_BtnReadLeft,c_BtnReadTop,c_BtnReadWidth,c_BtnReadHeight,/
g_hWndMain,IDC_BtnRead,g_hInstance,NULL
movg_hBtnRead, eax

;---Createfileverinfoeditbox
;invokeCreateWindowEx,NULL,addrg_szEditCls,NULL,/
;WS_CHILD+WS_VISIBLE+ES_MULTILINE+WS_HSCROLL+WS_VSCROLL+WS_BORDER,/
;c_EdtHTMLLeft,c_EdtHTMLTop,c_EdtHTMLWidth,c_EdtHTMLHeight,/
;g_hWndMain,IDC_EdtHTML,g_hInstance,NULL
invokeCreateWindowEx,WS_EX_CLIENTEDGE, OFFSETg_szRichEditClass,/
NULL,c_RichEditStyle,c_EdtHTMLLeft,c_EdtHTMLTop,c_EdtHTMLWidth,c_EdtHTMLHeight,/
hWnd,IDC_EdtHTML,g_hInstance,0
movg_hEditVerInfo, eax

;---Setthetextlimit.Thedefaultis64K
invokePostMessage,g_hEditVerInfo,EM_LIMITTEXT,-1,0

ifd_TestDataeq1
invokeSetWindowText,g_hEditURL, OFFSETg_szTestURL
endif

.elseifuMsg==WM_COMMAND
.IFlParam
mov edx,wParam
mov eax, edx
shr edx,16
.if dx==BN_CLICKED
.IF ax==IDC_BtnRead
invokeGetWindowText,g_hEditURL, addrg_szURL,sizeofg_szURL
test eax, eax
.ifZERO?
invokeMessageBox,g_hWndMain, addrg_szEnterURL, addrg_szAppName,MB_ICONQUESTION
.else
;invokeMessageBox,g_hWndMain,addrg_szURL,addrg_szAppName,MB_ICONQUESTION
invokebtnShow_Click
.endif
.endif
.ENDIF
.endif
.elseifuMsg==WM_DESTROY
invokePostQuitMessage,NULL
ifc_Resizeeq1
.elseifuMsg==WM_SIZE
invokeResizeConctrol,lParam
xor eax, eax
jz@F
endif
.else
@@:
invokeDefWindowProc,hWnd,uMsg,wParam,lParam
ret
.endif
xor eax, eax
ret
WndProc endp


btnShow_Click proc
;---FirstIinitializetheinternetandgetthesocketusingthiscode.
invokefInitInternet,g_hWndMain
test eax, eax
.if!ZERO?
invokeMessageBox,g_hWndMain, eax, addrg_szAppName,0
.else

;---Second,Iconnectthesocketusingthiscode
invokefConnect, addrg_szURL,80
test eax, eax
.if!ZERO?
invokeMessageBox,g_hWndMain, eax, addrg_szAppName,0
.else
invokeSendHttpHead, addrg_szURL,NULL

;---Readhttpreponseheadmsg
invokeReadSockData, addrg_buf,c_BufLen
mov eax, offsetg_buf
mov edi, eax
add edi,c_BufLen-1
.while( dword ptr[ eax]!=0a0d0a0dh)&&( eax< edi)
inc eax
.endw


.if( eax< edi)
add eax,3 ;inceax
mov byte ptr[ eax],0
inc eax

;pusheax
;invokeMessageBox,g_hWndMain,eax,addrg_szAppName,0
;popeax

.else
xor eax, eax
.endif
push eax
m_InsCrStr addrg_buf

;---writehttpbodymsgfollowinghttpreponseheadmsg
pop eax
test eax, eax
jz@btnShow_ClickReadBody
m_InsStr eax
@btnShow_ClickReadBody:
;---Readhttpbodymsg
invokeReadSockData, addrg_buf,c_BufLen
push eax

;---Searchtheendmarkstring
invokeSearchStr, addrg_buf, addrg_szHTTP400
inc eax
jz@F ;nofound

dec eax
mov byte ptr[g_buf+ eax],0
@@:
;---writehttpbodymsg
m_InsCrStr addrg_buf

pop eax
cmp eax,SOCKET_ERROR
je@F
test eax, eax ;cmpeax,0
jnz@btnShow_ClickReadBody ;jg@btnShow_ClickReadBody
@@:
invokeclosesocket,sock

invokeWSACleanup
.if( eax==SOCKET_ERROR)
invokeWSAGetLastError
invokewsprintf, addrg_buf, addrg_szFmt2, eax
;invokeMessageBox,g_hWndMain,addrg_buf,addrg_szAppName,0
m_InsCrStr addrg_buf
.endif
.endif
.endif

ret
btnShow_Click endp


;eax==0,sucess
fInitInternet prochWnd: dword
;Madwizardtutorial
invokeWSAStartup,101h, addrwsadata
test eax, eax
jz@F
mov eax, offsetg_szFailWSAStartup
jmp@fInitInternetRet
@@:
;---Createastreamsocketforinternetuse
invokesocket,AF_INET,SOCK_STREAM,0 ;AF_UNSPEC;IPPROTO_TCP
;sock=socket(AF_INET,SOCK_STREAM,0);

.if eax!=INVALID_SOCKET
movsock, eax
;invokeWSAAsyncSelect,sock,hWnd,WM_FSOCKET,FD_CONNECT+FD_READ+FD_CLOSE+FD_WRITE+FD_ACCEPT
xor eax, eax
.else
mov eax, offsetg_szFailIni
.endif

@fInitInternetRet:
ret
fInitInternet endp


;eax==0,sucess
fConnect proc uses esi edilpszHostName: dword,nPortNumber: dword
invokegethostbyname,lpszHostName
cmp eax,NULL
jne@F
mov eax, offsetg_szFailGetHostName
jmp@fConnectRet
@@:

;pusheax
;invokeMessageBox,g_hWndMain,(hostentptr[eax]).h_name,addrg_szAppName,MB_OK
;popeax

mov eax,(hostent ptr[ eax]).h_list ;moveax,[eax+12]

mov eax,[ eax] ;copythepointertotheactualIPaddressintoeax
mov eax,[ eax] ;copyIPaddressintoeax
movsin.sin_addr.S_un.S_addr, eax ;movsin.sin_addr,eax

movsin.sin_family,AF_INET
invokehtons,nPortNumber
movsin.sin_port, ax

invokeconnect,sock, addrsin,sizeofsin
.if eax==SOCKET_ERROR
invokeWSAGetLastError
mov eax, offsetg_szConnect
.endif
@fConnectRet:
ret
fConnect endp


SendHttpHead proclpszHostName:LPSTR,lpParam:LPSTR
invokewsprintf, addrg_buf, addrg_szFmt1,lpParam,lpszHostName
m_InsStr addrg_buf
;invokeMessageBox,g_hWndMain,addrg_buf,addrg_szAppName,0

invokesend,sock, addrg_buf,sizeofg_buf,0
ret
SendHttpHead endp


ReadSockData proclpszBuffer:LPSTR,dwMax_buf_len: dword
invokeRtlZeroMemory,lpszBuffer,dwMax_buf_len
mov edi,lpszBuffer
mov esi,dwMax_buf_len
dec esi
@@:
push esi
push edi
invokerecv,sock, edi, esi,0
pop edi
pop esi
add edi, eax
sub esi, eax
jz@get_http_pageRet
;pushad
;invokeMessageBox,g_hWndMain,addrg_szAppName,addrg_szAppName,0
;popad

cmp eax,0
jg@B

@get_http_pageRet:
ret
ReadSockData endp


;ifeax=-1,nofound
;elseeax=substringposition
SearchStr proclpszOrgStr:LPSTR,lpszSubStr:LPSTR
localdwPos: dword

mov edi,lpszSubStr
cmp byte ptr[ edi],0
je@NoFound

mov esi,lpszOrgStr
movdwPos, esi

@SearchStrLoop1Begin:
mov al, byte ptr[ esi]
test al, al
jz@NoFound

cmp al, byte ptr[ edi]
jne@SearchStrLoop1Next

@SearchStrLoop2Begin:
inc esi
inc edi

mov ah, byte ptr[ edi]
test ah, ah
jz@Found

mov al, byte ptr[ esi]
test al, al
jz@NoFound

cmp al, ah
je@SearchStrLoop2Begin

mov edi,lpszSubStr
@SearchStrLoop1Next:
incdwPos
mov esi,dwPos

jmp@SearchStrLoop1Begin

@NoFound:
xor eax, eax ;moveax,-1
dec eax
jmp@SearchStrRet
@Found:
mov eax,dwPos
sub eax,lpszOrgStr
@SearchStrRet:
ret
SearchStr endp


ifc_Resizeeq1

ResizeConctrol PROCwh: DWORD
;---Getmainwindowwidth
mov eax,wh
mov ecx, eax

movzx eax, ax ;width
push eax ;pushforresizingtheverinfoedit

sub eax,4
shr ecx,16 ;height
sub ecx,c_EditURLHeight+10

invokeMoveWindow,g_hEditVerInfo,2,c_EdtHTMLTop, eax, ecx, FALSE

;---ResizetheGetbutton
pop eax
sub eax,5+c_BtnReadWidth
push eax
invokeMoveWindow,g_hBtnRead, eax,c_BtnReadTop,c_BtnReadWidth,c_BtnReadHeight, TRUE

;---Resizethefilespeceditbox
pop eax
sub eax,10
invokeMoveWindow,g_hEditURL,c_EditURLLeft,c_EditURLTop, eax,c_EditURLHeight, TRUE

ret
ResizeConctrol ENDP

endif ;c_Resize

endstart
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值