Post/Redirect/Get pattern

Get help with using AutoHotkey and its commands and hotkeys
malcev
Posts: 709
Joined: 12 Aug 2014, 12:37

Post/Redirect/Get pattern

15 Jun 2016, 14:32

I want to send post request.
But I will got "405 Not Allowed"

Code: Select all

url := "https://www.ss.lv/en/transport/cars/porsche/"
filter := "topt%5B8%5D%5Bmin%5D=800&topt%5B8%5D%5Bmax%5D=&topt%5B18%5D%5Bmin%5D=&topt%5B18%5D%5Bmax%5D=&topt%5B15%5D%5Bmin%5D=&topt%5B15%5D%5Bmax%5D=&opt%5B34%5D=&opt%5B35%5D=&opt%5B17%5D="
WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WebRequest.Open("POST", url, false)
WebRequest.Send(filter)
msgbox % WebRequest.ResponseText
In IE I see this Response Headers:
Key Value
Request POST /en/transport/cars/porsche/filter/ HTTP/1.1
Accept text/html, application/xhtml+xml, */*
Referer https://www.ss.lv/en/transport/cars/porsche/filter/
Accept-Language en-US
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding gzip, deflate
Host http://www.ss.lv
Connection Keep-Alive
Cache-Control no-cache
Cookie sid=2b0ccd23195e49d0f1f5c79a83ea681d08951470006de489319cddedcc2d99d5; LG=en; _ga=GA1.1.1238915062.1466008945; _gat=1; PHPSESSID=ac9b898dd3af79fe5214b7c4303d592c
Request GET /en/transport/cars/porsche/filter/ HTTP/1.1
Accept text/html, application/xhtml+xml, */*
Referer https://www.ss.lv/en/transport/cars/porsche/filter/
Accept-Language en-US
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding gzip, deflate
Host http://www.ss.lv
Connection Keep-Alive
Cache-Control no-cache
Cookie sid=2b0ccd23195e49d0f1f5c79a83ea681d08951470006de489319cddedcc2d99d5; LG=en; _ga=GA1.1.1238915062.1466008945; _gat=1; PHPSESSID=ac9b898dd3af79fe5214b7c4303d592c
As I understand it redirects to get request after post request.
https://en.wikipedia.org/wiki/Post/Redirect/Get
What can I do to receive ResponseText?
User avatar
rommmcek
Posts: 1097
Joined: 15 Aug 2014, 15:18

Re: Post/Redirect/Get pattern

17 Jun 2016, 16:51

Try this:

Code: Select all

; ... your code till now
WebRequest.Send(filter)
WebREquest.WaitForResponse()
Http_Text := WebRequest.ResponseText
msgbox % WebRequest.ResponseText
Sorry I was too hasty! I get 405 Not Allowed as well!
But if you change the site e.g. https://autohotkey.com/boards/viewtopic.php?f=5&t=19038 it'll work correctly! So I guess there is need to send Header. I don't know how to do it but I already saw scripts which at first worked good, quickly it was allover. E.g. This script worked fine but not for a long time it's written in AutoHotkey_H v2
Attachments
Translator_H.ahk
(34.72 KiB) Downloaded 55 times
Last edited by rommmcek on 17 Jun 2016, 17:10, edited 1 time in total.
malcev
Posts: 709
Joined: 12 Aug 2014, 12:37

Re: Post/Redirect/Get pattern

17 Jun 2016, 17:04

No it does not work.
I think that I have to made get request after the post request with some validation parameters to get correct answer.
But how to do it?
I also tried to save and send cookies.
But it does not work.
I mean this:
Go to link:
https://www.ss.lv/en/transport/cars/porsche/filter/
Insert 90000 in price edit box and push search.
We will get new data.
I want to get it with winhttprequest.
But can not.

Code: Select all

url := "https://www.ss.lv/en/transport/cars/porsche/filter/"
filter := "topt%5B8%5D%5Bmin%5D=90000&topt%5B8%5D%5Bmax%5D=&topt%5B18%5D%5Bmin%5D=&topt%5B18%5D%5Bmax%5D=&topt%5B15%5D%5Bmin%5D=&topt%5B15%5D%5Bmax%5D=&opt%5B34%5D=&opt%5B35%5D=&opt%5B32%5D=&opt%5B17%5D="
WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WebRequest.Open("POST", url, false)
WebRequest.Send(filter)
WebRequest.Open("GET", url, false)
WebRequest.Send
msgbox % WebRequest.ResponseText
User avatar
rommmcek
Posts: 1097
Joined: 15 Aug 2014, 15:18

Re: Post/Redirect/Get pattern

17 Jun 2016, 17:20

Maybe this would be helpful to you: https://autohotkey.com/board/topic/1130 ... quest-com/ and https://autohotkey.com/board/topic/3062 ... pdate-036/
I'm out of time now but anyway this is probably still too hard for me.

Good luck!
malcev
Posts: 709
Joined: 12 Aug 2014, 12:37

Re: Post/Redirect/Get pattern

17 Jun 2016, 17:43

E.g. This script worked fine but not for a long time it's written in AutoHotkey_H v2
The javascript function to get correct keys You can find here:
https://github.com/Stichoza/google-tran ... -218675898
And run it in autohotkey with this:
https://autohotkey.com/boards/viewtopic.php?f=6&t=4555
But I do not know how to solve my problem and google does not help me. :(
User avatar
Capn Odin
Posts: 1308
Joined: 23 Feb 2016, 19:45
Location: Denmark

Re: Post/Redirect/Get pattern

17 Jun 2016, 20:13

Try this.

Code: Select all

import requests

def SSLVsearch(car = "", Region = "", Sort = "", minPrice = "", maxPrice = "", minYear = "", maxYear = "", minVol = "", maxVol = "", Engine = "", GB = "", BodyType = "", Color = ""):
	s = requests.Session()
	
	Engines = {"Diesel" : 494, "Gasoline" : 493, "Gasoline/gas" : 495, "Hybrid" : 7434}
	GBs = {"Automatic" : 497, "Manual" : 496}
	BodyTypes = {"Cabriolet" : 488, "Coupe" : 487, "Hatchback" : 486, "Jeep" : 477, "Minivan" : 476, "Sedan" : 484, "Universal" : 483, "-" : 24775}
	Colors = {"Black" : 153, "Blue" : 6314, "Brown" : 6319, "Dark red" : 6309, "Green" : 6312, "Grey" : 6317, "Light blue" : 6313, "Orange" : 6310, "Purple" : 6315, "Red" : 6308, "Silver" : 6316, "White" : 6318, "Yellow" : 6311, "-" : 137}
	
	Regions = {"Riga" : "riga_f/", "Yurmala" : "yurmala_f/", "Riga region" : "riga_region_f/", "Aizkraukle and reg." : "aizkraukle_f/", "Aluksne and reg." : "aluksne_f/", "Balvi and reg." : "balvi_f/", "Bauska and reg." : "bauska_f/", "Cesis and reg." : "cesis_f/", "Daugavpils and reg." : "daugavpils_f/", "Dobele and reg." : "dobele_f/", "Gulbene and reg." : "gulbene_f/", "Jekabpils and reg." : "jekabpils_f/", "Jelgava and reg." : "jelgava_f/", "Kraslava and reg." : "kraslava_f/", "Kuldiga and reg." : "kuldiga_f/", "Liepaja and reg." : "liepaja_f/", "Limbadzi and reg." : "limbadzi_f/", "Ludza and reg." : "ludza_f/", "Madona and reg." : "madona_f/", "Ogre and reg." : "ogre_f/", "Preili and reg." : "preili_f/", "Rezekne and reg." : "rezekne_f/", "Saldus and reg." : "saldus_f/", "Talsi and reg." : "talsi_f/", "Tukums and reg." : "tukums_f/", "Valka and reg." : "valka_f/", "Valmiera and reg." : "valmiera_f/", "Ventspils and reg." : "ventspils_f/", "Estonia" : "estonia_f/", "Lithuania" : "lithuania_f/"}
	
	Sorts = {"Model" : "fDgSeF4VFjwT.html", "Year" : "fDgSeF4SHTwT.html", "Engine volume l. and type" : "fDgSeF4SEDwT.html", "Run, km" : "fDgSeF4SEzwT.html", "Price" : "fDgSeF4belM=.html"}
	
	filter = {	"topt[8][min]"  : minPrice, 
				"topt[8][max]"  : maxPrice,
				"topt[18][min]" : minYear,
				"topt[18][max]" : maxYear,
				"topt[15][min]" : minVol,
				"topt[15][max]" : maxVol,
				"opt[34]"		: Engines.get(Engine, ""),
				"opt[35]"		: GBs.get(GB, ""),
				"opt[32]"		: BodyTypes.get(BodyType, ""),
				"opt[17]"		: Colors.get(Color, "")}


	search = "https://www.ss.lv/en/transport/cars/" + car + "/filter/"
	
	response = s.post(search, data=filter)
	
	search = "https://www.ss.lv/en/transport/cars/" + car + "/filter/" + Regions.get(Region, "") + Sorts.get(Sort, "")
	
	response = s.get(search)

	f = open("temp.htm", "wb")
	f.write(response.content)
	f.close()
	
	return response.content

SSLVsearch(car = "porsche", Region = "Riga", Sort = "Price", minPrice = "9000", maxPrice = "50000", minYear = "2010", maxYear = "2015", minVol = "2.5", maxVol = "3.8", Engine = "Diesel", GB = "Manual", BodyType = "Hatchback", Color = "Red")

input("Press Enter to Continue")

SSLVsearch(car = "porsche", Color = "Red")
P.S. Don't tell anyone that I solved your problem in python.
Please excuse my spelling I am dyslexic.
malcev
Posts: 709
Joined: 12 Aug 2014, 12:37

Re: Post/Redirect/Get pattern

17 Jun 2016, 20:55

Thank You. It works. But if it is possible to get data in Python then it is possible to do the same in autohotkey?
User avatar
Capn Odin
Posts: 1308
Joined: 23 Feb 2016, 19:45
Location: Denmark

Re: Post/Redirect/Get pattern

18 Jun 2016, 03:30

malcev wrote:Thank You. It works. But if it is possible to get data in Python then it is possible to do the same in autohotkey?
Ofcourse it is I was just more familiar with the requests library, sorry about that.
Please excuse my spelling I am dyslexic.
malcev
Posts: 709
Joined: 12 Aug 2014, 12:37

Re: Post/Redirect/Get pattern

18 Jun 2016, 07:50

Interesting to understand how this library works.
malcev
Posts: 709
Joined: 12 Aug 2014, 12:37

Re: Post/Redirect/Get pattern

19 Jun 2016, 19:01

Just understand my mistake.
Forgot to WebRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") when send post request:

Code: Select all

url := "https://www.ss.lv/ru/transport/cars/porsche/filter/"
filter := "topt%5B8%5D%5Bmin%5D=90000&topt%5B8%5D%5Bmax%5D=&topt%5B18%5D%5Bmin%5D=&topt%5B18%5D%5Bmax%5D=&topt%5B15%5D%5Bmin%5D=&topt%5B15%5D%5Bmax%5D=&opt%5B34%5D=&opt%5B35%5D=&opt%5B32%5D=&opt%5B17%5D="
WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WebRequest.Open("POST", url, false)
WebRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
WebRequest.Send(filter)
msgbox % WebRequest.ResponseText

Return to “Ask For Help”

Who is online

Users browsing this forum: Frosti, Google [Bot], jvalk87, seba1685, TAC109 and 32 guests