2014-03-25 198 views
0

我試圖在Linksys EA4500 AP中配置無線設置(如將ssid設置爲「vinoth」通道爲「1」)。我可以使用wireshark看到下面的POST消息。在POST中對JSON的HTTP POST請求

[email protected]@!dPzY+2K 
SPOST /JNAP/ HTTP/1.1 
Host: 192.168.1.1 
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:22.0) Gecko/20100101 Firefox/22.0 
Accept: */* 
Accept-Language: en-US,en;q=0.5 
Accept-Encoding: gzip, deflate 
Content-Type: application/json; charset=UTF-8 
X-JNAP-Action: http:cisco.com/jnap/core/Transaction 
X-JNAP-Authorization: Basic YWRtaW46YWRtaW4= 
X-Requested-With: XMLHttpRequest 
Referer: http:/cisco.com/  <<<<==== routerip 
Content-Length: 474 
Cookie: initial-tab=; ui-proxy-path=local; admin-auth=Basic%20YWRtaW46YWRtaW4%3D; current-applet=7B1F462B-1A78-4AF6-8FBB-0C221703BEA4 
Connection: keep-alive 
Pragma: no-cache 
Cache-Control: no-cache 

[{"action":"http:/cisco.com/jnap/wirelessap/SetRadioSettings","request":{"radios":[{"radioID":"RADIO_2.4GHz","settings":{"isEnabled":true,"mode":"802.11bgn","ssid":"vinoth","broadcastSSID":true,"channelWidth":"Auto","channel":1,"security":"None"}}]}},{"action":"http://cisco.com/jnap/guestnetwork/SetGuestNetworkSettings","request":{"isGuestNetworkEnabled":false,"guestSSID":"vinoth-guest","guestPassword":"BeMyGuest","maxSimultaneousGuests":5,"broadcastGuestSSID":false}}] 

我嘗試使用Perl自動執行上述操作,但它沒有鍛鍊。由於我對這個JSON頁面不熟悉,我不知道POST消息的發送方式。

內容我保留$ JSON是

'{"action":"http:/cisco.com/jnap/wirelessap/SetRadioSettings",' . 
     '"request":{"radios":{"radioID":"RADIO_2.4GHz","settings":'.'{"isEnabled":true,"mode":"802.11bgn","ssid":"vinoth212","broadcastSSID":true,"channelWidth":"Auto","channel":1,"security":"None"}}}},{"action":"http://cisco.com/jnap/guestnetwork/SetGuestNetworkSettings",' . 
    '"request":{"isGuestNetworkEnabled":false,"guestSSID":"vinoth-guest","guestPassword":"BeMyGuest","maxSimultaneousGuests":5,"broadcastGuestSSID":false}}'; 

,代碼:

use strict; 
    use warnings; 

    use LWP; 

    my $ua = LWP::UserAgent->new; 
    my $ip = $self->{ip}; 
    my $url = "http://$ip/"; 
    my $json = ""; 

    my $req = HTTP::Request->new(POST=>$url); 
    $req->header('content-type' => 'application/json'); 
    $req->authorization_basic("admin", "admin"); 
    $req->content($json); 

下面的代碼檢查請求的狀態

my $res = $ua->request($req); 
    if ($res->is_success) { 
    my $message = $res->decoded_content; 
    print "received the message"; 
    } else { 
    print "HTTP get code: ", $res->code, "\n"; 
    print "HTTP get: msg: ", $res->message, "\n"; 
    } 

請幫我解決這個。等待您的寶貴回覆。

P.S:在Wireshark Post消息中,鏈接不正確,因爲我無法發佈包含多於2個鏈接的查詢。

謝謝, Vinoth

+0

我想張貼我的代碼在我的問題,但它說很多indendation約束,所以我刪除它。 – user3441116

+0

可能是Referer頭部?你的代碼中沒有它... – gangabass

+0

什麼應該是referer頭?我看到了該行動訊息的兩個鏈接。你能告訴我如何解析它? – user3441116

回答

0

使用代碼$req->authorization_basic("admin", "admin");它意味着Authorization: Basic YWRtaW46YWRtaW4= HTTP頭進行任何請求的情況下。

但是從你的wireshirk的頭文件是X-JNAP-Authorization: Basic YWRtaW46YWRtaW4=,這是不同的東西(自定義頭文件)。所以刪除authorization_basic從您的塊,並使用這些的:

$req->header('Content-Type' => 'application/json; charset=UTF-8'); 
$req->header('X-JNAP-Action' => 'http:cisco.com/jnap/core/Transaction'); 
$req->header('X-JNAP-Authorization' => 'Basic YWRtaW46YWRtaW4='); 
## YWRtaW46YWRtaW4= is base64 of admin:admin 
$req->header('X-Requested-With' => 'XMLHttpRequest'); 
+0

我試過上面的但仍然不起作用從Wireshark發佈消息(嘗試登錄頁面):POST/JNAP HTTP/1.1 TE:deflate,gzip; q = 0.3 Connection:TE,close Host:192.168.1.1 Referer:http://192.168.1.1/ui/dynamic/login.html User-Agent:libwww-perl/6.03 Content-Type:application/json; X-JNAP-Action:http://cisco.com/jnap/core/CheckAdminPassword X-JNAP-Authorization:Basic YWRtaW46YWRtaW4 = X-Requested-With:XMLHttpRequest – user3441116

+0

「HTTP/1.1 411 Length熱曲ired \ n連接:關閉\ n日期:2012年8月14日星期二22:52:13 GMT \ nServer:lighttpd/1.4.28 \ n內容長度:357 \ n內容類型:text/html \ n客戶端日期: 2014 03:51:30 GMT \ nClient-Peer:192.168.1.1:80 \ nClient-Response-Num:1 \ nTitle:411 - 長度要求\ n \ n <?xml version = \「1.0 \」encoding = \「 iso-8859-1 \「?> \ n \ n \ n 411 - 需要長度 \ n \ n \ n

411 - 需要長度

\ n \ n \ n」 – user3441116

+0

我現在固定內容長度錯誤,但越來越「301 Moved Permanently」錯誤。「HTTP/1.1 301永久移動\ n日期:2012年8月15日星期三00:03:42 GMT \ n位置:http://192.168.1.1/JNAP/\nServer:lighttpd/1.4.28 \ nContent-Length:0 \ nClient-Date:Thu,2014年3月27日05:02:59 GMT \ nClient-Peer:192.168.1.1:80 \ nClient-Response-Num:1 \ n \ n「 HTTP獲取代碼:301 HTTP get msg:永久移動 – user3441116