Вот код 1:
Всё нормально, выводит "Ок"
Вот код 2:
Во втором случае не работает. Cookie в обоих случаях находит и записывает.
В чём может быть проблема?
Только что нашёл странную штуку.
Если совместить это в один код, то cookie подходят, к-е мы получили при помощи TCP.
т.е.:
П.с. Код вроде вырезал аккуратно. Должно работать после копирования и вставки(даже логин\пароль оставил)
Код:
Func GetCookie($m)
$m = StringRegExp($m, "sess_id=(\S+)", 3)
$Cookie = 'sess_id=' & $m[0]
MsgBox(0,'',''&$Cookie)
return $Cookie
EndFunc
$oHTTP = ObjCreate('WinHttp.WinHttpRequest.5.1')
$oHTTP.Open('POST', 'http://ts5.travian.com/dorf1.php')
$oHTTP.setTimeouts(5000, 5000, 15000, 15000)
$oHTTP.SetRequestHeader('Host', 'ts5.travian.com')
$oHTTP.SetRequestHeader('Connection', 'close')
$oHTTP.SetRequestHeader('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')
$oHTTP.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
$oHTTP.SetRequestHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
$oHTTP.SetRequestHeader('Accept-Language', 'ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3')
$oHTTP.SetRequestHeader('Accept-Charset', 'windows-1251,utf-8;q=0.7,*;q=0.7')
$sSendData = 'name=Nikam&password=132547698&w=1920%3A1080&login='
$oHTTP.Send($sSendData)
$oHTTP.WaitForResponse
$Response = $oHTTP.ResponseText
$headers = $oHTTP.GetAllResponseHeaders()
;MsgBox(0,'',''&$headers)
Global $Cookie = GetCookie($headers)
$oHTTP.Open('GET', 'http://ts5.travian.com/dorf1.php')
$oHTTP.setTimeouts(5000, 5000, 15000, 15000)
$oHTTP.SetRequestHeader('Host', 'ts5.travian.com')
$oHTTP.SetRequestHeader('Connection', 'Keep-alive')
$oHTTP.SetRequestHeader('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')
$oHTTP.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
$oHTTP.SetRequestHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
$oHTTP.SetRequestHeader('Referer', 'http://ts8.travian.com/dorf1.php')
$oHTTP.SetRequestHeader('Cookie', $Cookie)
$oHTTP.SetRequestHeader('Accept-Encoding', 'gzip, deflate')
$oHTTP.SetRequestHeader('Accept-Language', 'ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3')
$oHTTP.SetRequestHeader('Accept-Charset', 'windows-1251,utf-8;q=0.7,*;q=0.7')
$oHTTP.Send('')
$oHTTP.WaitForResponse
$Response = $oHTTP.ResponseText
If StringRegExp($Response,'stime') Then MsgBox(0,'','Ok')
Всё нормально, выводит "Ок"
Вот код 2:
Код:
Func PostHTML($host, $page, $SndPar, $Inf='')
Local $ip, $socket, $Headers, $RecBuff, $HTML, $Bytes
TCPStartup()
$ip = TCPNameToIP($host)
If $ip = '' Then
ConsoleWrite(@CRLF&'==============='&@CRLF&'Не удалось преобразовать адрес в ip'&@CRLF&'==============='&@CRLF)
TCPShutdown()
return -1
EndIf
While 1
$socket = TCPConnect($ip,80)
If $socket = -1 Then
ConsoleWrite(@CRLF&'==============='&@CRLF&'Не удалось подключиться! Переподключение...'&@CRLF&'==============='&@CRLF)
Sleep(5000)
Else
ExitLoop(1)
EndIf
WEnd
$Headers = 'POST ' &$page&' HTTP/1.1'&@CRLF
$Headers &= 'Host: '&$host&@CRLF
$Headers &= 'Content-Length: '&StringLen($SndPar)&@CRLF
If $Inf <> "" Then
$Headers &= $Inf&@CRLF
EndIf
;$Headers &= 'Origin: http://'&$host&@CRLF
$Headers &= 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'&@CRLF
$Headers &= 'Content-Type: application/x-www-form-urlencoded'&@CRLF
$Headers &= 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'&@CRLF
;$Headers &= 'Referer: http://'&$host&$page&@CRLF
$Headers &= 'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4'&@CRLF
$Headers &= 'Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.3'&@CRLF
$Headers &= 'Connection: close'&@CRLF&@CRLF
$Headers &=$SndPar
While 1
$Bytes = TCPSend($socket,$Headers)
If $Bytes = 0 Then
ConsoleWrite(@CRLF&'==============='&@CRLF&'Не удаётся отослать инФормацию! Новая попытка'&@CRLF&'==============='&@CRLF)
Sleep(5000)
Else
ExitLoop(1)
EndIf
WEnd
while 1
$RecBuff = TCPRecv($socket,1)
If @error <> 0 Then ExitLoop(1)
If $RecBuff <> '' Then $HTML &= $RecBuff
WEnd
Return $HTML
EndFunc
Func GetCookie($m)
$m = StringRegExp($m, "sess_id=(\S+)", 3)
$Cookie = 'sess_id=' & $m[0]
MsgBox(0,'',''&$Cookie)
return $Cookie
EndFunc
$login = PostHTML('ts5.travian.com', '/dorf1.php', 'name=Nikam&password=132547698&w=1920%3A1080&login=')
$Cookie = GetCookie($login)
$oHTTP.Open('GET', 'http://ts5.travian.com/dorf1.php')
$oHTTP.setTimeouts(5000, 5000, 15000, 15000)
$oHTTP.SetRequestHeader('Host', 'ts5.travian.com')
$oHTTP.SetRequestHeader('Connection', 'Keep-alive')
$oHTTP.SetRequestHeader('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')
$oHTTP.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
$oHTTP.SetRequestHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
$oHTTP.SetRequestHeader('Referer', 'http://ts8.travian.com/dorf1.php')
$oHTTP.SetRequestHeader('Cookie', $Cookie)
$oHTTP.SetRequestHeader('Accept-Encoding', 'gzip, deflate')
$oHTTP.SetRequestHeader('Accept-Language', 'ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3')
$oHTTP.SetRequestHeader('Accept-Charset', 'windows-1251,utf-8;q=0.7,*;q=0.7')
$oHTTP.Send('')
$oHTTP.WaitForResponse
$Response = $oHTTP.ResponseText
If StringRegExp($Response,'stime') Then MsgBox(0,'','Ok')
Во втором случае не работает. Cookie в обоих случаях находит и записывает.
В чём может быть проблема?
Только что нашёл странную штуку.
Если совместить это в один код, то cookie подходят, к-е мы получили при помощи TCP.
т.е.:
Код:
Func PostHTML($host, $page, $SndPar, $Inf='')
Local $ip, $socket, $Headers, $RecBuff, $HTML, $Bytes
TCPStartup()
$ip = TCPNameToIP($host)
If $ip = '' Then
ConsoleWrite(@CRLF&'==============='&@CRLF&'Не удалось преобразовать адрес в ip'&@CRLF&'==============='&@CRLF)
TCPShutdown()
return -1
EndIf
While 1
$socket = TCPConnect($ip,80)
If $socket = -1 Then
ConsoleWrite(@CRLF&'==============='&@CRLF&'Не удалось подключиться! Переподключение...'&@CRLF&'==============='&@CRLF)
Sleep(5000)
Else
ExitLoop(1)
EndIf
WEnd
$Headers = 'POST ' &$page&' HTTP/1.1'&@CRLF
$Headers &= 'Host: '&$host&@CRLF
$Headers &= 'Content-Length: '&StringLen($SndPar)&@CRLF
If $Inf <> "" Then
$Headers &= $Inf&@CRLF
EndIf
;$Headers &= 'Origin: http://'&$host&@CRLF
$Headers &= 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'&@CRLF
$Headers &= 'Content-Type: application/x-www-form-urlencoded'&@CRLF
$Headers &= 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'&@CRLF
;$Headers &= 'Referer: http://'&$host&$page&@CRLF
$Headers &= 'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4'&@CRLF
$Headers &= 'Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.3'&@CRLF
$Headers &= 'Connection: close'&@CRLF&@CRLF
$Headers &=$SndPar
While 1
$Bytes = TCPSend($socket,$Headers)
If $Bytes = 0 Then
ConsoleWrite(@CRLF&'==============='&@CRLF&'Не удаётся отослать инФормацию! Новая попытка'&@CRLF&'==============='&@CRLF)
Sleep(5000)
Else
ExitLoop(1)
EndIf
WEnd
while 1
$RecBuff = TCPRecv($socket,1)
If @error <> 0 Then ExitLoop(1)
If $RecBuff <> '' Then $HTML &= $RecBuff
WEnd
Return $HTML
EndFunc
$login = PostHTML('ts8.travian.com', '/dorf1.php', 'name=Nikam&password=132547698&w=1920%3A1080&login=')
$a = StringRegExp($login,'Set-Cookie: (\S+)',3)
$Cookie = 'sess_id='&$a[0]
$oHTTP = ObjCreate('WinHttp.WinHttpRequest.5.1')
$oHTTP.Open('POST', 'http://ts8.travian.com/dorf1.php')
$oHTTP.setTimeouts(5000, 5000, 15000, 15000)
$oHTTP.SetRequestHeader('Host', 'ts8.travian.com')
$oHTTP.SetRequestHeader('Connection', 'close')
$oHTTP.SetRequestHeader('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')
$oHTTP.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
$oHTTP.SetRequestHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
$oHTTP.SetRequestHeader('Accept-Language', 'ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3')
$oHTTP.SetRequestHeader('Accept-Charset', 'windows-1251,utf-8;q=0.7,*;q=0.7')
$sSendData = 'name=Nikam&password=132547698&w=1920%3A1080&login='
$oHTTP.Send($sSendData)
$oHTTP.Open('GET', 'http://ts8.travian.com/dorf1.php')
$oHTTP.setTimeouts(5000, 5000, 15000, 15000)
$oHTTP.SetRequestHeader('Host', 'ts8.travian.com')
$oHTTP.SetRequestHeader('Connection', 'Keep-alive')
$oHTTP.SetRequestHeader('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')
$oHTTP.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
$oHTTP.SetRequestHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
$oHTTP.SetRequestHeader('Referer', 'http://ts8.travian.com/dorf1.php')
$oHTTP.SetRequestHeader('Cookie', $Cookie)
$oHTTP.SetRequestHeader('Accept-Encoding', 'gzip, deflate')
$oHTTP.SetRequestHeader('Accept-Language', 'ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3')
$oHTTP.SetRequestHeader('Accept-Charset', 'windows-1251,utf-8;q=0.7,*;q=0.7')
$oHTTP.Send('')
$oHTTP.WaitForResponse
$Response = $oHTTP.ResponseText
If StringRegExp($Response,'stime') Then MsgBox(0,'','Ok')
П.с. Код вроде вырезал аккуратно. Должно работать после копирования и вставки(даже логин\пароль оставил)