HttpClient for Java test OpenStack Swift

Source: Internet
Author: User

Thank you for your reference documents and articles.

Package org.apache.http.examples.conn;
Import Java.io.File; 
Import Java.io.InputStream;
Import Org.apache.http.Header;
Import org.apache.http.HttpEntity;
Import Org.apache.http.HttpHost;
Import Org.apache.http.HttpResponse;
Import Org.apache.http.HttpStatus;
Import org.apache.http.client.HttpClient;
Import Org.apache.http.client.methods.HttpGet;
Import Org.apache.http.client.methods.HttpHead;
Import Org.apache.http.client.methods.HttpPut;
Import org.apache.http.entity.FileEntity;
Import org.apache.http.impl.client.DefaultHttpClient;
Import Org.apache.http.util.EntityUtils; 
Import Java.io.FileOutputStream;
 /** * To send a request directly using {@link httpclient}. * * @since 4.0 */public class Test_m {public static void main (string[] args) throws Exception {defaultht
        Tpclient httpclient = new Defaulthttpclient ();
            try {httphost target = new Httphost ("http://127.0.0.1", 8080, "http"); HttpGet req = new HttpGet ("Http://127.0.0.1:8080/auth/v1.0 ");
            Req.addheader ("X-storage-user", "admin:admin");            
            Req.addheader ("X-storage-pass", "admin");

            SYSTEM.OUT.PRINTLN ("Executing request to" + target);
            HttpResponse RSP = Httpclient.execute (target, req);
            HttpResponse RSP = Httpclient.execute (req);

            httpentity entity = rsp.getentity ();
            System.out.println ("----------------------------------------");
            System.out.println (Rsp.getstatusline ());
            header[] headers = rsp.getallheaders ();
            for (int i = 0; i < headers.length i++) {System.out.println (headers[i]);

            } System.out.println ("----------------------------------------");
            if (entity!= null) {SYSTEM.OUT.PRINTLN (entityutils.tostring (entity));
            Header H1=rsp.getfirstheader ("X-storage-url");
        Header H2=rsp.getfirstheader ("X-auth-token");    System.out.println (H1.getvalue () +h2.getvalue ());
            Test Head method Httphead hph=new Httphead (H1.getvalue ());
            Hph.addheader (H2);
            Rsp=httpclient.execute (HPH);
            Entity=rsp.getentity ();
            System.out.println ("----------------------------------------");
            System.out.println (Rsp.getstatusline ());
            Headers=null;
            headers = Rsp.getallheaders ();
            for (int i = 0; i < headers.length i++) {System.out.println (headers[i]);
            } System.out.println ("----------------------------------------");
            Test put method Httpput hpp=new httpput (h1.getvalue () + "/photos5");
            Hpp.addheader (H2);
            Rsp=httpclient.execute (HPP);
            Entity=rsp.getentity ();
            System.out.println ("----------------------------------------");
            System.out.println (Rsp.getstatusline ());
    Headers=null;        headers = Rsp.getallheaders ();
            for (int i = 0; i < headers.length i++) {System.out.println (headers[i]);
            } System.out.println ("----------------------------------------");
            Put file File = new file ("/home/ralph/cute.jpg");
            File File = new file ("/home/ralph/test.txt"); Fileentity entity1 = new Fileentity (file, "Text/plain;

            Charset=\ "utf-8\");
            Httpput HttpPost = new Httpput (H1.getvalue () + "/photos5/lzl.jpg");
            Httpput HttpPost = new Httpput (H1.getvalue () + "/photos5/lzl.txt");
            Httppost.setheader (H2);
            Httppost.setheader ("Content-type", "image/jpeg");
            Httppost.setheader ("Content-type", "Text/plain", charset=\ "utf-8\");
            Httppost.setheader ("X-object-meta-lzl", "a picture of the beauty of Lin Chi Ling");
            Httppost.setentity (entity1);
            Defaulthttpclient httpclient1 = new Defaulthttpclient (); Rsp=httpClient1.execute (HttpPost);
            Entity=rsp.getentity ();
            System.out.println ("----------------------------------------");
            System.out.println (Rsp.getstatusline ());
            Headers=null;
            headers = Rsp.getallheaders ();
            for (int i = 0; i < headers.length i++) {System.out.println (headers[i]);   
} System.out.println ("----------------------------------------"); 
            Put file//download file httpget httpget = new HttpGet (H1.getvalue () + "/photos5/lzl.jpg");
            Httpget.addheader (H2);
            Defaulthttpclient Httpclient2 = new Defaulthttpclient ();  
            HttpResponse response = Httpclient2.execute (HttpGet);  
                if (Httpstatus.sc_ok==response.getstatusline (). Getstatuscode ()) {entity = response.getentity (); if (Entity!= null) {System.out.println (Entity.getcontenttype ());  
                    System.out.println (Entity.isstreaming ());    
                    File StoreFile = new file ("/home/ralph/cute1.jpg");  
                    FileOutputStream output = new FileOutputStream (storefile);  
                    InputStream input = Entity.getcontent ();  
                    byte b[] = new byte[1024];  
                    int j = 0;  
                    while ((j = Input.read (b))!=-1) {output.write (b,0,j);  
                    } output.flush ();   
                Output.close ();  
                } if (entity!= null) {entity.consumecontent (); }//end Download File} finally {//Then HttpClient instance is no longer needed,//Shut down the Connection Manager to ensure//immediate deallocation of all SyStem Httpclient.getconnectionmanager (). Shutdown ();
 }
    }

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.