我正在寻找一种向每个 http 请求添加 2 个自定义 cookie 的方法。
browsermob 代理 ( https://github.com/lightbody/browsermob-proxy ) 具有 removeHeaders() 和 addHeader() 方法,但我能做些什么来保留现有的 cookie 请求,但再添加 2 个 cookie?
谢谢!
我正在寻找一种向每个 http 请求添加 2 个自定义 cookie 的方法。
browsermob 代理 ( https://github.com/lightbody/browsermob-proxy ) 具有 removeHeaders() 和 addHeader() 方法,但我能做些什么来保留现有的 cookie 请求,但再添加 2 个 cookie?
谢谢!
您可以使用此方法在每个请求/响应中调用您的自定义 js 代码 https://github.com/lightbody/browsermob-proxy#http-request-manipulation Python 中的一些示例
def response_interceptor(self, js):
"""
Executes the javascript against each response
:param js: the javascript to execute
"""
r = requests.post(url='%s/proxy/%s/interceptor/response' % (self.host, self.port),
data=js,
headers={'content-type': 'x-www-form-urlencoded'})
return r.status_code
def request_interceptor(self, js):
"""
Executes the javascript against each request
:param js: the javascript to execute
"""
r = requests.post(url='%s/proxy/%s/interceptor/request' % (self.host, self.port),
data=js,
headers={'content-type': 'x-www-form-urlencoded'})
return r.status_code
并测试:
def test_request_interceptor_with_parsing_js(self):
"""
/proxy/:port/interceptor/request
"""
js = 'alert("foo")'
status_code = self.client.request_interceptor(js)
assert(status_code == 200)
正如我在上面回答的那样,您可以使用代理的 REST API 为通过代理发出的每个请求设置自定义 js 处理程序。
例如,您可以为每个请求添加任何自定义 cookie:
curl -X POST -H 'Content-Type: text/plain' -d 'js 代码' http://10.100.100.20:8080/proxy/8081/interceptor/request
在 php 中,它看起来像:
/**
* @param Proxy $proxyObject
* @param array $cookiesArray
*/
protected function _setRequestCookies(Proxy $proxyObject, array $cookiesArray)
{
foreach ($cookiesArray as $nameString => $valueString) {
$cookiesArray[$nameString] = $nameString . '=' . $valueString;
}
$jsHandlerString = sprintf(
'var c = request.getMethod().getFirstHeader("Cookie") ? request.getMethod().getFirstHeader("Cookie").getValue() : ""; request.getMethod().setHeader("Cookie", c + "; %s");',
implode('; ', $cookiesArray)
);
$urlString = sprintf('%sproxy/%u/interceptor/request', $this->_hubUrlString, $proxyObject->getPort());
$this->_requesterObject->makeRequest($urlString, Requester::REQUEST_METHOD_POST, $jsHandlerString);
}