Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl中JSON的HTTP POST请求_Json_Perl_Httprequest_Lwp Useragent - Fatal编程技术网

Perl中JSON的HTTP POST请求

Perl中JSON的HTTP POST请求,json,perl,httprequest,lwp-useragent,Json,Perl,Httprequest,Lwp Useragent,我正在尝试配置无线设置,例如在Linksys EA4500 AP中将ssid设置为vinoth通道1。我可以用wireshark看到下面的帖子 3E@@!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;

我正在尝试配置无线设置,例如在Linksys EA4500 AP中将ssid设置为vinoth通道1。我可以用wireshark看到下面的帖子

3E@@!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}}]
以及守则:

    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";
    }
请帮我修一下这个。等待您宝贵的答复

附言:在Wireshark Post消息中,链接不正确,因为我无法发布超过2个链接的查询

谢谢, Vinoth

使用代码$req->授权\u basicadmin,admin;它表示发出任何请求时的授权:Basic YWRtaW46YWRtaW4=HTTP头

但是从wireshirk的头文件来看,它是X-JNAP-Authorization:Basic YWRtaW46YWRtaW4=,这与自定义头文件不同。因此,请从块中删除该授权,并使用以下授权:

$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');

我试图在我的问题中发布我的代码,但它说了很多索引约束,所以我删除了它。可能是Referer头?您的代码中没有它…referer头应该是什么?我在这个行动信息上看到了两个链接。你能告诉我如何解析它吗?我试过这个。我的$req=HTTP::Request->newPOST=>$请求->标题内容类型=>应用程序/json$req->content$json;我的$res=$ua->请求$req;$res->is_success的结果返回了内部服务器错误和错误代码500I。我尝试了上述操作,但仍然不起作用。来自Wireshark登录页面的Post消息:Post/JNAP HTTP/1.1 TE:deflate,gzip;q=0.3连接:TE,关闭主机:192.168.1.1 Referer:User-Agent:libwww-perl/6.03内容类型:application/json;charset=UTF-8 X-JNAP-Action:X-JNAP-Authorization:Basic YWRtaW46YWRtaW4=X-request-With:XMLHttpRequestHTTP/1.1 411所需长度\n连接:关闭\n日期:周二,2012年8月14日22:52:13 GMT\n服务器:lighttpd/1.4.28\n内容长度:357\n内容类型:text/html\n客户端日期:周四,2014年3月27日03:51:30 GMT\n客户端对等方:192.168.1.1:80\n客户端响应编号:1\n标题:411-所需长度\n\n我现在修复了内容长度错误。但是移动301是一个错误。HTTP/1.1 301永久移动\n日期:Wed,2012年8月15日00:03:42 GMT\n位置:\n服务器:lighttpd/1.4.28\n内容长度:0\n客户端日期:2014年3月27日星期四05:02:59 GMT\n客户端对等方:192.168.1.1:80\n客户端响应编号:1\n\n HTTP get代码:301 HTTP get msg:Moved Permanently最终获得。我在url中漏掉了一个。我给了,而不是。听起来很棒!!我离开了几个小时,所以无法回复。仅供参考,要处理重定向,您可以从LWP::UserAgent使用$ua->max_redirect2
$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');