Что нового

Получить URL всплывающего окна

Latoid

Знающий
Сообщения
95
Репутация
11
Здравствуйте

Вот такой нехитрый код заполняет web-форму и жмет кнопку.

Код:
#include <IE.au3>

$sURL = 'http://www.emspost.ru/ru/printInternational/'

$oIE = _IECreate ($sURL)
Local $oForm = _IEFormGetObjByName($oIE, "form1")
_IEFormElementSetValue(_IEFormElementGetObjByName($oForm, "SenderName"), "Sender name")
_IEFormElementSetValue(_IEFormElementGetObjByName($oForm, "SenderAddress"), "Sender Address")
_IEFormElementSetValue(_IEFormElementGetObjByName($oForm, "SenderPhone"), "Sender Phone")
_IEFormElementSetValue(_IEFormElementGetObjByName($oForm, "RecipientName"), "Recipient Name")
_IEFormElementSetValue(_IEFormElementGetObjByName($oForm, "RecipientAddress"), "Recipient Address")
_IEFormElementSetValue(_IEFormElementGetObjByName($oForm, "RecipientPhone"), "Recipient Phone")
_IEFormElementSetValue(_IEFormElementGetObjByName($oForm, "MailingDescription"), "Mailing Description")
_IEFormElementRadioSelect ($oForm, "Gift", "MailingType")

;~ ConsoleWrite (_IEPropertyGet($oIE, 'locationurl') & @CRLF)
_IEAction(_IEGetObjByName($oIE, "btnPrint"), "click")
_IELoadWait($oIE)


Заполненный бланк в формате PDF открывается в POP UP окне. Требуется получить URL (вида http://www.emspost.ru/ru/getInternationalLabel.ashx?uniqueName=F_[буквы, цифры, тире]) всплывающего окна для скачивания PDF и последующих манипуляций с ним.

Возможно, есть какой-то иной подход? Цель - получить URL PDF документа.

Спасибо
 

firex

AutoIT Гуру
Сообщения
943
Репутация
208
Latoid
Как альтернатива: запрашивайте у сервера документ без использования IE (его интерфейсы используются конечно, но чуть пониже).


Код:
Global $__g_oAutErrEvent = ObjEvent('AutoIt.Error', '__ComErrCallback')
; ---
Local $sUrl = _emspost_GetPdf( _
	'Sender name', _
	'Sender Address', _
	'Sender Phone', _
	'Recipient Name', _
	'Recipient Address', _
	'Recipient Phone', _
	'Mailing Description', _
	'Gift', _
	0 _
	)
If Not @Error Then
	MsgBox(64, '', $sUrl)
Else
	MsgBox(16, '', 'Error code=' & @Error)
	; 2 - исчерпан лимит запросов / сервер не вернул имя файла
	; 3 - ответ некорректный
EndIf

Func _emspost_GetPdf( _
	$_sSenderName, _
	$_sSenderAddress, _
	$_sSenderPhone, _
	$_sRecipientName, _
	$_sRecipientAddress, _
	$_sRecipientPhone, _
	$_sMailingDescription, _
	$_sMailingType, _ ; Gift, ...
	$_iMailingAgreementNo _
) ;
	Local $oHTTP, $sJsonPayload, $vResp, $sUrl
	; ---
	$oHTTP = ObjCreate('WinHttp.WinHttpRequest.5.1')
	If Not IsObj($oHTTP) Then _
		Return SetError(1, 0, '')
	; *
	$sJsonPayload = _
		'{"s":"{\"SenderName\":\"' & $_sSenderName & _
		'\",\"SenderAddress\":\"' & $_sSenderAddress & _
		'\",\"SenderPhone\":\"' & $_sSenderPhone & _
		'\",\"RecipientName\":\"' & $_sRecipientName & _
		'\",\"RecipientAddress\":\"' & $_sRecipientAddress & _
		'\",\"RecipientPhone\":\"' & $_sRecipientPhone & _
		'\",\"MailingDescription\":\"' & $_sMailingDescription & _
		'\",\"MailingType\":\"' & $_sMailingType & _
		'\",\"MailingAgreementNo\":\"' & $_iMailingAgreementNo & _
		'\"}"}'
	; *
	With $oHTTP
		.Open('POST', 'http://www.emspost.ru/ru/printinternational.aspx/printLabel', False)
		.SetRequestHeader('Accept-Encoding', 'deflate')
		.SetRequestHeader('Content-Type', 'application/json; charset=UTF-8')
		.SetRequestHeader('Referer', 'http://www.emspost.ru/ru/printInternational/')
		.SetRequestHeader('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36')
		.Send($sJsonPayload)
	EndWith
	; *
	$vResp = $oHTTP.ResponseText
	If $vResp Then
		$vResp = __jsonLoad($vResp)
		If Not @Error Then
			$vResp = __jsonLoad($vResp.d)
			If Not @Error Then
				$sUrl = $vResp.fileName

				If $sUrl Then _
					Return ('http://www.emspost.ru/ru/getInternationalLabel.ashx?uniqueName=' & $sUrl)

				Return SetError(2, 0, '')
			EndIf
		EndIf
	EndIf
	; ---
	Return SetError(3, 0, '')
EndFunc


Func __ComErrCallback()
	; Nothing
EndFunc

Func __jsonLoad($sJson)
	Local $oRes, $oSC = ObjCreate('ScriptControl')
    If Not IsObj($oSC) Then _
		Return SetError(1, 0, 0)
	; *
	$oSC.Language = 'JavaScript'
	; ---
    $sJson = StringRegExpReplace($sJson, '[\r\n]+', ' ')
    $sJson = StringReplace($sJson, "'", "\'")
    $oRes = $oSC.Eval('(' & $sJson & ')')
    If Not IsObj($oRes) Then  _
		Return SetError(2, 0, 0)
	; ---
    Return $oRes
EndFunc
 
Автор
L

Latoid

Знающий
Сообщения
95
Репутация
11
firex Превосходно! Огромное спасибо! :IL_AutoIt_1:
 
Верх