Linux中使用CURL模拟网络请求
GET请求
1 2 3 4
| @GetMapping("/demo1") public String demo1(String name, String age) { return "demo01 => name = " + name + " age = " + age; }
|
1 2 3 4
|
curl localhost:8080/demo1 -G -d "name=Andu&age=20" demo01 => name = Andu age = 20
|
POST表单请求
1 2 3 4
| @PostMapping("/demo2") public String demo2(String name, String age) { return "demo02 => name = " + name + " age = " + age; }
|
1 2 3
| curl localhost:8080/demo2 -d "name=Andu&age=20" demo02 => name = Andu age = 20
|
文件上传请求
1 2 3 4
| @PostMapping("/demo3") public String demo3(String name,MultipartFile file) { return "dem03 => name = " + name + " file = " + file.getOriginalFilename(); }
|
1 2 3
| curl localhost:8080/demo3 -F "name=submit" -F "file=@C:\\Users\\BPDBSIR\\Desktop\\cmd.php" dem03 => name = submit file = cmd.php
|
POST/JSON请求
1
| curl -l -H "Content-type: application/json" -X POST -d '{"phone":"13521389587","password":"test"}' URL
|
文件下载请求
1
| curl http://1.15.235.91:8000/Bad.zip -O
|
发送Cookie
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| curl URL --cookie "xxx"
curl URL --cookie-jar cookie_file
curl -b 'foo=bar' https://taobao.com
curl -b 'foo1=bar' -b 'foo2=baz' https://taobao.com
curl -b cookies.txt https://www.taobao.com
|
自定义请求头
1 2 3 4 5 6 7 8 9
| curl -H 'Accept-Language: en-US' https://google.com
curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com
curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login
|
设置用户代理字符串
有些网站访问会提示只能使用IE浏览器来访问,这是因为这些网站设置了检查用户代理,可以使用curl把用户代理设置为IE,这样就可以访问了。使用–user-agent或者-A选项:
1 2
| curl URL --user-agent "Mozilla/5.0" curl URL -A "Mozilla/5.0"
|
其他HTTP头部信息也可以使用curl来发送,使用-H”头部信息” 传递多个头部信息,例如:
1
| curl -H "Host:URL" -H "accept-language:zh-cn" URL
|