Created
March 1, 2019 08:25
-
-
Save zlei1/cbb1e3ac1dcba2c5f531c7c56a60593b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
host = 'localhost' # web服务器 | |
port = 80 # 默认 HTTP 端口 | |
path = "/data.json" # 想要获取的文件地址 | |
# 这是个 HTTP 请求 | |
request = "GET #{path} HTTP/1.0\r\n\r\n" | |
socket = TCPSocket.open(host,port) # 连接服务器 | |
socket.print(request) # 发送请求 | |
response = socket.read # 读取完整的响应 | |
# Split response at first blank line into headers and body | |
headers,body = response.split("\r\n\r\n", 2) | |
print body # 输出结果 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment