Created
December 4, 2012 16:09
-
-
Save henryyan/4205615 to your computer and use it in GitHub Desktop.
使用HttpClient读取Activiti Rest接口数据
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.IOException; | |
| import org.apache.http.HttpEntity; | |
| import org.apache.http.HttpHost; | |
| import org.apache.http.HttpResponse; | |
| import org.apache.http.auth.AuthScope; | |
| import org.apache.http.auth.UsernamePasswordCredentials; | |
| import org.apache.http.client.AuthCache; | |
| import org.apache.http.client.ClientProtocolException; | |
| 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.BasicAuthCache; | |
| import org.apache.http.impl.client.DefaultHttpClient; | |
| import org.apache.http.protocol.BasicHttpContext; | |
| import org.apache.http.util.EntityUtils; | |
| public class TestRest { | |
| private static String REST_URI = "http://localhost:8080/activiti-rest/service/tasks?assignee=kermit"; | |
| public static void main(String[] args) throws ClientProtocolException, IOException { | |
| DefaultHttpClient client = new DefaultHttpClient(); | |
| client.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8080), new UsernamePasswordCredentials("kermit", "kermit")); | |
| // Create AuthCache instance | |
| AuthCache authCache = new BasicAuthCache(); | |
| // Generate BASIC scheme object and add it to the local auth cache | |
| BasicScheme basicAuth = new BasicScheme(); | |
| authCache.put(new HttpHost("localhost", 8080, "http"), basicAuth); | |
| // Add AuthCache to the execution context | |
| BasicHttpContext localcontext = new BasicHttpContext(); | |
| localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); | |
| HttpGet postMethod = new HttpGet(REST_URI); | |
| HttpResponse response = client.execute(postMethod, localcontext); | |
| HttpEntity entity = response.getEntity(); | |
| if (entity != null) { | |
| String content = EntityUtils.toString(entity, "UTF-8"); | |
| System.out.println(content); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment