2014-07-04 70 views
2

我對lua有麻煩。發送到lua的網站

我需要通過GET發送請求到網站,並從網站獲得響應。

大氣壓所有我有是這樣的:

local LuaSocket = require("socket") 
client = LuaSocket.connect("example.com", 80) 
client:send("GET /login.php?login=admin&pass=admin HTTP/1.0\r\n\r\n") 
while true do 
    s, status, partial = client:receive('*a') 
    print(s or partial) 
    if status == "closed" then 
    break 
    end 
end 
client:close() 

我應該怎麼做才能得到服務器的響應?

我想發送一些信息到這個網站,並得到頁面的結果。

任何想法?

+0

什麼'打印(S或部分)的問題'回來嗎?如果它正在打印零,那麼你的連接沒有建立。否則,'s'包含來自服務器的響應(包括標題信息)。 – hjpotter92

回答

0

嗨,大家好我解決了通過頭

local LuaSocket = require("socket") 
client = LuaSocket.connect("example.com", 80) 
client:send("GET /login.php?login=admin&pass=admin HTTP/1.0\r\nHost: example.com\r\n\r\n") 
while true do 
    s, status, partial = client:receive('*a') 
    print(s or partial) 
    if status == "closed" then 
    break 
    end 
end 
client:close() 
0

這可能不會工作,因爲*a將被讀取,直到連接關閉,但在這種情況下,客戶端不知道要讀多少。你需要做的是逐行讀取並解析頭文件以找到Content-Length,然後在看到兩行結束符後,讀取指定的字節數(如在Content-Length中設置的那樣)。

socket.http不是自己做所有事情(讀取和解析標題,處理重定向,100次繼續以及所有這些),socket.http將爲您處理所有這些複雜問題。嘗試是這樣的:

local http = require("socket.http") 
local body, code, headers, status = http.request("https://www.google.com") 
print(code, status, #body)