您的位置:首页 > 理论基础 > 计算机网络

httpclient for java 测试 openstack swift

2012-10-22 20:20 495 查看
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;

/**

* How to send a request directly using {@link HttpClient}.

*

* @since 4.0

*/

public class test_m {

public static void main(String[] args) throws Exception {

DefaultHttpClient 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

/*test put file

HttpPut httppost = new HttpPut(h1.getValue()+"/photos1/lzl.jpg");

httppost.addHeader(h2);

httppost.addHeader("Content-Type","image/jpeg");

httppost.addHeader("X-Object-Meta-lzl", "一张关于林志玲的美图");

File file = new File("/home/ralph/cute.jpg");

InputStreamEntity reqEntity = new InputStreamEntity(

new FileInputStream(file), -1);

reqEntity.setContentType("binary/octet-stream");

reqEntity.setChunked(true);

// It may be more appropriate to use FileEntity class in this particular

// instance but we are using a more generic InputStreamEntity to demonstrate

// the capability to stream out data from any arbitrary source

//

// FileEntity entity = new FileEntity(file, "binary/octet-stream");

httppost.setEntity(reqEntity);

System.out.println("executing request " + httppost.getRequestLine());

rsp = httpclient.execute(httppost);

//end test put file

*/

/* FileBody file = new FileBody(new File("/home/ralph/cute.jpg"));

MultipartEntity ef = new MultipartEntity();

ef.addPart("file", file);

HttpPut httppost = new HttpPut(h1.getValue()+"/photos1/lzl.jpg");

// httppost.setHeader("Content-Type","image/jpeg");

httppost.addHeader(h2);

// httppost.addHeader("X-Object-Meta-lzl", "一张关于林志玲的美图");

httppost.setEntity(ef);

rsp=httpclient.execute(httppost);*/

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", "一张关于林志玲的美图");

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("----------------------------------------");

//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) {

//这里可以得到文件的类型 如image/jpg /zip /tiff 等等 但是发现并不是十分有效,有时明明后缀是.rar但是取到的是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 {

// When HttpClient instance is no longer needed,

// shut down the connection manager to ensure

// immediate deallocation of all system resources

httpclient.getConnectionManager().shutdown();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: