;win32汇编环境,网络编程入门之十三
;在这一教程里,我们学习一下如何利用 HttpQueryInfo 函数,测试一下所访问的网页是否存在
;一般情况下,返回404错误即意味着没有该网页,我们可以用这个HTTP的返回状态码来测试链接是否存在
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.386
.model flat,stdcall
option casemap:none
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Include 文件定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
include windows.inc
include user32.inc
include kernel32.inc
include wininet.inc ;需要添加的wininet头文件
includelib user32.lib
includelib kernel32.lib
includelib wininet.lib ;需要添加的wininet库文件
; 自定义函数声明
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
DlgProc proto :DWORD,:DWORD,:DWORD,:DWORD ;对话框窗口函数
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Equ 等值定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
MAINDIALOG equ 1
ICO_MAIN equ 1000 ;图标
ID_BUTTON01 equ 41
ID_EDIT01 equ 11
ID_EDIT02 equ 12
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 数据段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.data
szMsg db "提示",0
szFrm db "返回的状态码是: %s",13,10
db "如果是404,则没有这个网址页面",0
szErr db "错误",0
szEnter db 13,10,0 ;回车换行符
szAgent db "Microsoft Internet Explorer",0 ;骗网站说自已是IE浏览器
szHostName db "www.kepai2023.cn",0 ;要访问的主机域名
szUrlPath db "www.kepai2023.cn/asd/abc.php",0 ;要访问的页面,随便弄个不存在的网址测试一下
szVerb db "GET",0 ;GET方法访问
szAccept db "Accept: text/html",0 ;只接受text或html文件返回
.data?
hInstance HINSTANCE ?
hEdithwnd01 HWND ?
hEdithwnd02 HWND ?
hInternet dd ?
hHttpSession dd ?
hHttpRequest dd ?
.const
; 代码段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.code
start:
invoke GetModuleHandle, NULL
mov hInstance,eax
invoke DialogBoxParam, hInstance, MAINDIALOG,NULL, addr DlgProc, NULL
invoke ExitProcess,eax
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_WorkThread proc _lParam
LOCAL @dwBytesReaded
LOCAL @szBuffer[256]:byte
LOCAL @szRecBuffer[1024]:byte
LOCAL @szSaveBuffer[1024]:byte
LOCAL @szShowEdit02[256]:byte
LOCAL @stCR:CHARRANGE
LOCAL @szContentLength[128]:byte
LOCAL @szStatusCode[128]:byte
LOCAL @Totallength
invoke InternetOpen,addr szAgent,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0 ;初始化应用程序的 WinINet 函数的使用
mov hInternet,eax
invoke InternetConnect,hInternet,addr szHostName,80,NULL,NULL,INTERNET_SERVICE_HTTP,0,0 ;为给定站点打开文件传输协议(FTP)或 HTTP 会话,80是http的端口
mov hHttpSession,eax
.if eax == NULL
invoke MessageBox,NULL,addr szErr,addr szMsg,MB_OK
.endif
invoke HttpOpenRequest,hHttpSession,addr szVerb,addr szUrlPath,NULL,addr szAccept,0,0,0 ;创建 HTTP 请求句柄
mov hHttpRequest,eax
invoke HttpSendRequest,hHttpRequest,NULL,0,NULL,0 ;将指定的请求发送到 HTTP 服务器,可以附加数据,后面再说,先实现基本效果先
mov @Totallength,128 ;注意这里要先赋值为缓冲区的字节大小,不能使用sizeof前面缓冲区的大小,因为它还要保存其它的值
invoke HttpQueryInfo,hHttpRequest,HTTP_QUERY_STATUS_CODE,addr @szContentLength,addr @Totallength,NULL ;HttpQueryInfo 函数需要在HttpSendRequest函数调用成功之后使用,HTTP_QUERY_STATUS_CODE值为测试HTTP连接的返回状态码
invoke wsprintf,addr @szShowEdit02,addr szFrm,addr @szContentLength
invoke SendMessage,hEdithwnd02,WM_SETTEXT,0,addr @szShowEdit02
invoke SendMessage,hEdithwnd01,WM_SETTEXT,0,0
.while TRUE ;通过循环反复读取,直到读到0字节为止
invoke RtlZeroMemory,addr @szRecBuffer,sizeof @szRecBuffer
invoke RtlZeroMemory,addr @szSaveBuffer,sizeof @szSaveBuffer
mov @dwBytesReaded,0
invoke InternetReadFile,hHttpRequest,addr @szRecBuffer,1024,addr @dwBytesReaded ;从打开的句柄读取数据,
.break .if @dwBytesReaded == 0
invoke MultiByteToWideChar,65001,0,addr @szRecBuffer,1024,addr @szSaveBuffer,1024 ;将UTF8编码数据转为UNICODE编码
invoke GetWindowTextLength,hEdithwnd01 ;从后面添加内容,防止覆盖上去
mov @stCR.cpMin,eax
mov @stCR.cpMax,eax
invoke SendMessageW,hEdithwnd01,EM_EXSETSEL,0,addr @stCR
invoke SendMessageW,hEdithwnd01,EM_REPLACESEL,FALSE,addr @szSaveBuffer
.endw
ret
_WorkThread endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
DlgProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
LOCAL @stWsa:WSADATA
.if uMsg == WM_INITDIALOG
invoke LoadIcon,hInstance,ICO_MAIN
invoke SendMessage,hWnd,WM_SETICON,ICON_BIG,eax
invoke GetDlgItem,hWnd,ID_EDIT01
mov hEdithwnd01,eax
invoke GetDlgItem,hWnd,ID_EDIT02
mov hEdithwnd02,eax
.elseif uMsg == WM_COMMAND
mov ebx,wParam
.if bx == ID_BUTTON01
invoke CreateThread,NULL,0,offset _WorkThread,0,NULL,0 ;启动连接线程,用线程的原因是不让网络等待卡住主进程
.endif
.elseif uMsg == WM_CLOSE
invoke InternetCloseHandle,hHttpRequest ;反向清除各类HINTERNET 句柄,即先生成的后清除,后生成的先清除
invoke InternetCloseHandle,hHttpSession
invoke InternetCloseHandle,hInternet
invoke EndDialog,hWnd,NULL
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret
DlgProc endp
end start
;下面为rc文件内容
#include "resource.h" //提示缺少该文件,可以在资源里下载
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#define MAINDIALOG 1
#define ICO_MAIN 1000 //图标
#define ID_BUTTON01 41
#define ID_EDIT01 11 //编辑框标识符
#define ID_EDIT02 12
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ICO_MAIN ICON "Main.ico"
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//定义对话框
MAINDIALOG DIALOG 10, 10, 180, 250
STYLE DS_CENTER | WS_CAPTION | WS_MINIMIZEBOX |
WS_SYSMENU | WS_VISIBLE | WS_OVERLAPPED | DS_MODALFRAME | DS_3DLOOK
CAPTION "对话框程序模版"
FONT 11, "方正姚体"
BEGIN
PUSHBUTTON "连接网站", ID_BUTTON01, 120,20,50,12
CONTROL "这里显示的是服务器返回的数据",ID_EDIT01,"Edit",WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP|ES_MULTILINE|ES_WANTRETURN|ES_AUTOVSCROLL|WS_VSCROLL,10, 50, 160, 190,WS_EX_CLIENTEDGE //设置成多行编辑框,按回车时加回车符
CONTROL "显示HTTP返回信息",ID_EDIT02,"Edit",WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP|ES_MULTILINE|ES_WANTRETURN|ES_AUTOVSCROLL|WS_VSCROLL,10, 10, 100, 30,WS_EX_CLIENTEDGE
END