我的GET请求出了什么问题?

抱歉打扰了,这应该是一件简单的事情。

我有一个 HTTP GET 请求:

GET /ip HTTP/1.1
Host: httpbin.org
Connection: close
Accept: */*
User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)

当我通过我的 ESP8266 发送这个请求时,它返回一个 404 错误:

HTTP/1.1 404 Not Found
Date: Fri, 04 Sep 2015 16:34:46 GMT
Server: Apache
Content-Length: 1363
X-Frame-Options: deny
Connection: close
Content-Type: text/html

但是,当我(和你)访问 http://httpbin.org/ip 时,它完美地工作!

是什么出了问题?

细节

我用 Lua 构建了我的请求:

conn:on("connection", function(conn, payload)
    print('\nConnected')
    req = "GET /ip"
    .." HTTP/1.1\r\n"
    .."Host: httpbin.org\r\n"
    .."Connection: close\r\n"
    .."Accept: */*\r\n"
    .."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"
    .."\r\n"
    print(req)
    conn:send(req)
end)

如果我使用另一个主机(给出的例子是),它就可以工作:

conn:on("connection", function(conn, payload)
    print('\nConnected')
    conn:send("GET /esp8266/test.php?"
    .."T="..(tmr.now()-Tstart)
    .."&heap="..node.heap()
    .." HTTP/1.1\r\n"
    .."Host: benlo.com\r\n"
    .."Connection: close\r\n"
    .."Accept: */*\r\n"
    .."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"
    .."\r\n")
end)
点赞
用户2879085
用户2879085

这是由于您的请求行被服务器拒绝了。 以下方式可以解决:

GET http://httpbin.org/ip HTTP/1.1
Host: httpbin.org
2015-09-04 18:51:32
用户5404218
用户5404218

你确实正在连接到httpbin.org吗?还是别的地方?

我刚刚尝试通过在telnet中输入你的请求来发出请求,并取得了成功。但是响应服务器是nginx,而你的示例显示了apache。

$ telnet httpbin.org 80
Trying 54.175.219.8...
Connected to httpbin.org.
Escape character is '^]'.
GET /ip HTTP/1.1
Host: httpbin.org
Connection: close
Accept: */*
User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)

HTTP/1.1 200 OK
Server: nginx
Date: Wed, 07 Oct 2015 06:08:40 GMT
Content-Type: application/json
Content-Length: 32
Connection: close
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

{
  “origin”:“124.149.55.34”
}
Connection closed by foreign host.

当我尝试使用其他URI发出另一个请求来强制404响应时,我看到了这个:

HTTP/1.1 404 NOT FOUND
Server: nginx
Date: Wed, 07 Oct 2015 06:12:21 GMT
Content-Type: text/html
Content-Length: 233
Connection: close
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

这与你所说你从httpbin.org获得的响应完全不同。

2015-10-07 06:13:49
用户7422048
用户7422048
使用 `http.get` 方法向 `http://httpbin.org/ip` 发送 GET 请求并获取返回值,代码如下:

```lua
http.get("http://httpbin.org/ip", nil, function(code, data)
   if (code < 0) then
      print("HTTP request failed")
   else
      print(code, data)
   end
end)

使用 http.post 方法向 http://httpbin.org/post 发送 POST 请求并获取返回值,代码如下:

http.post('http://httpbin.org/post',
    'Content-Type: application/json\r\n',
    '{"hello":"world"}',
    function(code, data)
        if (code < 0) then
          print("HTTP request failed")
       else
          print(code, data)
       end
     end)

详细使用方法和参数说明请参考 此处

2017-01-15 16:38:39