简体   繁体   English

为发布请求模拟xhr

[英]simulating xhr for a post request

I'm trying to send a post request via python, but it goes through badly. 我正在尝试通过python发送发帖请求,但是操作很糟糕。

I want my code to approve my selected seats and continue to payment. 我希望我的代码批准我选择的席位并继续付款。

I took this url,data and token from the post request after putting the selected cinema place time and seats. 在放置选定的电影院时间和座位后,我从发布请求中获取了该网址,数据和令牌。

import urllib.parse, urllib.request 

url = "https://tickets.yesplanet.co.il/YPR/SelectSeatPageRes.aspx/SetSelectedSeats?ec=10725013018-246564"


data = urllib.parse.urlencode(dict(
seats = "11,19#11,20#11,21#11,22",
token ="246564#5#1"
))


res = urllib.request.urlopen(url, data.encode("utf8"))


print (res.read())

the link has an expiration but this is the result: 链接已过期,但这是结果:

Session Ended It appears that the session has ended before you were able to complete your purchase. 会话结束会话似乎已经结束,您才能够完成购买。

a link to the main site : https://www.yesplanet.co.il 指向主站点的链接: https : //www.yesplanet.co.il

how do i know if my request is complete? 我怎么知道我的请求是否完成?

for your convince info from the headers and response tabs from the development tool: 为您从开发工具的标题和响应选项卡中提供令人信服的信息:

response headers: 响应头:

Cache-Control:private, max-age=0
Content-Length:170
Content-Type:application/json; charset=utf-8
Date:Tue, 30 Jan 2018 01:27:26 GMT
P3P:CP="NOI ADM DEV COM NAV OUR STP"
Server:Microsoft-IIS/8.5
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET

request headers: 请求标头:

**Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate, br
Accept-Language:he-IL,he;q=0.9,en-US;q=0.8,en;q=0.7
Connection:keep-alive
Content-Length:44
Content-Type:application/json; charset=UTF-8
Cookie:ASP.NET_SessionId=p4citijvw3vrqxuoekqnlrhw; _ga=GA1.3.525452416.1517275557; _gid=GA1.3.1168599094.1517275557; _gat_tealium_0=1; utag_main=v_id:016144aba503001d7d72fa299b0904072001c06a00868$_sn:1$_ss:0$_st:1517277365866$ses_id:1517275555076%3Bexp-session$_pn:2%3Bexp-session; hfOIKey=CXCFcTD1; SS#246564#5#1=; SS%23246564%235%231=17%2C12%2317%2C13; hfSKey=%7C%7C%7C%7C%7C%7C%7C%7C%7C1072_res%7C10725013018-246564%7C20
Host:tickets.yesplanet.co.il
Origin:https://tickets.yesplanet.co.il
Referer:https://tickets.yesplanet.co.il/YPR/SelectSeatPageRes.aspx?dtticks=636528796178961691&cf=1004&ec=10725013018-246564
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36
X-Requested-With:XMLHttpRequest**

request payload 请求有效载荷

{seats: "16,10#16,11", token: "246564#5#1"}
seats
:
"16,10#16,11"
token
:
"246564#5#1"

and the response tab: 和响应选项卡:

{"d":"{\"ReturnCode\":0,\"Message\":null,\"Redirect\":\"/YPR/OrderFormPageRes.aspx?dtticks=636528796470870119\\u0026cf=1005\\u0026ec=10725013018-246564\",\"Data\":null}"}

The cookie header is the key. Cookie标头是关键。 When you send a request from xhr (aka your browser), relevant cookies are automatically appended to your request. 当您从xhr(又名浏览器)发送请求时,相关的cookie会自动附加到您的请求中。

These cookies are how sessions are usually managed, and the response message indicates that the server did not find a valid session cookie in your request. 这些cookie是通常管理会话的方式,并且响应消息指示服务器未在您的请求中找到有效的会话cookie。

You will need to "authorize", via logging in or otherwise beginning this session, and then insert that session cookie into your request before sending it. 您将需要通过登录或以其他方式开始此会话来“授权”,然后在发送请求之前将会话cookie插入请求中。

After rereading, the token header is most likely not static either. 重新读取后,令牌头很可能也不是静态的。 My guess would be that this is engineered to prevent automated requests, and so may be difficult to circumvent. 我的猜测是,这是为了防止自动请求而设计的,因此可能很难规避。

Update in response to OP comment: 更新以回应OP评论:

Use cookiejar or just read the urllib docs and figure out how to extract and then insert cookies. 使用cookiejar或只是阅读urllib文档,并弄清楚如何提取并插入cookie。

how to send cookies inside post request 如何在请求后发送Cookie

You will need to study the website's behavior in your developer tools and see which request triggers a session cookie update, and then simulate that request before you simulate your post request. 您将需要在开发人员工具中研究网站的行为,并查看哪个请求触发了会话cookie更新,然后在模拟发布请求之前模拟该请求。

You've been provided three answers. 已为您提供了三个答案。 Mark the question as correct and post another, more specific if you still have trouble. 如果您仍然遇到问题,请将问题标记为正确,然后发布另一个更具体的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM