1. 程式人生 > >perl post json資料

perl post json資料



use LWP::UserAgent;
use URI::Escape;
use Net::Ping;
use JSON qw(encode_json);
use Socket;
use Net::SMTP;
use LWP;
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Headers;
use HTTP::Response;
use Encode;
use URI::Escape;
use URI::URL;
use File::Temp qw/tempfile/;
use AE;
my $now          = time();
my $ua = LWP::UserAgent->new;
$ua->timeout(5);
$ua->env_proxy;
$ua->agent("Mozilla/8.0");
my $cookie_jar = HTTP::Cookies->new(

    file           => 'lwp_cookies.txt',
    autosave       => 1,
    ignore_discard => 1
);
$ua->cookie_jar($cookie_jar);
###獲取uuid,用於生成2維碼
my $host =
"https://login.weixin.qq.com/jslogin?appid=wx782c26e4c19acffb&redirect_uri=https%3A%2F%2Fwx.qq.com%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxnewloginpage&fun=new&lang=zh_CN&_=$now";
my $response = $ua->get($host);
$ua->default_headers;

if ( $response->is_success ) {
    $r = $response->decoded_content;
	print $r;
    print "\n";
    }
else {
    die $response->status_line;  
   };
   
 my $uuid = $1  if ( $r =~ /window\.QRLogin\.code = 200; window\.QRLogin\.uuid = "(.*?)"/g ) ;
 print "\$uuid is $uuid\n";
 
 ###根據uuid,生成驗證碼
my $api      = "https://login.weixin.qq.com/qrcode/$uuid";
my $response = $ua->get($api);
$ua->default_headers;
if ( $response->is_success ) {
    $r = $response->decoded_content;    # 驗證碼內容
    print "\n";
}
else {
    die $response->status_line;
};
my ( $fh, $filename ) =
  tempfile( "weixin_qrcode_XXXX", SUFFIX => ".jpg", DIR => '/tmp' );
  binmode $fh;
  print $fh $r;
  close $fh;
  print "登入二維碼已經下載到本地 [ $filename ] \n";

#https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid=waJHHDI-og==&tip=1&r=-1969830274&_=1479438579726
# window.code=201;window.userAvatar = 'data:img/jpg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDABQODxIP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q1rHHVONqkqps0qVoo2weaU49GWpoX
# /HG22rLDcIF0RDqbsbf6lNkoCybTi+exXYsgpoCywEtrsAGVdhkwAqBFeRsAATsJJWr7oAAR5Zvi39xoXKXLYAdOqztvrZBbUkixABybMnwRuYAAxHQAKgl2ITtJMAIqVygAAP
# /Z';  
#如果返回是201 就把$show_tip 設定為0
my $show_tip = 1;
sub login {
    while (1) {
        my $api ="https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid=$uuid&tip=$show_tip&r=-$now&_=$now";
        my $response = $ua->get($api);
		print "1--\$show_tip is $show_tip\n";
		print "2--\$api is $api\n";
		sleep (2);
        $ua->default_headers;
        if ( $response->is_success ) {
            $r = $response->decoded_content;
            print "\$r is $r\n";
            next unless defined $r;
            my %data = $r =~ /window\.(.+?)=(.+?);/g;
            if ( $data{code} == 201 ) {
                print"手機微信掃碼成功,請在手機微信上點選 [登入] 按鈕...\n";
                $show_tip = 0;
				##回到迴圈頂部,此時請求的url是"https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid=$uuid&tip=$show_tip&r=-$now&_=$now";
				
                next;
            }
            elsif ( $data{code} == 200 ) {
                print "正在進行登入...\n";
                foreach ($r) {
                    if ( $_ =~ /window.redirect_uri="(.*?)"/ ) 
					   {
                        $api = $1;
                        print "\$api is $api\n";
                        my $response = $ua->get($api);
                        $ua->default_headers;
                      }
                }
				};
              
                    if ( $api =~ /https\:\/\/wx\.qq\.com\/cgi\-bin\/mmwebwx-bin\/webwxnewloginpage\?ticket=(.*?)\&uuid=(.*?)\&lang=(.*?)\&scan=(.*)/
                      )
                    {
                        print "\$1 is $1\n";
                        print "\$2 is $2\n";
                        print "\$3 is $3\n";
                        print "\$4 is $4\n";
                        $ticket=$1;
                        $user = $4;
                        print "\$user is $user\n";
                        return 1;
                    }
					}
            else {
                die $response->status_line;
                 }

        }
	};
	&login();
	    my $api ="https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage?ticket=$ticket&uuid=$uuid&lang=zh_CN&scan=$user&fun=new&version=v2&lang=zh_CN";
        my $response = $ua->get($api);
        $ua->default_headers;

        if ( $response->is_success ) {
          print $response->decoded_content;    # or whatever
          $r = $response->decoded_content;
          if ( $r =~/\<error.*\<skey\>(.*?)\<\/skey\>\<wxsid\>(.*?)\<\/wxsid\>\<wxuin\>(.*?)\<\/wxuin\>\<pass_ticket\>(.*?)\<\/pass_ticket.*/
          )
        {
            $Skey        = $1;
            $wxsid       = $2;
            $wxuin       = $3;
            $pass_ticket = $4;

            print "\$Skey is $Skey\n";
            print "\$wxsid is $wxsid\n";
            print "\$wxuin is $wxuin\n";
            print "\$pass_ticket is $pass_ticket\n";
            $Skey_x = uri_escape($Skey);
            use POSIX;
            my $a = "e";
            for ( my $b = 0 ; 15 > $b ; $b++ ) {
                $a .= POSIX::floor( 10 * rand() );
            }
            $DeviceID = $a;
        }

    };
my @chatroom_id = ();

#my $response= $browser->post("https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit", [ "r" => "-$now","lang" => "zh_CN","pass_ticket"=>"$pass_ticket"]);
my     $login_url ="https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r=-$now&lang=zh_CN&pass_ticket=$pass_ticket";
    my $post = {
        BaseRequest => {
            Uin      => $wxuin,
            Sid      => $wxsid,
            Skey     => $Skey,
            DeviceID => $DeviceID,
        }
    };
    use JSON qw(encode_json);
    $json_string = encode_json($post);

    my $req = HTTP::Request->new(
        'POST' => $login_url,
        [ 'r' => "-$now", 'lang' => 'zh_CN', 'pass_ticket' => "$pass_ticket" ]
    );
    $req->referer("https://wx.qq.com/?&lang=zh_CN");
    $req->content_type('application/json; charset=UTF-8')
      ;    #post請求,如果有傳送引數,必須要有這句
    $req->content("$json_string");    #傳送post的引數
    my $res = $ua->request($req);
	print $res->content();            #獲取的是響應正文