#include <Array.au3>
#include <WinHttp_GetRespond.au3>
$s_PicFileName=@ScriptDir &"\key2.bmp"
If FileExists($s_PicFileName) Then FileDelete($s_PicFileName)
$MyOpen=_WinHttpOpen()
Local $Request[3]=["$hRequest","$hConnect",$s_PicFileName] ;-----该参数与 _DownLoadByResPodn 的参数相对应
_WinHTTP_GetRespond($MyOpen,"http://www.29cy.com/getcode.asp",0 ,-2,0,12000,"","","","_DirectHandle",$Request)
If @error Then MsgBox(0,"",@error)
_WinHttpCloseHandle($MyOpen)
Func _DirectHandle($hRequest,$hConnect,$SaveFile)
$rDate= _WinHttpReadData($hRequest,2, _WinHttpQueryHeaders($hRequest,$WINHTTP_QUERY_CONTENT_LENGTH))
$Cookies=_WinHttpQueryHeaders($hRequest,$WINHTTP_QUERY_SET_COOKIE)
$Cookies=StringRegExp($Cookies,"(.*?;)",3)
$Cookies=$Cookies[0]
;~ MsgBox(0,"",$rContext)
$File=FileOpen($SaveFile,18)
FileWrite($File,$rDate)
FileClose($File)
_WinHttpCloseHandle($hRequest)
;~ Return
Dim $s_GetOcrString = _myConvertOcr($s_PicFileName)
If $s_GetOcrString = "" Then; 或 If @error Then
MsgBox(0, "提示", "出现错误")
Exit
EndIf
;以下是返回结果情况
ConsoleWrite($s_GetOcrString & @CRLF);显示原串,形如:8(100%)5(100%)8(100%)6(100%)8(100%),括号内为识别情况,100%表示完全匹配
;或,不严格的正则,如\d+实际范围是0-100, 如考虑小数点,简单的写法是\d+改为\d+(?:\.\d+)?
ConsoleWrite(StringRegExpReplace($s_GetOcrString, "\(\d+%\)", "") & @CRLF);
;或,不严格的正则,如\d+实际范围是0-100
ConsoleWrite(StringRegExpReplace($s_GetOcrString, "(\S)\(\d+%\)", "$1") & @CRLF);
$sFlags = Default
$hRequest = _WinHttpOpenRequest($hConnect, "POST", "/chklogin.asp",Default,Default,Default,$sFlags)
If @error Or $hRequest = 0 Then MsgBox(0,@error,_GetLastError())
$hTimeOut = 12000
_WinHttpSetTimeouts($hRequest,$hTimeOut,$hTimeOut,$hTimeOut,$hTimeOut + 5000)
Dim $aOcrString = StringRegExp($s_GetOcrString, "(\S)\(\d+%\)", 3)
_ArrayDisplay($aOcrString);分解每个字符
$Context = "title=%C2%CC%C9%AB%D7%D4%D6%FA%C1%B4&leibie=111&qq=320134733&Email=320134733@qq.com&url=http%3A%2F%2Fwww.aqmh.net&logo=&memo=%C2%CC%C9%AB%D7%D4%D6%FA%C1%B4&mofei=" & $aOcrString[0] & $aOcrString[1] & $aOcrString[2] & $aOcrString[3] & "&action=add_wz"
_WinHttpAddRequestHeaders($hRequest, "Content-Length: "&StringLen($ConText)&@CRLF)
_WinHttpAddRequestHeaders($hRequest, "Cookie: "&$Cookies&@CRLF)
_WinHttpAddRequestHeaders($hRequest,"Content-Type: application/x-www-form-urlencoded")
_WinHttpSendRequest($hRequest)
If @error Then Return MsgBox(48,2,_GetLastError())
_WinHttpWriteData($hRequest,$Context)
_WinHttpReceiveResponse($hRequest)
If Not _WinHttpQueryDataAvailable($hRequest) Then Return MsgBox(48,3,_GetLastError())
$rDate= _WinHttpReadData($hRequest,2, _WinHttpQueryHeaders($hRequest,$WINHTTP_QUERY_CONTENT_LENGTH))
MsgBox(0,"",BinaryToString($rDate))
FileWrite("test.html",$rDate)
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
EndFunc
Func _myConvertOcr($s_PicFileName, $s_OcrToolPath = @ScriptDir)
If Not FileExists($s_PicFileName) Then Return SetError(1, 0, "");要转换的图形文件没找到
If StringRight($s_OcrToolPath, 1) = "" Then $s_OcrToolPath = StringTrimRight($s_OcrToolPath, 1)
If (Not FileExists($s_OcrToolPath & "\Convert.ini")) Or (Not FileExists($s_OcrToolPath & "\Ocr.exe")) Then Return SetError(1, 0, "");OCR工具或静默参数设置未找到
Local $s_IniFile = $s_OcrToolPath & "\Convert.ini" ;由ocr.exe生成的静默参数文件
IniWrite($s_IniFile, "Setup", "CheckCodeFile", $s_PicFileName)
#cs
;原调用方式
FileDelete($s_OcrToolPath&"\CheckCode.Txt")
FileDelete($s_OcrToolPath&"\Error.Log")
Run($s_OcrToolPath&"\Ocr.exe Convert");原版本未做RunWait改造
While 1
If FileExists($s_OcrToolPath&"\CheckCode.Txt") Then Return $s_OcrToolPath&"\CheckCode.Txt"
If FileExists($s_OcrToolPath&"\Error.Log") Then Return SetError(1, 0, "")
Sleep(10)
WEnd
#ce
RunWait($s_OcrToolPath & "\Ocr.exe Ocr");
Select
Case FileExists($s_OcrToolPath & "\CheckCode.Txt")
Return FileRead($s_OcrToolPath & "\CheckCode.Txt");成功返回
Case Else;或读取$s_OcrToolPath&"\Error.Log"内容做进一步错误分析
Return SetError(1, 0, "")
EndSelect
EndFunc ;==>_myConvertOcr