想利用js代码模拟取消关注,现在是取消关注一个人,不知道代码问题在哪里
注: 隐去了一些cookie和token
首先在Network上看到请求如下(浏览器上的操作)
Request URL:https://www.zhihu.com/node/MemberFollowBaseV2
Request Method:POST
Status Code:200 OK
Remote Address:112.29.152.100:443
Response Headers
view source
Cache-Control:no-store
Connection:keep-alive
Content-Encoding:gzip
Content-Length:42
Content-Security-Policy:default-src *; img-src * data:; frame-src 'self' *.zhihu.com getpocket.com note.youdao.com read.amazon.cn; script-src 'self' *.zhihu.com *.google-analytics.com zhstatic.zhihu.com res.wx.qq.com 'unsafe-eval'; style-src 'self' *.zhihu.com 'unsafe-inline'
Content-Type:application/json
Date:Thu, 06 Oct 2016 04:52:32 GMT
Pragma:no-cache
Server:Qnginx/1.2.0
Set-Cookie:_xsrf=; Domain=zhihu.com; expires=Wed, 07 Oct 2015 04:52:32 GMT; Path=/
Set-Cookie: 你的cookie
Set-Cookie:
Vary:Accept-Encoding
X-Frame-Options:DENY
X-NWS-LOG-UUID:afc436a9-5205-46fa-a0fa-dbd2cc4bb19c
X-Req-ID:A041C7357F5D890
X-Za-Experiment:default:None
X-Za-Response-Id:000400af22560788
Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Content-Length:90
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie: 你的cookie
Host:www.zhihu.com
Origin:https://www.zhihu.com
Pragma:no-cache
Referer:https://www.zhihu.com/people/he-zhiming/followees
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36
X-Requested-With:XMLHttpRequest
X-Xsrftoken: 你的token
Form Data
view source
view URL encoded
method:unfollow_member
params:{"hash_id":"9c47aea775c35ebd6b647f0041bb7b6c"}
可知,是一个简单的POST请求,然后我用js代码模拟这一过程(见后文)
但是,结果返回403,body如下
Request URL:https://www.zhihu.com/node/MemberFollowBaseV2
Request Method:POST
Status Code:403 Forbidden
Remote Address:112.29.152.100:443
Response Headers
view source
Cache-Control:no-store
Connection:keep-alive
Content-Encoding:gzip
Content-Length:70
Content-Security-Policy:default-src *; img-src * data:; frame-src 'self' *.zhihu.com getpocket.com note.youdao.com read.amazon.cn; script-src 'self' *.zhihu.com *.google-analytics.com zhstatic.zhihu.com res.wx.qq.com 'unsafe-eval'; style-src 'self' *.zhihu.com 'unsafe-inline'
Content-Type:text/html; charset=UTF-8
Date:Thu, 06 Oct 2016 05:17:56 GMT
Pragma:no-cache
Server:Qnginx/1.2.0
Set-Cookie:q_c1=280cac781fda44199c454de14296dd24|1475731076000|1475731076000; Domain=zhihu.com; expires=Sun, 06 Oct 2019 05:17:56 GMT; Path=/
Set-Cookie:_xsrf=; Domain=zhihu.com; expires=Wed, 07 Oct 2015 05:17:56 GMT; Path=/
Set-Cookie:你的cookie
Set-Cookie:n_c=1; Domain=zhihu.com; Path=/
Set-Cookie: 你的cookie
Vary:Accept-Encoding
X-Frame-Options:DENY
X-NWS-LOG-UUID:c8fde986-b6b3-42e5-81d5-96ccf1e4e54c
X-Req-ID:A11B2A857F5DE82
X-Za-Response-Id:0009e73ab30e66f5
Request Headers
view source
accept:*/*
Accept-Encoding:gzip, deflate, br
accept-language:en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Content-Length:90
content-type:application/x-www-form-urlencoded; charset=UTF-8
Host:www.zhihu.com
Origin:https://www.zhihu.com
post:/node/MemberFollowBaseV2 HTTP/1.1
Pragma:no-cache
Referer:https://www.zhihu.com/people/he-zhiming/followees
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36
x-requested-with:XMLHttpRequest
x-xsrftoken: 你的token
Form Data
view source
view URL encoded
method:unfollow_member
params:{"hash_id":"9f2f9b4aa78b9a1e76fcfd9225e2dc25"}
Chrome最新版,支持fetch-API
Console上粘贴代码
拷贝我的js代码,运行之
可能一些cookie,token之类的需要修改,具体查看你的chrome即可
var _hashid = '9f2f9b4aa78b9a1e76fcfd9225e2dc25' //这是copy的一个hash_id
var url = '/node/MemberFollowBaseV2'
//构造payload
var body = 'method=unfollow_member¶ms=' + encodeURIComponent(JSON.stringify({"hash_id":_hashid}));
var _headersObj = {
POST : '/node/MemberFollowBaseV2 HTTP/1.1',
Host: 'www.zhihu.com',
Connection: 'keep-alive',
'Content-Length': '90',
Pragma: 'no-cache',
'Cache-Control': 'no-cache',
Origin: 'https://www.zhihu.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
Accept: '*/*',
'X-Requested-With': 'XMLHttpRequest',
'X-Xsrftoken': '粘贴你的token',
Referer: '从chrome处粘贴',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4',
Cookie: 粘贴你的cookie
}
var headers = new Headers(_headersObj)
var request = new Request(
url,
{
method: 'POST',
headers: headers,
body: body
}
)
fetch(request).then(function (response) {
if (response.ok) {
var json = response.json()
if (json.r === 0) {
console.log('SUCCESS')
}
}else {
console.log('FAILED')
}
})
见上文
查阅了fetch-API的使用: https://davidwalsh.name/fetch
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
搞cookie没有必要,浏览器会自动加上。这种事情看源码就OK了。
刷新后我们可以看到取消成功:
取消成功。PS:要刷新一下,因为界面的重新渲染不是以请求发送为准,而是你点了那个按钮才会触发,所以你直接模拟请求是没用的,并不会使按钮重新渲染,虽然已经取消成功了。所以刷新下就行了。
代码附在下方了:
另外源代码在下: