【问题标题】:What is wrong with my GET request?我的 GET 请求有什么问题?
【发布时间】:2015-11-30 21:33:38
【问题描述】:

很抱歉打扰了一些应该很容易的事情。

我有这个 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)

如果我使用另一台主机 (given is this example),它可以工作:

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)

【问题讨论】:

  • 您是否使用任何 IP 过滤?这意味着基于远程 IP 地址的预期结果会有所不同吗?
  • 显示你是如何构建请求的?你有请求的网络捕获吗?
  • 也许服务器拒绝了那个特定的User-Agent
  • 请求似乎有效,并且将确切的请求粘贴到针对该服务器的 telnet 连接中...有效。
  • 你搞定了吗?

标签: http lua httprequest http-get nodemcu


【解决方案1】:

您实际上是在连接到 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 得到的响应完全不同。

【讨论】:

    【解决方案2】:
    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',
        '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)
    

    请参阅参考资料here

    【讨论】:

      【解决方案3】:

      服务器不喜欢你的请求行。 这将完成这项工作:

      GET http://httpbin.org/ip HTTP/1.1
      Host: httpbin.org
      

      【讨论】:

        猜你喜欢
        • 2019-09-02
        • 1970-01-01
        • 1970-01-01
        • 2013-11-20
        • 1970-01-01
        • 2021-10-20
        • 2016-09-01
        • 1970-01-01
        • 2012-06-06
        相关资源
        最近更新 更多