首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >机械制造LoadControl XHR请求

机械制造LoadControl XHR请求
EN

Stack Overflow用户
提问于 2019-11-17 02:38:23
回答 1查看 188关注 0票数 1

我有一个具有机械导航外部网站的Python脚本。该网站显然是用ASP.NET编写的,使用的是动态控件。我试图模拟XHR来加载控件(通常通过单击<button>来触发),但是每当我执行XHR时,ASP服务器都会返回一个500个错误。我匹配通常发送的POST请求的有效负载。

基本上,我在做这个:

代码语言:javascript
复制
browser = mechanize.Browser()

browser.open('https://external.site/page.html')
# Here would be code to parse the content and extract the
# parameters for the XHR.

# Now we're making the XHR to update the control:
body = urllib.parse.urlencode({
    'request': {
                'control': 'Control_Name',
                'parameters': parameters,
            }
        })

request = mechanize.Request(
    url='https://external.site/server.asmx/LoadControl',
    data=body,
    method='POST',
)

response = browser.open(request)

当我这样做时,浏览器会引发一个异常,因为它从服务器收到了一个500。

这可能是不可能的,因为一个CORS检查或类似的东西,但我觉得我只是没有提出正确的请求。当我在浏览器中使用外部网站时,URL和数据有效负载匹配通常发送的内容。

有没有办法通过机械化提出LoadControl请求?

作为参考,我试图浏览的网站上的按钮如下所示:

代码语言:javascript
复制
<button type="button"
     data-focus="{&quot;LoadParams&quot;:{&quot;ControlName&quot;: [lots of key/value pairs here here]"}}"
     data-action="GB.LoadControl">Button</button>

当我单击该按钮时,XHR将包含以下有效负载:

代码语言:javascript
复制
{
    "request": {
        [the data here matches data-focus from the button]
    }
}

以及请求头:

代码语言:javascript
复制
POST /MyService.asmx/LoadControl HTTP/1.1
Host: myservice.com
Connection: keep-alive
Content-Length: 367
Accept: application/json, text/javascript, */*; q=0.01
X-Requested-With: XMLHttpRequest 
User-Agent: Mozilla/5.0 (X11; CrOS x86_64 12871.23.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.31 Safari/537.36 
Content-Type: application/json; charset=UTF-8 
Origin: https://myservice.com 
Sec-Fetch-Site: same-origin 
Sec-Fetch-Mode: cors 
Sec-Fetch-Dest: empty 
Referer: https://myservice.comService.aspx?AGU=1&[...] 
Accept-Encoding: gzip, deflate, br 
Accept-Language: en-US,en;q=0.9,de-DE;q=0.8,de;q=0.7 
Cookie: BIGipServerSD_Home=224813834.20480.0000; ASP.NET_SessionId=152i5tiwq1r2cbiuamxxxxxx; PVUE=00
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-29 03:28:42

发送给服务器的机械化请求中的标题application/x-www-form-urlencoded

在传递给Content-typeheaders参数中将请求设置为请求

代码语言:javascript
复制
headers = {'Content-type': 'application/json'}
request = mechanize.Request(
    url='https://external.site/server.asmx/LoadControl',
    data=body,
    method='POST',
    headers=headers
)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58897051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档