javaeye api httpclient 实现二

package com.javaeye.api;
import java.io.IOException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.client.ClientProtocolException;
public class TestClientUtils {
	public static void main(String[] args) {
		String str = null;
		try {
			str = ClientUtils.getJsonContent("api.iteye.com",
					"api/user_favorites/list", "username", "password");
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(str);
		JSONObject json1 = null;
		JSONObject json = JSONObject.fromObject("{\"a\":" + str + "}");
		JSONArray array = json.getJSONArray("a");
		for (int i = 0; i < array.size(); i++) {
			json1 = JSONObject.fromObject(array.get(i));
			System.out.print(json1.get("title"));
			System.out.print("\t");
			System.out.print(json1.get("url"));
			System.out.print("\n");
		}
	}

}

 

package com.javaeye.api;

import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

public class ClientUtils {
	public static String getJsonContent(String hostUrl, String getUrl,
			String userName, String password) throws ClientProtocolException,
			IOException {
		String jsonStr = "";
		DefaultHttpClient httpclient = new DefaultHttpClient();
		httpclient.getCredentialsProvider().setCredentials(
				new AuthScope(hostUrl, 80),
				new UsernamePasswordCredentials(userName, password));
		BasicHttpContext localcontext = new BasicHttpContext();
		BasicScheme basicAuth = new BasicScheme();
		localcontext.setAttribute("preemptive-auth", basicAuth);
		httpclient.addRequestInterceptor(
				(HttpRequestInterceptor) new PreemptiveAuth(), 0);
		HttpHost targetHost = new HttpHost(hostUrl);
		HttpGet httpget = new HttpGet("/" + getUrl);
		HttpResponse response = httpclient.execute(targetHost, httpget,
				localcontext);
		HttpEntity entity = response.getEntity();
		if (entity != null) {
			jsonStr = EntityUtils.toString(entity);
			entity.consumeContent();
		}
		httpclient.getConnectionManager().shutdown();
		return jsonStr;
	}
	static class PreemptiveAuth implements HttpRequestInterceptor {
		public void process(final HttpRequest request, final HttpContext context)
				throws HttpException, IOException {
			AuthState authState = (AuthState) context
					.getAttribute(ClientContext.TARGET_AUTH_STATE);

			// If no auth scheme avaialble yet, try to initialize it
			// preemptively
			if (authState.getAuthScheme() == null) {
				AuthScheme authScheme = (AuthScheme) context
						.getAttribute("preemptive-auth");
				CredentialsProvider credsProvider = (CredentialsProvider) context
						.getAttribute(ClientContext.CREDS_PROVIDER);
				HttpHost targetHost = (HttpHost) context
						.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
				if (authScheme != null) {
					Credentials creds = credsProvider
							.getCredentials(new AuthScope(targetHost
									.getHostName(), targetHost.getPort()));
					if (creds == null) {
						throw new HttpException(
								"No credentials for preemptive authentication");
					}
					authState.setAuthScheme(authScheme);
					authState.setCredentials(creds);
				}
			}

		}
	}
}

 

你可能感兴趣的:(apache,json,.net,Scheme)