如何使用netcat手动发出HTTP GET请求?
所以,我必须从任何一个城市获取温度 假设我想取回坎普尔的 如何使用Netcat发出HTTP GET请求 我正在做这样的事情如何使用netcat手动发出HTTP GET请求?,http,get,netcat,Http,Get,Netcat,所以,我必须从任何一个城市获取温度 假设我想取回坎普尔的 如何使用Netcat发出HTTP GET请求 我正在做这样的事情 nc -v rssweather.com 80 GET http://www.rssweather.com/wx/in/kanpur/wx.php HTTP/1.1 我甚至不知道我的方向是否正确。我找不到任何关于如何使用netcat发出HTTP get请求的好教程,因此我将其发布在这里。当然,你可以深入了解谷歌搜索的标准,但实际上,如果你只想获得一个URL,那么就不值得付
nc -v rssweather.com 80
GET http://www.rssweather.com/wx/in/kanpur/wx.php HTTP/1.1
我甚至不知道我的方向是否正确。我找不到任何关于如何使用netcat发出HTTP get请求的好教程,因此我将其发布在这里。当然,你可以深入了解谷歌搜索的标准,但实际上,如果你只想获得一个URL,那么就不值得付出努力 您也可以在侦听模式下在端口上启动netcat:
nc -l 64738
(有时nc-l-p64738
是正确的参数列表)
…然后使用真正的浏览器向该端口发出浏览器请求。只需在浏览器中键入http://localhost:64738
并请参阅
在您的实际情况中,问题在于HTTP/1.1不会自动关闭连接,但它会等待您要检索的下一个URL。解决方案很简单:
使用HTTP/1.0:
GET /this/url/you/want/to/get HTTP/1.0
Host: www.rssweather.com
<empty line>
扩展:在GET头之后,只写请求的路径部分。要从中获取数据的主机名属于主机:
头,如我的示例所示。这是因为多个网站可以在同一个Web服务器上运行,所以浏览器需要告诉他,它想从哪个网站加载页面。这对我很有用:
$ nc www.rssweather.com 80
GET /wx/in/kanpur/wx.php HTTP/1.0
Host: www.rssweather.com
然后双击
,即一次用于远程http服务器,一次用于nc
命令
来源:在MacOS上,您需要-c标志,如下所示:
Little-Net:~ minfrin$ nc -c rssweather.com 80
GET /wx/in/kanpur/wx.php HTTP/1.1
Host: rssweather.com
Connection: close
[empty line]
HTTP/1.1 200 OK
Date: Thu, 23 Aug 2018 13:20:49 GMT
Server: Apache
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html
然后,响应如下所示:
Little-Net:~ minfrin$ nc -c rssweather.com 80
GET /wx/in/kanpur/wx.php HTTP/1.1
Host: rssweather.com
Connection: close
[empty line]
HTTP/1.1 200 OK
Date: Thu, 23 Aug 2018 13:20:49 GMT
Server: Apache
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html
-c标志被描述为“发送CRLF作为行结束”
要兼容HTTP/1.1,如果要禁用keepalive,则需要主机头以及“连接:关闭”。使用python3 HTTP.server在本地测试它 这也是一个有趣的测试方法。在一个shell上,启动本地文件服务器:
python3 -m http.server 8000
然后在第二个shell上发出请求:
printf 'GET / HTTP/1.1\r\nHost: localhost\r\n\r\n' | nc localhost 8000
HTTP 1.1中需要主机:
头
这显示了目录的HTML列表,正如您在以下位置所看到的:
firefox http://localhost:8000
接下来,您可以尝试列出文件和目录并观察响应:
printf 'GET /my-subdir/ HTTP/1.1\n\n' | nc localhost 8000
printf 'GET /my-file HTTP/1.1\n\n' | nc localhost 8000
每次请求成功时,服务器都会打印:
127.0.0.1 - - [05/Oct/2018 11:20:55] "GET / HTTP/1.1" 200 -
确认收到了
example.com
此维护域是另一个良好的测试URL:
printf 'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n' | nc example.com 80
并与:
https
SSL
nc
似乎无法处理https
url。相反,您可以使用:
sudo apt-get install nmap
printf 'GET / HTTP/1.1\r\nHost: github.com\r\n\r\n' | ncat --ssl github.com 443
另见:
如果尝试nc
,它将挂起:
printf 'GET / HTTP/1.1\r\nHost: github.com\r\n\r\n' | nc github.com 443
并尝试端口80
:
printf 'GET / HTTP/1.1\r\nHost: github.com\r\n\r\n' | nc github.com 443
只需对https
版本给出重定向响应:
HTTP/1.1 301 Moved Permanently
Content-Length: 0
Location: https://github.com/
Connection: keep-alive
在Ubuntu 18.04上测试。您甚至不需要使用/安装netcat
- 通过一个未使用的文件描述符创建一个tcp套接字,即我在这里使用88
- 将请求写入其中
- 使用fd
exec 88<>/dev/tcp/rssweather.com/80 echo -e "GET /dir/Asia/India HTTP/1.1\nhost: www.rssweather.com\nConnection: close\n\n" >&88 sed 's/<[^>]*>/ /g' <&88
exec 88/dev/tcp/rssweather.com/80 echo-e“GET/dir/Asia/India HTTP/1.1\n主机:www.rssweather.com\n连接:close\n\n”>&88
sed的//]*>//g'谢谢。这帮了大忙。现在结果已经从错误的请求变为禁止。服务器不允许我访问该文件。有没有办法解决这个问题?HTTP/1.1 403禁止日期:2015年9月1日星期二22:14:50 GMT服务器:Apache连接:关闭内容类型:text/html;charset=iso-8859-1 403禁止禁止您无权访问此服务器上的/wx/in/kanpur/wx.php。
Apache/1.3.41服务器:www.rssweather.com Port 80@AvinashBhawnani谢谢。你可以随时编辑你的问题,长拷贝粘贴在评论中看起来不太好,在类似的情况下,如果你把它们编辑到你的问题中会更好。如果你还有其他问题,你可以在一个新问题中提问,这不是问题(如果你问了两次同样的问题)。这不是一个正确的答案,但我建议
或curl
。你可以使用wget
,或者提供一个HTTP/1.0
标题,如果您得到“HTTP/1.1 426需要升级”,请尝试将HTTP/1.0更改为HTTP/1.1这是linux的大写字母“-C”,作为对此答案的补充,如果没有它,上述建议将不起作用。连接:close