zoukankan      html  css  js  c++  java
  • perl 模拟curl 自定义请求头

    auth ping
    
    curl -H 'Authorization: Token 24fd3c026886e3121b2ca630805ed425c272cb96' https://cloud.seafile.com/api2/auth/ping/
    
    "pong"
    
    
       -H/--header <header>
                  (HTTP)  Extra  header to use when getting a web page. You may specify any number of extra headers.
     Note that if you should add a custom header that has the same name as one of the internal
                  ones curl would use, your externally set header will be used instead of the internal one. 
    This allows you to make even trickier stuff than curl would normally do. You  should  not  replace
                  internally  set  headers  without  knowing  perfectly  well  what  you’re doing. 
    Remove an internal header by giving a replacement without content on the right side of the colon, as in: -H
                  "Host:".
    
                  curl will make sure that each header you add/replace is sent with the proper end-of-line marker, you should thus not add that as a part of the header content: do not add newlines  or  car-
                  riage returns, they will only mess things up for you.
    
                  See also the -A/--user-agent and -e/--referer options.
    
                  This option can be used multiple times to add/replace/remove multiple headers.
    
    
    
    (HTTP) 额外的header 来使用当 请求一个web 页面,你可以指定任何数量的额外请求头。
    
    
    注意,如果你可以增加一个自定义的请求头
    
    
    
    use  LWP::UserAgent; 
    use LWP;
    use Encode;
    use LWP::Simple;
    use LWP::UserAgent;
    use HTTP::Cookies;
    use HTTP::Headers;
    use HTTP::Response;
    use Encode;
    use URI::Escape;
    use URI::URL;
    use JSON;
      my $ua = LWP::UserAgent->new;
      $ua->agent("Mozilla/5.0 (Windows NT 6.1; rv:30.0) Gecko/20100101 Firefox/30.0");
      my $cookie_jar = HTTP::Cookies->new(
         file=>'lwp_cookies.txt',
         autosave=>1,
         ignore_discard=>1);
         $ua->cookie_jar($cookie_jar);
       my $token_url= 'http://127.0.0.1:8000/api2/auth-token/';
                    
      
       my $res = $ua->post($token_url,
                    {
                    'username'=>'015208@zjtlcb.com',
                    'password'=>'1234567'
                    });
       print $res->content();
       print "
    ";
       my $r= $res->content();
       my $r=encode_utf8($r);
       my $hash = decode_json($r);
       my $token =$hash->{"token"};
       print "$r is $r
    ";
       my $host = "http://127.0.0.1:8000/api2/auth/ping/";
       @header = (  
        'accept'=> "application/json",  
        'content-type'=> "application/json",  
        'Authorization'=> "Token $token" 
        );  
        
        $host="http://127.0.0.1:8000/api2/auth/ping/";  
        $request = HTTP::Request->new(GET=>"$host");  
        $request->header(@header);  
      
        $response = $ua->request($request);   
        print $response->decoded_content;  
     


    
                                        
    
  • 相关阅读:
    编程语言的简介
    ava 8 stream的详细用法
    Java 8 Steam 例子整理
    redis常用命令
    常用正则表达式
    保留一些常用文章
    tag的简单使用
    GitFlow详解教程
    Git基本命令和GitFlow工作流
    Redis 2.8.18 安装报错 error: jemalloc/jemalloc.h: No such file or directory
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349915.html
Copyright © 2011-2022 走看看