List of usage examples for org.apache.http.impl.client DefaultHttpClient getCookieStore
public synchronized final CookieStore getCookieStore()
From source file:com.abombrecords.amt_unlocker.AMT_Unlocker.java
public static boolean UnlockDoor(String Username, String Password, String DoorPIN) throws Exception { boolean bSuccess = false; //Log.d(LogTag, "Login"); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httpost = new HttpPost("http://acemonstertoys.org/node?destination=node"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("op", "Log in")); nvps.add(new BasicNameValuePair("name", Username)); nvps.add(new BasicNameValuePair("pass", Password)); nvps.add(new BasicNameValuePair("openid.return_to", "http://acemonstertoys.org/openid/authenticate?destination=node")); nvps.add(new BasicNameValuePair("form_id", "user_login_block")); nvps.add(new BasicNameValuePair("form_build_id", "form-4d6478bc67a79eda5e36c01499ba4c88")); httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); HttpResponse response = httpclient.execute(httpost); HttpEntity entity = response.getEntity(); //Log.d(LogTag, "Login form get: " + response.getStatusLine()); if (entity != null) { entity.consumeContent();//from w w w .j a va 2s . c om } //Log.d(LogTag, "Post Login cookies:"); // look for drupal_uid and fail out if it isn't there List<Cookie> cookies = httpclient.getCookieStore().getCookies(); if (cookies.isEmpty()) { //Log.d(LogTag, "None"); } else { for (int i = 0; i < cookies.size(); i++) { //Log.d(LogTag, "- " + cookies.get(i).toString()); if (cookies.get(i).getName().equals("drupal_uid")) { bSuccess = true; } } } if (bSuccess) { HttpPost httpost2 = new HttpPost("http://acemonstertoys.org/membership"); List<NameValuePair> nvps2 = new ArrayList<NameValuePair>(); nvps2.add(new BasicNameValuePair("doorcode", DoorPIN)); nvps2.add(new BasicNameValuePair("forceit", "Open Door")); httpost2.setEntity(new UrlEncodedFormEntity(nvps2, HTTP.UTF_8)); response = httpclient.execute(httpost2); entity = response.getEntity(); //Log.d(LogTag, "Unlock form get: " + response.getStatusLine()); if (entity != null) { entity.consumeContent(); } } httpclient.getConnectionManager().shutdown(); return bSuccess; }
From source file:com.nineash.hutsync.client.NetworkUtilities.java
/** * Connects to the SampleSync test server, authenticates the provided * username and password./*w w w .j av a 2 s.c o m*/ * * @param username The server account username * @param password The server account password * @return String The authentication token returned by the server (or null) */ public static String authenticate(String username, String password, Context context) { try { final HttpResponse resp; final HttpResponse init_resp; final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); DefaultHttpClient hClient = getHttpClient(context); final HttpPost init_post = new HttpPost(BASE_URL); final int first_cookies; final ArrayList<SerializableCookie> saveCooks = new ArrayList<SerializableCookie>(); BasicCookieStore bcs = new BasicCookieStore(); params.add(new BasicNameValuePair(PARAM_USERNAME, username)); params.add(new BasicNameValuePair(PARAM_PASSWORD, password)); params.add(new BasicNameValuePair(PARAM_REMEMBER, "yes")); init_resp = hClient.execute(init_post); String respString = EntityUtils.toString(init_resp.getEntity()); List<Cookie> cookies = hClient.getCookieStore().getCookies(); if (cookies.isEmpty()) { Log.e(TAG, "No cookies gathered first time round"); } first_cookies = cookies.size(); if (first_cookies != 2) { Log.e(TAG, "Should be two cookie to start off with"); } Document doc = Jsoup.parse(respString); Elements hiddens = doc.select("div.homepage input[type=hidden]"); for (Element hidden : hiddens) { params.add(new BasicNameValuePair(hidden.attr("name"), hidden.attr("value"))); } final HttpEntity entity; try { entity = new UrlEncodedFormEntity(params); } catch (final UnsupportedEncodingException e) { // this should never happen. throw new IllegalStateException(e); } Log.i(TAG, "Authenticating to: " + AUTH_URI); final HttpPost post = new HttpPost(AUTH_URI); post.addHeader(entity.getContentType()); post.setEntity(entity); resp = hClient.execute(post); String authToken = null; if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { //set authtoken here cookies = hClient.getCookieStore().getCookies(); if (cookies.isEmpty()) { Log.e(TAG, "No cookies gathered"); } else { if (cookies.size() == first_cookies + 2) { //we get two new cookies when we log in for (int i = 0; i < cookies.size(); i++) { Cookie cur_cookie = cookies.get(i); if (cur_cookie.isPersistent()) { saveCooks.add(new SerializableCookie(cur_cookie)); } } authToken = toString(saveCooks); } } } if ((authToken != null) && (authToken.length() > 0)) { Log.v(TAG, "Successful authentication"); return authToken; } else { Log.e(TAG, "Error authenticating" + resp.getStatusLine()); return null; } } catch (final IOException e) { Log.e(TAG, "IOException when getting authtoken", e); return null; } finally { Log.v(TAG, "getAuthtoken completing"); } }
From source file:com.cappuccino.requestframework.CookieManager.java
public static void loadCookie(DefaultHttpClient client) { // /*w w w . j a v a2 s. c o m*/ if (CookieManager.initialized == false) { throw new RuntimeException(" ?"); } if (client == null) { throw new IllegalArgumentException(); } // ? if (application.hasCookie() == false) { // ? ? return; } else { // ? ? String cookieName = application.getCookieName(); String cookieValue = application.getCookieValue(); String cookieDomain = application.getCookieDomain(); if (RequestConfig.debuggable() == true) { Log.e(); Log.e(" ?"); Log.e(" ? : " + cookieName); Log.e(" : " + cookieValue); Log.e(" ?? : " + cookieDomain); } // BasicClientCookie cookie = new BasicClientCookie(cookieName, cookieValue); cookie.setDomain(cookieDomain); // client.getCookieStore().addCookie(cookie); } }
From source file:simple.crawler.HttpClientUtilTestCase.java
@Test public void testAddCookie() throws Exception { DefaultHttpClient httpclient = HttpClientFactory.createNewDefaultHttpClient(); BasicClientCookie cookie = (BasicClientCookie) HttpClientUtil.addCookie(httpclient, "jsession", "12345"); cookie.setDomain("org.simple"); cookie.setPath("/"); cookie.setSecure(false);//from ww w . j a va 2 s . c o m List<Cookie> cookies = httpclient.getCookieStore().getCookies(); assertEquals(1, cookies.size()); assertEquals("jsession", cookies.get(0).getName()); assertEquals("12345", cookies.get(0).getValue()); assertEquals("org.simple", cookies.get(0).getDomain()); assertEquals("/", cookies.get(0).getPath()); assertFalse(cookies.get(0).isSecure()); }
From source file:com.mingsoft.util.proxy.Proxy.java
/** * get/*from w ww . java2 s .com*/ * * @param url * ?<br> * @param header * ?Header new Header()<br> * @param params * ?<br> * @return Result */ public static Result get(String url, com.mingsoft.util.proxy.Header header, Map params) { //log.info("?" + url); // Httpclient DefaultHttpClient client = new DefaultHttpClient(); // ?? url = url + (null == params ? "" : assemblyParameter(params)); // get HttpGet httpGet = new HttpGet(url); // cookie?() httpGet.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY); // ? if (null != header && header.getHeaders().size() > 0) { httpGet.setHeaders(Proxy.assemblyHeader(header.getHeaders())); } // ?HttpResponse HttpResponse response; try { response = client.execute(httpGet); // // httpGet.abort(); // HttpEntity HttpEntity entity = response.getEntity(); // ?? Result result = new Result(); // cookie result.setCookie(assemblyCookie(client.getCookieStore().getCookies())); // ? result.setStatusCode(response.getStatusLine().getStatusCode()); // result.setHeaders(response.getAllHeaders()); // ? result.setHttpEntity(entity); return result; } catch (ClientProtocolException e) { e.printStackTrace(); log.error(e.getMessage()); } catch (IOException e) { e.printStackTrace(); log.error(e.getMessage()); } return null; }
From source file:com.googlecode.pondskum.client.listener.DetailedConnectionListener.java
public void handleEvent(final DefaultHttpClient httpClient, final HttpResponse response) { logger.info("statusline -> " + response.getStatusLine()); logger.info("Headers -> " + Arrays.toString(response.getAllHeaders())); HttpEntity entity = response.getEntity(); if (entity != null) { try {// www. j av a 2 s . com dumpCookies(httpClient.getCookieStore().getCookies()); dumpContent(entity); } catch (Exception e) { logger.log(Level.SEVERE, "Couldn't write content due to an Exception. See below for details", e); } } }
From source file:dk.moerks.ratebeermobile.io.NetBroker.java
private static void signin(Context context, DefaultHttpClient httpclient) throws NetworkException, LoginException { HttpPost httppost = new HttpPost("http://www.ratebeer.com/signin/"); Log.d(LOGTAG, "Before Try"); try {/*w ww. ja v a2 s .co m*/ SharedPreferences settings = context.getSharedPreferences(Settings.PREFERENCETAG, 0); String username = settings.getString("rb_username", ""); String password = settings.getString("rb_password", ""); if (username != null && password != null && username.length() > 0 && password.length() > 0) { Log.d(LOGTAG, "Building Login Request"); // Add your data List<NameValuePair> parameters = new ArrayList<NameValuePair>(); parameters.add(new BasicNameValuePair("SaveInfo", "on")); parameters.add(new BasicNameValuePair("username", username)); parameters.add(new BasicNameValuePair("pwd", password)); httppost.setEntity(new UrlEncodedFormEntity(parameters)); //Check to see if we are already logged in List<Cookie> beforeCookies = httpclient.getCookieStore().getCookies(); for (Iterator<Cookie> iterator = beforeCookies.iterator(); iterator.hasNext();) { Cookie cookie = iterator.next(); if (cookie.getName().equalsIgnoreCase("SessionCode")) { return; } } Log.d(LOGTAG, "Executing Login Request"); // Execute HTTP Post Request HttpResponse result = httpclient.execute(httppost); int statusCode = result.getStatusLine().getStatusCode(); Log.d(LOGTAG, "Validating we got the right status from server: " + statusCode); if (statusCode == 200) { List<Cookie> cookies = httpclient.getCookieStore().getCookies(); Log.d(LOGTAG, "Start Validating Cookies. List length: " + cookies.size()); for (Iterator<Cookie> iterator = cookies.iterator(); iterator.hasNext();) { Cookie cookie = iterator.next(); Log.d(LOGTAG, "Matching Cookie: " + cookie.getName()); if (cookie.getName().equalsIgnoreCase("SessionCode")) { result.getEntity().consumeContent(); return; } } throw new LoginException(LOGTAG, "Login to ratebeer.com failed. Check your credentials"); } else { throw new LoginException(LOGTAG, "Login to ratebeer.com failed. Check your credentials"); } } else { throw new LoginException(LOGTAG, "Login to ratebeer.com failed. Check your credentials"); } } catch (ClientProtocolException e) { } catch (IOException e) { throw new NetworkException(context, LOGTAG, "Network Error - Do you have a network connection?", e); } }
From source file:com.mingsoft.util.proxy.Proxy.java
/** * get??/*from w ww.j a v a2s.c o m*/ * * @param url * ?<br> * @param fileUrl * ????<br> * @param header * ?Header new Header()<br> */ public static void getRandCode(String url, com.mingsoft.util.proxy.Header header, String fileUrl) { DefaultHttpClient client = new DefaultHttpClient(); //log.info("?" + url); // get HttpGet get = new HttpGet(url); // cookie?() get.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY); Map _headers = header.getHeaders(); // ? if (null != header && _headers.size() > 0) { get.setHeaders(Proxy.assemblyHeader(_headers)); } HttpResponse response; try { response = client.execute(get); // ? // Header[] h = (Header[]) response.getAllHeaders(); // for (int i = 0; i < h.length; i++) { // Header a = h[i]; // } HttpEntity entity = response.getEntity(); InputStream in = entity.getContent(); // cookie???cookie header.setCookie(assemblyCookie(client.getCookieStore().getCookies())); int temp = 0; // File file = new File(fileUrl); // ?? FileOutputStream out = new FileOutputStream(file); while ((temp = in.read()) != -1) { out.write(temp); } in.close(); out.close(); } catch (ClientProtocolException e) { e.printStackTrace(); log.error(e.getMessage()); } catch (IOException e) { e.printStackTrace(); log.error(e.getMessage()); } }
From source file:org.sonatype.nexus.testsuite.security.nexus4257.Nexus4257CookieVerificationIT.java
/** * Tests that an anonymous user with a stateless client does NOT receive a session cookie. *//*from w w w . ja v a 2 s . c om*/ @Test public void testCookieForStateFullClientForAnonUser() throws Exception { setAnonymousAccess(true); String url = this.getBaseNexusUrl() + "content/"; DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet getMethod = new HttpGet(url); assertThat(executeAndRelease(httpClient, getMethod, null), equalTo(200)); Cookie sessionCookie = this.getSessionCookie(httpClient.getCookieStore().getCookies()); assertThat("Session Cookie should not be set", sessionCookie, nullValue()); }
From source file:com.jelastic.JelasticService.java
public AuthenticationResponse authentication(String email, String password) { AuthenticationResponse authenticationResponse = null; try {/*from w ww.j av a 2 s . co m*/ List<NameValuePair> qparams = new ArrayList<NameValuePair>(); qparams.add(new BasicNameValuePair("login", email)); qparams.add(new BasicNameValuePair("password", password)); URI uri = URIUtils.createURI(getProtocol(), getApiHoster(), getPort(), getUrlAuthentication(), null, null); project.log("Authentication url : " + uri.toString(), Project.MSG_DEBUG); HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(new UrlEncodedFormEntity(qparams, "UTF-8")); ResponseHandler<String> responseHandler = new BasicResponseHandler(); DefaultHttpClient httpclient = getHttpClient(); String responseBody = httpclient.execute(httpPost, responseHandler); setCookieStore(httpclient.getCookieStore()); project.log("Authentication response : " + responseBody, Project.MSG_DEBUG); authenticationResponse = deserialize(responseBody, AuthenticationResponse.class); } catch (URISyntaxException | IOException e) { project.log(e.getMessage(), Project.MSG_ERR); } return authenticationResponse; }