Skip to content

Instantly share code, notes, and snippets.

@zlei1
Created March 1, 2019 08:25
Show Gist options
  • Save zlei1/cbb1e3ac1dcba2c5f531c7c56a60593b to your computer and use it in GitHub Desktop.
Save zlei1/cbb1e3ac1dcba2c5f531c7c56a60593b to your computer and use it in GitHub Desktop.
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