函数参考


_WinHttpSimpleFormFill

填写表单

#Include <WinHttp.au3>
_WinHttpSimpleFormFill(ByRef $hInternet [, $sActionPage = Default [, $sFormId = Default [, $sFieldId1 = Default [, $sData1 = Default [, (...)]]]]])

参数

$hInternet 由 _WinHttpConnect 函数返回的句柄 或者带表单的字符串变量.
$sActionPage [可选参数] 如果$hInternet为一个字符串则指定表单页面的路径,反之为一个会话句柄.(默认: "" - 空字符串;表示服务器上的'默认'页).
$sFormId [可选参数] 表单ID. 可以是一个名称或者基于0开始的一个索引(参考备注说明).
$sFieldId1 [可选参数] 输入框ID.
$sData1 [可选参数] 要设置到相应字段的数据.
(...) [可选参数] 其它部分的ID/数据. 字段总数不能超过40.

返回值

成功: 返回由已提交表单的服务器返回页面的HTML源代码.
失败: 返回空字符串并设置 @error:
1 - 页面上没有表单
2 - 无效表单
3 - 页面上没有指定属性的表单
4 - 连接问题
5 - 表单 "动作" 无效
6 - 传递了一个无效会话句柄

注意/说明

In case form requires redirection and $hInternet is internet handle, this handle will be closed and replaced with new and required one.

When $hInternet is form string, form's "action" must specify URL and $sActionPage parameter must be session handle. On succesful call this variable will be changed to connection handle of the internally made connection.
Don't forget to close this handle after the function returns and when no longer needed.

$sFormId specifies Id of the form same as .getElementById(FormId). Aditionally you can use "index:FormIndex" to
identify form by its zero-based index number (in case of e.g. three forms on some page first one will have index=0, second index=1, third index=2).
Use "name:FormName" to identify form by its name like with .getElementsByName(FormName). FormName will be taken to be what's right of colon mark.
In that case first form with that name is filled.

As for fields, If "name:FieldName" option is used all the fields except last with that name are removed from the form. Last one is filled with specified $sData data.

This function can be used to fill forms with up to 40 fields at once.

"Submit" control you want to keep (click) set to True. If no such control is set then the first one found in the form is "clicked"
and the other removed from the submited form. "Checkbox" and "Button" input types are removed from the submitted form unless explicitly set. Same goes for "Radio" with exception that
only one such control can be set, the rest are removed. These controls are set by their values. Wrong value makes them invalid and therefore not part of the submited data.

All other non-set fields are left default.



If this function is used to upload multiple files then there are two available ways. Default would be to submit the form following RFC2388 specification.
In that case every file is represented as multipart/mixed part embedded within the multipart/form-data.

If you want to upload using alternative way (to avoid certain PHP bug that could exist on server side) then prefix the file string with "PHP#50338:" string.

For example: ..."name:files[]", "PHP#50338:" & $sFile1 & ...

Muliple files are always separated with vertical line ASCII character when filling the form.

相关

_WinHttpConnect

示例/演示


#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

#include "WinHttp.au3"

Opt("MustDeclareVars", 1)

Global $hOpen, $hConnect
Global $sRead, $hFileHTM, $sFileHTM = @ScriptDir & "\Form.htm"

; 示例1:
;    1. 打开 APNIC whois 页面 (http://wq.apnic.net/apnic-bin/whois.pl)
;    2. 使用这些值/条件填写那个页面的表单:
;         - 填写默认表单
;         - 设置 IP 地址 4.2.2.2 到输入框. 使用名称属性定位输入
;         - 发送表单 (点击按钮)
;         - 采集返回的数据

; 初始化并获取会话句柄
$hOpen = _WinHttpOpen()
; 获取连接句柄
$hConnect = _WinHttpConnect($hOpen, "wq.apnic.net")
; 填写此页面的表单
$sRead = _WinHttpSimpleFormFill($hConnect, "apnic-bin/whois.pl", Default, "name:searchtext", "4.2.2.2")
; 关闭连接句柄
_WinHttpCloseHandle($hConnect)
; 关闭会话句柄
_WinHttpCloseHandle($hOpen)

; 看看返回了什么 (在默认浏览器或其他什么的)
If $sRead Then
    MsgBox(64 + 262144, "Done!", "Will open returned page in your default browser now." & @CRLF & _
            "When you close that window another example will run.")
    $hFileHTM = FileOpen($sFileHTM, 2)
    FileWrite($hFileHTM, $sRead)
    FileClose($hFileHTM)
    ShellExecuteWait($sFileHTM)
EndIf


;=====================================================================================================================
If MsgBox(262148, "Example 2", "Run new example?") = 7 Then Exit

; 示例 2:
;    1. 打开 w3schools 表单页面 (http://www.w3schools.com/html/html_forms.asp)
;    2. 使用这些值/条件填写那个页面的表单:
;         - 表单是根据其名称标识的, 名称为 "input0"
;         - 设置 "OMG!!!" 数据到输入框. 根据名称找到输入框. 名称为 "user"
;         - 采集数据

; 初始化并获取会话句柄
$hOpen = _WinHttpOpen()
; 获取连接句柄
$hConnect = _WinHttpConnect($hOpen, "w3schools.com")
; 填写此页面的表单
$sRead = _WinHttpSimpleFormFill($hConnect, "html/html_forms.asp", "name:input0", "name:user", "OMG!!!")
; 关闭连接句柄
_WinHttpCloseHandle($hConnect)
; 关闭会话句柄
_WinHttpCloseHandle($hOpen)

If $sRead Then
    MsgBox(64 + 262144, "Done!", "Will open returned page in your default browser now." & @CRLF & _
            "You should see 'OMG!!!' or 'OMG%21%21%21' (encoded version) somewhere on that page.")
    $hFileHTM = FileOpen($sFileHTM, 2)
    FileWrite($hFileHTM, $sRead)
    FileClose($hFileHTM)
    ShellExecuteWait($sFileHTM)
EndIf


;=====================================================================================================================
If MsgBox(262148, "Example 3", "Run new example?") = 7 Then Exit

; 示例 3:
;    1. Open cs.tut.fi forms page (http://www.cs.tut.fi/~jkorpela/forms/testing.html)
;    2. 使用这些值/条件填写那个页面的表单:
;         - 表单是根据其索引标识的, 这是页面上的首个表单, 即索引为 0
;         - 设置 "Johnny B. Goode" 数据到文本区. 根据 "Comments" 名称找到它.
;         - 选中复选框. 根据名称 "box" 找到它. 选中的值为 "yes".
;         - 设置 "This is hidden, so what?" 数据到由 "hidden field" 名称标识的输入字段.
;         - 采集数据

; 初始化并获取会话句柄
$hOpen = _WinHttpOpen()
; 获取连接句柄
$hConnect = _WinHttpConnect($hOpen, "www.cs.tut.fi")
; 填写此页面的表单
$sRead = _WinHttpSimpleFormFill($hConnect, "~jkorpela/forms/testing.html", "index:0", "name:Comments", "Johnny B. Goode", "name:box", "yes", "name:hidden field", "This is hidden, so what?")
; 关闭连接句柄
_WinHttpCloseHandle($hConnect)
; 现在关闭不再需要的会话句柄
_WinHttpCloseHandle($hOpen)

If $sRead Then
    MsgBox(64 + 262144, "Done!", "Will open returned page in your default browser now." & @CRLF & _
            "It should show sent data.")
    $hFileHTM = FileOpen($sFileHTM, 2)
    FileWrite($hFileHTM, $sRead)
    FileClose($hFileHTM)
    ShellExecuteWait($sFileHTM)
EndIf


;=====================================================================================================================
If MsgBox(262148, "Example 4", "Run new example?") = 7 Then Exit

; 示例 4:
;    1. 打开雅虎邮箱登录页面 (https://login.yahoo.com/config/login_verify2?&.src=ym)
;    2. 使用这些值/条件填写那个页面的表单:
;         - 表单是根据其名称标识的, 名称为 "login_form"
;         - 设置 "MyUserName" 数据到用户名输入框. 根据其 ID 找到输入框. 它是 "username"
;         - 设置 "MyPassword" 数据到密码输入框. 根据其 ID 找到输入框. 它是 "passwd"
;         - 采集数据

; 初始化并获取会话句柄
$hOpen = _WinHttpOpen()
; 获取连接句柄
$hConnect = _WinHttpConnect($hOpen, "login.yahoo.com")
; 填写此页面的表单
$sRead = _WinHttpSimpleFormFill($hConnect, "config/login_verify2?&.src=ym", "name:login_form", "username", "MyUserName", "passwd", "MyPassword")
;关闭连接句柄
_WinHttpCloseHandle($hConnect)
; 关闭会话句柄
_WinHttpCloseHandle($hOpen)

;打印返回的:
ConsoleWrite($sRead & @CRLF)
MsgBox(262144, "The End", "Source of the last example is printed to console." & @CRLF & _
 "In case valid login data was passed it will be user's mail page on yahoo.mail")