Apache HttpClient
1 2 3 |
HttpClient client = factory.getHttpClient(); //or any method to get a client instance Credentials credentials = new UsernamePasswordCredentials(username, password); client.getState().setCredentials(AuthScope.ANY, credentials); |
1 2 3 4 5 |
CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("username", "password")); CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build(); |
imports
1 2 3 4 5 6 7 |
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(TESTURL); HostConfiguration config = client.getHostConfiguration(); config.setProxy(PROXY_HOST, PROXY_PORT); String username = "Kutay"; String password = "Zorlu"; Credentials credentials = new UsernamePasswordCredentials(username, password); AuthScope authScope = new AuthScope(PROXY_HOST, PROXY_PORT); client.getState().setProxyCredentials(authScope, credentials); client.getState().setCredentials(AuthScope.ANY, credentials); try { client.executeMethod(method); String response = method.getResponseBodyAsString(); if (method.getStatusCode() == HttpStatus.SC_OK) { response = method.getResponseBodyAsString(); } } catch (IOException e) { e.printStackTrace(); } finally { method.releaseConnection(); } |
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
try { DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials( new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials("kutay", "zorlu")); HttpPost httppost = new HttpPost("http://kutayzorlu.com:post/test/login"); System.out.println("executing request " + httppost.getRequestLine()); HttpResponse response; response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } if (entity != null) { entity.consumeContent(); } httpclient.getConnectionManager().shutdown(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } |
Example 3_
1 2 3 4 5 6 |
HttpPost httppost = new HttpPost(postData); CookieStore cookieStore = new BasicCookieStore(); BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", getSessionId()); cookieStore.addCookie(cookie); client.setCookieStore(cookieStore); response = client.execute(httppost); |
1 2 3 4 5 |
HttpContext localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); response = client.execute(httppost, localContext); |
1 2 3 4 5 6 7 8 9 |
HttpPost httppost = new HttpPost(postData); CookieStore cookieStore = new BasicCookieStore(); BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", getSessionId()); cookie.setDomain("kutayzorlu.com"); cookie.setPath("/posts"); cookieStore.addCookie(cookie); client.setCookieStore(cookieStore); response = client.execute(httppost); |