2015-09-04 79 views
2

對不起,應該很容易的事情。我的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) 

如果我用另一臺主機(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) 
+0

你使用任何IP過濾?這意味着基於遠程IP地址的預期結果不同? – Mike

+0

顯示如何構建請求?你有網絡捕獲請求嗎? –

+0

也許服務器拒絕特定的'User-Agent'? – hjpotter92

回答

0

這是您的要求的服務器沒有按」喜歡。 這將完成這項工作:

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

你確實連接到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得到的迴應。

1
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

相關問題