Я использую Excel VBA для автоматизации работы браузера: вход в систему, публикация http, выход из системы с помощью WinHTTP. Все запросы в vba имитируют запрос Curl. Для входа в систему и выхода из системы работает нормально, но для сообщения http, тогда как при использовании multipart / form-data, это всегда дает время ожидания.
В почтовых данных есть много параметров, один из которых имеет: Content-Type: text / plain.
Это VBA:
Function getDatafromFile() As String
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim sourceFile As Object
Dim myFilePath As String
Dim myFileText As String
myFilePath = "C:\users\tmpfile\post.txt"
Set sourceFile = fso.OpenTextFile(myFilePath, ForReading)
getDatafromFile = sourceFile.ReadAll ' myFileText now contains the content of the text file
sourceFile.Close ' close the file
End Function
Sub singleRun()
Dim dteStart As Date
Dim dteFinish As Date
'Dim objHttp As WinHttp.WinHttpRequest
'Dim objDoc As HTMLDocument
Dim strResponse As String
Dim kue As String
On Error GoTo ExitFunction
'addReference
Dim objHttp As Object
'Set objHttp = New MSXML2.ServerXMLHTTP60
Set objHttp = New WinHttp.WinHttpRequest
With objHttp
.Open "get", "http://192.168.1.9/smsb/main.php?act=logout_act", False
.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0"
.setRequestHeader "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
.setRequestHeader "Accept-Language", "en-US,en;q=0.5"
.setRequestHeader "Connection", "keep-alive"
.setRequestHeader "Referer", "http://192.168.1.9/smsb/index.php?act=login_act"
.setRequestHeader "Upgrade-Insecure-Requests", "1"
.setRequestHeader "Cookie", "PHPSESSID=cpjk366ochpkvi1gq3l66lp2u4"
.setRequestHeader "Connection", "keep-alive"
.setRequestHeader "Content-Encoding", "deflate"
.send
End With
dteStart = Now
With objHttp
.Open "post", "http://192.168.1.9/smsb/main.php?act=login_act", False
.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0"
.setRequestHeader "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
.setRequestHeader "Accept-Language", "en-US,en;q=0.5"
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.setRequestHeader "Origin", "http://192.168.1.9"
.setRequestHeader "Connection", "keep-alive"
.setRequestHeader "Referer", "http://192.168.1.9/smsb/main.php?act="
.setRequestHeader "Upgrade-Insecure-Requests", "1"
.setRequestHeader "Cookie", "PHPSESSID=cpjk366ochpkvi1gq3l66lp2u4"
.send "username=dian&password=dian123&submit=Login"
strResponse = .responseText
If InStr(strResponse, "FAIL") > 0 Then
Debug.Print "LOGIN ERROR"
End
End If
End With
'strData = Sheets("Config").Cells(2, 2)
strData = getDatafromFile
With objHttp
'.setTimeouts 30000, 60000, 30000, 120000
.Open "POST", "http://192.168.1.9/smsb/main.php?act=bc_header_act", False
.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0"
.setRequestHeader "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
.setRequestHeader "Accept-Language", "en-US,en;q=0.5'"
.setRequestHeader "Accept-Charset", "ISO-8859-1,utf-8"
.setRequestHeader "Content-Type", "multipart/form-data; boundary=kerasakti; charset=utf-8"
.setRequestHeader "Origin", "http://192.168.1.9"
.setRequestHeader "Connection", "keep-alive"
.setRequestHeader "Referer", "http://192.168.1.9/smsb/main.php?act=bc_header_add"
.setRequestHeader "Cookie", "PHPSESSID=cpjk366ochpkvi1gq3l66lp2u4"
.setRequestHeader "Upgrade-Insecure-Requests", "1"
.send (strData)
End With
Open "c:\users\82013623\output.html" For Output As #1
Print #1, objHttp.responseText
Close #1
strResponse = objHttp.responseText
Debug.Print strResponse
With objHttp
.Open "get", "http://192.168.1.9/smsb/main.php?act=logout_act", False
.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0"
.setRequestHeader "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
.setRequestHeader "Accept-Language", "en-US,en;q=0.5"
.setRequestHeader "Connection", "keep-alive"
.setRequestHeader "Referer", "http://192.168.1.9/smsb/index.php?act=login_act"
.setRequestHeader "Cookie", "PHPSESSID=cpjk366ochpkvi1gq3l66lp2u4"
.setRequestHeader "Upgrade-Insecure-Requests", "1"
.setRequestHeader "Connection", "keep-alive"
.setRequestHeader "Content-Encoding", "deflate"
.send
End With
ExitFunction:
If Err.Number <> 0 Then
Debug.Print Err.Description
strResponse = Err.Description
End If
'Debug.Print strResponse
End Sub
это Файл post.txt:
--kerasakti
Content-Disposition: form-data; name="bc_formno"
--kerasakti
Content-Disposition: form-data; name="bc_subject"
TESTING JANGAN DI SUBMIT
--kerasakti
Content-Disposition: form-data; name="bc_networktype"
1
--kerasakti
Content-Disposition: form-data; name="bc_bizflow_cond"
33
--kerasakti
Content-Disposition: form-data; name="bc_region"
--kerasakti
Content-Disposition: form-data; name="bc_regarea"
--kerasakti
Content-Disposition: form-data; name="bc_dest_area"
--kerasakti
Content-Disposition: form-data; name="kategori"
13
--kerasakti
Content-Disposition: form-data; name="sdata"
2
--kerasakti
Content-Disposition: form-data; name="bc_reqname"
dika
--kerasakti
Content-Disposition: form-data; name="bc_reqname"
dika sulastri
--kerasakti
Content-Disposition: form-data; name="bc_reqorg1"
--kerasakti
Content-Disposition: form-data; name="bc_reqphone"
628168299999
--kerasakti
Content-Disposition: form-data; name="bc_reqorg2"
--kerasakti
Content-Disposition: form-data; name="bc_reqemail"
dianwahyuni@GNOMEooredoo.com
--kerasakti
Content-Disposition: form-data; name="bc_reqorg3"
--kerasakti
Content-Disposition: form-data; name="bc_masking"
GNOME
--kerasakti
Content-Disposition: form-data; name="bctype"
2
--kerasakti
Content-Disposition: form-data; name="bc_msg"
Content of SMS will be sent to to cust
--kerasakti
Content-Disposition: form-data; name="msg_count"
151
--kerasakti
Content-Disposition: form-data; name="bc_controlnumbers"; filename="control.txt"
Content-Type: text/plain
628168299999
--kerasakti
Content-Disposition: form-data; name="emailnotif"
al.furqon@GNOMEooredoo.com
--kerasakti
Content-Disposition: form-data; name="chkcontrolnumbers"
1
--kerasakti
Content-Disposition: form-data; name="submit"
SAVE
--kerasakti--
И это успешный запрос скручивания:
curl -vL "http://10.34.20.5/smsb/main.php?act=login_act" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" -H "Accept-Language: en-US,en;q=0.5" --compressed -H "Content-Type: application/x-www-form-urlencoded" -H "Origin: http://10.34.20.5" -H "Connection: keep-alive" -H "Referer: http://10.34.20.5/smsb/main.php?act=" -H "Cookie: PHPSESSID=cpjk366ochpkvi1gq3l66lp2u4" -H "Upgrade-Insecure-Requests: 1" --data "username=dian&password=dian123&submit=Login"
curl -vL 'http://10.34.20.5/smsb/main.php?act=bc_header_act' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Content-Type: multipart/form-data; boundary=kerasakti' -H 'Origin: http://10.34.20.5' -H 'Connection: keep-alive' -H 'Referer: http://10.34.20.5/smsb/main.php?act=bc_header_add' -H 'Cookie: PHPSESSID=cpjk366ochpkvi1gq3l66lp2u4' -H 'Upgrade-Insecure-Requests: 1' --data-binary $'--kerasakti\r\nContent-Disposition: form-data; name="bc_formno"\r\n\r\n\r\n--kerasakti\r\nContent-Disposition: form-data; name="bc_subject"\r\n\r\nTESTING JANGAN DI SUBMIT\r\n--kerasakti\r\nContent-Disposition: form-data; name="bc_networktype"\r\n\r\n1\r\n--kerasakti\r\nContent-Disposition: form-data; name="bc_bizflow_cond"\r\n\r\n33\r\n--kerasakti\r\nContent-Disposition: form-data; name="bc_region"\r\n\r\n\r\n--kerasakti\r\nContent-Disposition: form-data; name="bc_regarea"\r\n\r\n\r\n--kerasakti\r\nContent-Disposition: form-data; name="bc_dest_area"\r\n\r\n\r\n--kerasakti\r\nContent-Disposition: form-data; name="kategori"\r\n\r\n13\r\n--kerasakti\r\nContent-Disposition: form-data; name="sdata"\r\n\r\n2\r\n--kerasakti\r\nContent-Disposition: form-data; name="bc_reqname"\r\n\r\ndian\r\n--kerasakti\r\nContent-Disposition: form-data; name="bc_reqname"\r\n\r\nDian Wahyuni\r\n--kerasakti\r\nContent-Disposition: form-data; name="bc_reqorg1"\r\n\r\n\r\n--kerasakti\r\nContent-Disposition: form-data; name="bc_reqphone"\r\n\r\n628168299999\r\n--kerasakti\r\nContent-Disposition: form-data; name="bc_reqorg2"\r\n\r\n\r\n--kerasakti\r\nContent-Disposition: form-data; name="bc_reqemail"\r\n\r\ndianwahyuni@GNOMEooredoo.com\r\n--kerasakti\r\nContent-Disposition: form-data; name="bc_reqorg3"\r\n\r\n\r\n--kerasakti\r\nContent-Disposition: form-data; name="bc_masking"\r\n\r\nGNOME\r\n--kerasakti\r\nContent-Disposition: form-data; name="bctype"\r\n\r\n2\r\n--kerasakti\r\nContent-Disposition: form-data; name="bc_msg"\r\n\r\nContent of SMS will be sent to to cust\r\n--kerasakti\r\nContent-Disposition: form-data; name="msg_count"\r\n\r\n151\r\n--kerasakti\r\nContent-Disposition: form-data; name="bc_controlnumbers"; filename="control.txt"\r\nContent-Type: text/plain\r\n\r\n628168299999\r\n--kerasakti\r\nContent-Disposition: form-data; name="emailnotif"\r\n\r\nal.furqon@GNOMEooredoo.com\r\n--kerasakti\r\nContent-Disposition: form-data; name="chkcontrolnumbers"\r\n\r\n1\r\n--kerasakti\r\nContent-Disposition: form-data; name="submit"\r\n\r\nSAVE\r\n--kerasakti--\r\n'
curl -vL 'http://10.34.20.5/smsb/main.php?act=logout_act' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Connection: keep-alive' -H 'Referer: http://10.34.20.5/smsb/index.php?act=login_act' -H 'Cookie: PHPSESSID=cpjk366ochpkvi1gq3l66lp2u4' -H 'Upgrade-Insecure-Requests: 1'
Также это winhttp dll vba см.
Directory of c:\Windows\System32
03/19/2019 11:45 AM 99,328 winhttpcom.dll
1 File(s) 99,328 bytes
Пожалуйста, помогите мне на этом, спасибо.