Use the request method of node to simulate comments in the video course of Mujiao.com, and find that it has failed all the time. Find the solution.

the code is as follows:

var http = require("http")
var querystring = require("querystring")

var postData = querystring.stringify({
    "content": "!",
    "cid": "348"
})

var options = {
    hostname: "www.imooc.com",
    port: 80,
    path: "/course/docomment",
    method: "POST",
    headers: {
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Connection": "keep-alive",
        "Content-Length": postData.length,
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "Cookie": "imooc_uuid=6c55c3b0-3c94-4f53-a44e-6b6b7b105293; imooc_isnew_ct=1510198466; UM_distinctid=15fc3e34db2693-01d3aef2b9e856-5b44271d-1fa400-15fc3e34db39cd; CNZZDATA1261110065=1876606176-1510817537-https%253A%252F%252Fwww.baidu.com%252F%7C1515481428; imooc_isnew=2; loginstate=1; apsid=ZhM2VhNmI3MDM4ZjViMjFkZDg0MWIxNGYwNDlkZGUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjA5MDY5NAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA5ODY5OTI0ODRAcXEuY29tAAAAAAAAAAAAAAAAAAAAADc2ZjY0Y2MwMWZjZTA1NDBlYWU2YjE0Nzg1ZTU0MGE0d5aoWneWqFo%3DMT; CNZZDATA1273093942=1311350390-1520995090-%7C1520995090; PHPSESSID=9jo6kjqo4lrk4r06sfs0uokju1; IMCDNS=0; Hm_lvt_f0cfcccd7b1393990c78efdeebff3968=1521029597,1521461698,1521552907,1522654733; Hm_lpvt_f0cfcccd7b1393990c78efdeebff3968=1522655634; cvde=5ac1ddfbb8ac7-37",
        "Host": "www.imooc.com",
        "Origin": "https://www.imooc.com",
        "Referer": "https://www.imooc.com/comment/348",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
        "X-Requested-With": "XMLHttpRequest"
    }
}

//request
var req = http.request(options, function(res) {
    console.log("Status:" + res.statusCode)
    console.log("headers:" + JSON.stringify(res.headers))

    res.on("data", function(chunk) {
        console.log(Buffer.isBuffer(chunk))
    })

    res.on("end", function() {
        console.log("")
    })
})

req.on("error", function(e) {
    console.log("Error" + e.message)
})
//write
req.write(postData)
//
req.end()

found that the error has been reported all the time. Unsupported Request Method and Protocol .
Squid does not support all request methods for all access protocols.
For example, you can not POST a Gopher request.

Feb.28,2021

it's not your code's fault, it's the squid agent.


protocols are different, HTTP is replaced by HTTPS

Menu