用perl发送http请求
来源:互联网 发布:送货派单软件 编辑:程序博客网 时间:2024/05/29 14:35
工作中经常用到脚本发送http请求,但是用脚本发送和用浏览器发送差别很到,当你用脚本调用一些接口时可能没有权限,此时需要获取到调用接口的权限,一般情况下就是。请求的时候需要带着一个cookie值或者一个token值,浏览器发送时一般都会保存这个值,但是脚本发送不知道保存在那个地方还是不会生成(知道的小伙伴告诉我一声),但是一般的做法是带着用户名和密码去请求,服务器给你回复一个cookie值,然后下一次调用接口的时候就带着这个cookie值去请求;
获取cookie值
```sub getCookie{ my($ip,$port)=@_; my $ua = LWP::UserAgent->new( protocols_allowed => [ 'http', 'https' ], timeout => 30, ssl_opts => { verify_hostname => 0}, ); $username='admin'; $password='admin';#密码有没经过加密处理,如果经过加密处理要写加密后的 my $arg={ 'username' => $username, 'password' => $password, }; $url="https://$ip:$port/vapi/extjs/access/ticket";#url可以抓包获取,不是登录的那个地址 my $resp = $ua->post($url,$arg,'Content_Type' => 'form-data');#用post方法提交参数 my $status=$resp->status_line; my $header=$resp->headers_as_string;#解析头部 my $content = $resp->content; if ($status=~ /200/) { @array=split(/\n/,$header); foreach $rock (@array) { if ($rock=~/^Set-Cookie/) { @Setcookie=split(/\s+/,$rock); $Setcookie[1]=~s/;$//; my $cookie=$Setcookie[1]; $content=$cookie; last; } } } return $content;}
发送get请求
调用上面的getcookie函数,获取cookie值sub https_Get { my ( $url,$cookie) = @_; my $ua = LWP::UserAgent->new( protocols_allowed => [ 'http', 'https' ], timeout => 30, ssl_opts => { verify_hostname => 0 }, ); $ua->default_header('cookie' => "$cookie" ); my $response = $ua->get($url); return $response->{_content};}
发送post请求
sub https_Post{ my($url,$arg,$cookie) = @_; #arg为需要提交的参数 my $ua = LWP::UserAgent->new( protocols_allowed => [ 'http', 'https' ], timeout => 30, ssl_opts => { verify_hostname => 0}, ); $ua->default_header('cookie' => "$cookie" ); $resp = $ua->post($url,$arg,'Content_Type' => 'form-data'); #注意参数的格式是form-data还是其他格式,可用postman(一款模拟发送http请求的插件,只可以在谷歌浏览器中用)调试 $content = $resp->content; return $content; }
1 0
- 用perl发送http请求
- 用perl发送http请求
- Perl 发送http 请求。
- 用clientsocket发送http头请求
- 用telnet发送HTTP请求进行测试
- java-用HttpURLConnection发送Http请求.
- 用HTTP的PUT方式发送请求
- [NIO]用dawn发送接收HTTP请求
- php 用guzzule 6 发送HTTP请求
- C#发送HTTP请求
- java 发送http请求
- socket 发送http请求
- python发送HTTP请求
- 异步发送HTTP请求
- python发送HTTP请求
- Qt发送HTTP请求
- socket发送http请求
- 发送http请求
- PHP和JS判断字符串是否为数字
- 上机题目(初级)-小明的筷子(Java)
- 物联网重点
- unix 环境高级编程(1)
- 银行小系统知识 头文件,函数指针菜单
- 用perl发送http请求
- libusb学习-3
- Gevent tutorial (Gevent中文教程,Gevent中文翻译)
- hdoj 1087 Super Jumping! Jumping! Jumping! 【dp&&最大递增子段和】
- libusb学习-4
- Harry Potter's Exam
- 计算机基础知识-一级缓存与二级缓存,堆栈
- OpenStreetMap学习(一)
- SKSpriteKit 2-Physics and Collision