使用HttpClient4.0调用JavaEye API

package com.javaeye.client;

import java.io.IOException;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

public class JavaEyeClientUtils {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static void main(String[] args) throws ClientProtocolException, IOException {
		String url = "http://api.iteye.com/api/auth/verify";
		String code = new String(Base64.encodeBase64("name:password".getBytes()));
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(url);
		
		Header header = new BasicHeader("authorization", code);
		httpget.addHeader(header);
		
		HttpResponse response = httpclient.execute(httpget);
		HttpEntity entity = response.getEntity();
		String jsonStr = EntityUtils.toString(entity);
		System.out.println(jsonStr);
      //释放连接
      httpclient.getConnectionManager().shutdown();        

	}

}

你可能感兴趣的:(java,apache)