List of usage examples for org.apache.http.client.protocol ClientContext COOKIE_STORE
String COOKIE_STORE
To view the source code for org.apache.http.client.protocol ClientContext COOKIE_STORE.
Click Source Link
From source file:com.mama100.android.member.http.ClientCustomContext.java
public final static void main(String[] args) throws Exception { HttpClient httpclient = new DefaultHttpClient(); // Create a local instance of cookie store CookieStore cookieStore = new BasicCookieStore(); // Create local HTTP context HttpContext localContext = new BasicHttpContext(); // Bind custom cookie store to the local context localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet httpget = new HttpGet("http://www.weibo.com/"); System.out.println("executing request " + httpget.getURI()); // Pass local context as a parameter HttpResponse response = httpclient.execute(httpget, localContext); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); }/*ww w.j av a2 s .com*/ List<Cookie> cookies = cookieStore.getCookies(); for (int i = 0; i < cookies.size(); i++) { System.out.println("Local cookie: " + cookies.get(i)); } // Consume response content if (entity != null) { entity.consumeContent(); } System.out.println("----------------------------------------"); // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); }
From source file:com.hilatest.httpclient.apacheexample.ClientCustomContext.java
public final static void main(String[] args) throws Exception { HttpClient httpclient = new DefaultHttpClient(); // Create a local instance of cookie store CookieStore cookieStore = new BasicCookieStore(); // Create local HTTP context HttpContext localContext = new BasicHttpContext(); // Bind custom cookie store to the local context localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet httpget = new HttpGet("http://www.google.com/"); System.out.println("executing request " + httpget.getURI()); // Pass local context as a parameter HttpResponse response = httpclient.execute(httpget, localContext); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); }/* ww w . j av a 2 s . c om*/ List<Cookie> cookies = cookieStore.getCookies(); for (int i = 0; i < cookies.size(); i++) { System.out.println("Local cookie: " + cookies.get(i)); } // Consume response content if (entity != null) { entity.consumeContent(); } System.out.println("----------------------------------------"); // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); }
From source file:com.mtea.macrotea_httpclient_study.ClientCustomContext.java
public final static void main(String[] args) throws Exception { HttpClient httpclient = new DefaultHttpClient(); try {/*from w ww.j av a2s . c o m*/ // CookieStore CookieStore cookieStore = new BasicCookieStore(); // HttpContext HttpContext localContext = new BasicHttpContext(); //HttpContextCookieStore localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet httpget = new HttpGet("http://www.google.com/"); System.out.println("executing request " + httpget.getURI()); // ? HttpResponse response = httpclient.execute(httpget, localContext); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } //???cookie List<Cookie> cookies = cookieStore.getCookies(); for (int i = 0; i < cookies.size(); i++) { System.out.println("Local cookie: " + cookies.get(i)); } // EntityUtils.consume(entity); System.out.println("----------------------------------------"); } finally { //??httpclient??? httpclient.getConnectionManager().shutdown(); } }
From source file:com.dlmu.heipacker.crawler.client.ClientCustomContext.java
public final static void main(String[] args) throws Exception { HttpClient httpclient = new DefaultHttpClient(); try {//from w w w .ja v a 2 s .c om // Create a local instance of cookie store CookieStore cookieStore = new BasicCookieStore(); // Create local HTTP context HttpContext localContext = new BasicHttpContext(); // Bind custom cookie store to the local context localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet httpget = new HttpGet("http://www.google.com/"); System.out.println("executing request " + httpget.getURI()); // Pass local context as a parameter HttpResponse response = httpclient.execute(httpget, localContext); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } List<Cookie> cookies = cookieStore.getCookies(); for (int i = 0; i < cookies.size(); i++) { System.out.println("Local cookie: " + cookies.get(i)); } // Consume response content EntityUtils.consume(entity); System.out.println("----------------------------------------"); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:com.nebkat.plugin.youtube.YoutubeRetriever.java
public static String getLocation(String videoId) { CookieStore cookieStore = new BasicCookieStore(); HttpContext localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet get = new HttpGet(String.format(YOUTUBE_API_URL, videoId)); get.setHeader("User-Agent", USER_AGENT); HttpResponse response;// www.j a va2 s .c o m try { response = ConnectionManager.getHttpClient().execute(get, localContext); } catch (IOException ex) { get.abort(); return ""; } if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { get.abort(); return ""; } String videoInfoData; try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))) { StringBuilder builder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { builder.append(line); } videoInfoData = builder.toString(); } catch (IOException ex) { return ""; } Map<String, String> videoInfo = getNameValuePairMap(videoInfoData); String[] formats = videoInfo.get("url_encoded_fmt_stream_map").split(","); for (String format : formats) { Map<String, String> formatInfo = getNameValuePairMap(format); String itag = formatInfo.get("itag"); if (itag.equals(Integer.toString(PREFERRED_FORMAT))) { String url = formatInfo.get("url"); String sig = formatInfo.get("sig"); return url + "&signature=" + sig; } } return ""; }
From source file:simple.crawler.ext.vanilla.VanillaForumUtil.java
public static HttpContext login(String loginURL, String username, String password) throws Exception { DefaultHttpClient httpclient = HttpClientFactory.getInstance(); CookieStore cookieStore = new BasicCookieStore(); HttpContext httpContext = new BasicHttpContext(); httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpResponse res = httpclient.execute(new HttpGet(loginURL), httpContext); String html = HttpClientUtil.getContentBodyAsString(res); HtmlParser parser = new HtmlParser(); Document doc = parser.parseNonWellForm(html); ////from w w w. ja v a2 s . c o m Node loginTransientKey = (Node) XPathUtil.read(doc, "//*[@id=\"Form_TransientKey\"]", XPathConstants.NODE); Node hpt = (Node) XPathUtil.read(doc, "//*[@id=\"Form_hpt\"]", XPathConstants.NODE); Node target = (Node) XPathUtil.read(doc, "//*[@id=\"Form_Target\"]", XPathConstants.NODE); Node clientHour = (Node) XPathUtil.read(doc, "//*[@id=\"Form_ClientHour\"]", XPathConstants.NODE); // List<NameValuePair> list = new ArrayList<NameValuePair>(); list.add(new BasicNameValuePair("Form/TransientKey", ((Element) loginTransientKey).getAttribute("value"))); list.add(new BasicNameValuePair("Form/hpt", ((Element) hpt).getAttribute("value"))); list.add(new BasicNameValuePair("Form/Target", ((Element) target).getAttribute("value"))); list.add(new BasicNameValuePair("Form/ClientHour", ((Element) clientHour).getAttribute("value"))); list.add(new BasicNameValuePair("Form/Email", "admin")); list.add(new BasicNameValuePair("Form/Password", "admin")); list.add(new BasicNameValuePair("Form/Sign_In", "Sign In")); list.add(new BasicNameValuePair("Form/RememberMe", "1")); list.add(new BasicNameValuePair("Checkboxes[]", "RememberMe")); HttpPost post = new HttpPost(loginURL); post.setEntity(new UrlEncodedFormEntity(list)); res = httpclient.execute(post, httpContext); return httpContext; }
From source file:com.electronicpanopticon.spring.web.rest.StatefullRestTemplate.java
public StatefullRestTemplate() { super();//w w w. j a va 2s . com httpClient = new DefaultHttpClient(); cookieStore = new BasicCookieStore(); httpContext = new BasicHttpContext(); httpContext.setAttribute(ClientContext.COOKIE_STORE, getCookieStore()); statefullHttpComponentsClientHttpRequestFactory = new StatefullHttpComponentsClientHttpRequestFactory( httpClient, httpContext); super.setRequestFactory(statefullHttpComponentsClientHttpRequestFactory); }
From source file:com.grinnellplans.plandroid.SessionService.java
public SessionService() { super();// w ww . ja v a 2 s. c o m _httpContext = new BasicHttpContext(); _httpContext.setAttribute(ClientContext.COOKIE_STORE, new BasicCookieStore()); _loggedIn = false; _af = new AutofingerList(); set_serverName("grinnellplans.com"); }
From source file:net.giovannicapuano.galax.util.Utils.java
/** * Perform a HTTP POST request.// ww w.ja v a 2 s .c om */ public static HttpData postData(String path, List<NameValuePair> post, Context context) { HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 5000); HttpConnectionParams.setSoTimeout(httpParameters, 10000); int status = HttpStatus.SC_INTERNAL_SERVER_ERROR; String body = ""; HttpClient httpClient = new DefaultHttpClient(httpParameters); HttpContext localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.COOKIE_STORE, new PersistentCookieStore(context)); StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build()); try { HttpPost httpPost = new HttpPost(context.getString(R.string.server) + path); httpPost.setEntity(new UrlEncodedFormEntity(post)); HttpResponse response = httpClient.execute(httpPost, localContext); status = response.getStatusLine().getStatusCode(); body = EntityUtils.toString(response.getEntity()); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return new HttpData(status, body); }
From source file:org.wso2.am.integration.ui.tests.util.TestUtil.java
/** * Login to API Store or Publisher/*from w w w .j a va 2s. c o m*/ * * @param userName * @param password * @param URL API Store or Publisher URL * @return * @throws Exception */ public static HttpContext login(String userName, String password, String URL) throws Exception { CookieStore cookieStore = new BasicCookieStore(); HttpContext httpContext = new BasicHttpContext(); httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(URL + APIMTestConstants.APISTORE_LOGIN_URL); // Request parameters and other properties. List<NameValuePair> params = new ArrayList<NameValuePair>(3); params.add(new BasicNameValuePair(APIMTestConstants.API_ACTION, APIMTestConstants.API_LOGIN_ACTION)); params.add(new BasicNameValuePair(APIMTestConstants.APISTORE_LOGIN_USERNAME, userName)); params.add(new BasicNameValuePair(APIMTestConstants.APISTORE_LOGIN_PASSWORD, password)); httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); HttpResponse response = httpclient.execute(httppost, httpContext); HttpEntity entity = response.getEntity(); String responseString = EntityUtils.toString(entity, "UTF-8"); boolean isError = Boolean.parseBoolean(responseString.split(",")[0].split(":")[1].split("}")[0].trim()); if (isError) { String errorMsg = responseString.split(",")[1].split(":")[1].split("}")[0].trim(); throw new Exception("Error while Login to API Publisher : " + errorMsg); } else { return httpContext; } }