List of usage examples for org.apache.http.impl.client DefaultHttpClient execute
public CloseableHttpResponse execute(final HttpUriRequest request) throws IOException, ClientProtocolException
From source file:core.HttpClient.java
/** * gets the checksum of the updated file * //from ww w. j av a2s . co m * @return String- the checksum */ public static String getUpdateFileChecksum() { DefaultHttpClient httpclient = new DefaultHttpClient(); try { HttpResponse response; HttpEntity entity; HttpPost httpost = new HttpPost(COMPANY_WEBSITE); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("checksum", "")); httpost.setEntity(new UrlEncodedFormEntity(nvps)); response = httpclient.execute(httpost); entity = response.getEntity(); String data = "", line; BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent())); while ((line = br.readLine()) != null) { data += line; } if (data.equals("")) { return null; } else { return data; } } catch (ClientProtocolException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } finally { httpclient.getConnectionManager().shutdown(); } }
From source file:Pusher.java
/** * Delivers a message to the Pusher API/*from www . j a v a 2 s. c om*/ * @param channel * @param event * @param jsonData * @param socketId * @return * @throws IOException * @throws ClientProtocolException */ public static String triggerPush(String channel, String event, String jsonData, String socketId) throws ClientProtocolException, IOException { //Build URI path String uriPath = buildURIPath(channel); //Build query String query = buildQuery(event, jsonData, socketId); //Generate signature String signature = buildAuthenticationSignature(uriPath, query); //Build URI String url = buildURI(uriPath, query, signature); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpContext cntxt = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Content-Type", "application/json"); httpPost.setEntity(new StringEntity(jsonData)); org.apache.http.HttpResponse httpResponse = httpClient.execute(httpPost); //Start request try { return EntityUtils.toString(httpResponse.getEntity()); } catch (IOException e) { //Log warning return null; } }
From source file:core.HttpClient.java
/** * Checks if we have the latest version/* ww w .java2s.com*/ * * @param version * @return boolean - true if version is not up to date */ public static boolean isUpdateRequired(String version) { DefaultHttpClient httpclient = new DefaultHttpClient(); try { HttpResponse response; HttpEntity entity; HttpPost httpost = new HttpPost(COMPANY_WEBSITE); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("version", version)); httpost.setEntity(new UrlEncodedFormEntity(nvps)); response = httpclient.execute(httpost); entity = response.getEntity(); String data = "", line; BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent())); while ((line = br.readLine()) != null) { data += line; } if (data.startsWith("true")) { return true; } else { return false; } } catch (ClientProtocolException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } finally { httpclient.getConnectionManager().shutdown(); } }
From source file:info.smartkit.hairy_batman.demo.MoniterWechatBrowser.java
/** * ?URLhtml?//www.j a v a 2s.c o m */ @SuppressWarnings("deprecation") public static String getHttpClientHtml(String url, String code, String userAgent) { String html = null; @SuppressWarnings("deprecation") DefaultHttpClient httpClient = new DefaultHttpClient();// httpClient HttpGet httpget = new HttpGet(url);// get?URL // Pause for 4 seconds try { Thread.sleep(5000); } catch (InterruptedException e1) { e1.printStackTrace(); System.out.println(e1.toString()); } // httpget.setHeader("User-Agent", userAgent); try { // responce HttpResponse responce = httpClient.execute(httpget); // ? int returnCode = responce.getStatusLine().getStatusCode(); // 200? ? if (returnCode == HttpStatus.SC_OK) { // HttpEntity entity = responce.getEntity(); if (entity != null) { html = new String(EntityUtils.toString(entity));// html?? } } } catch (Exception e) { System.out.println(""); e.printStackTrace(); } finally { httpClient.getConnectionManager().shutdown(); } return html; }
From source file:dk.moerks.ratebeermobile.io.NetBroker.java
public static Drawable doGetImage(Context context, DefaultHttpClient httpclient, String url) throws NetworkException { if (httpclient == null) { httpclient = init();/*from ww w . j a va 2 s . c o m*/ } Log.d(LOGTAG, "URL: " + url); HttpGet httpget = new HttpGet(url); try { // Execute HTTP Post Request HttpResponse response = httpclient.execute(httpget); Drawable result = responseDrawable(response); response.getEntity().consumeContent(); return result; } catch (ClientProtocolException e) { } catch (IOException e) { } catch (Exception e) { throw new NetworkException(context, LOGTAG, "Network Error - Do you have a network connection?", e); } return null; }
From source file:be.benvd.mvforandroid.data.MVDataHelper.java
/** * Returns the GET response of the given url. * // w w w. j a v a 2 s.co m * @throws IOException * @return The response of the given URL. If no response was found, null is * returned. */ public static String getResponse(String username, String password, String url) throws IOException { /* DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials(username + ":" + password)); HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(httpget); */ DefaultHttpClient httpclient = new DefaultHttpClient(); Credentials creds = new UsernamePasswordCredentials(username, password); httpclient.getCredentialsProvider().setCredentials(new AuthScope(null, -1), creds); String auth = android.util.Base64.encodeToString((username + ":" + password).getBytes("UTF-8"), android.util.Base64.NO_WRAP); HttpGet httpget = new HttpGet(url); httpget.addHeader("Authorization", "Basic " + auth); HttpResponse response = httpclient.execute(httpget); if (response.getEntity() != null) { BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line); } reader.close(); Log.v(MVDataHelper.class.getSimpleName(), "Response:" + sb.toString()); return sb.toString(); } return null; }
From source file:cn.loveapple.client.android.util.ApiUtil.java
public static String getHttpBody(String url, PackageManager packageManager) { if (StringUtils.isEmpty(url)) { return null; }/*www .j a v a 2 s.c o m*/ String body = null; DefaultHttpClient httpClient = new DefaultHttpClient(); HttpParams params = httpClient.getParams(); // SYSTEM INFO params.setParameter("", ""); params.setParameter("", ""); params.setParameter("", ""); HttpConnectionParams.setConnectionTimeout(params, 1000); HttpConnectionParams.setSoTimeout(params, 1000); HttpPost httpRequest = new HttpPost(url); HttpResponse httpResponse = null; try { httpResponse = httpClient.execute(httpRequest); } catch (Exception e) { Log.e(LOG_TAG, "http response execute failed.", e); return null; } if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity httpEntity = httpResponse.getEntity(); try { body = EntityUtils.toString(httpEntity); } catch (Exception e) { Log.e(LOG_TAG, "get http response body failed.", e); return null; } finally { try { httpEntity.consumeContent(); } catch (IOException e) { } } } httpClient.getConnectionManager().shutdown(); return body; }
From source file:org.sharetask.data.IntegrationTest.java
@BeforeClass public static void login() throws Exception { final DefaultHttpClient client = new DefaultHttpClient(); final HttpPost httpPost = new HttpPost(BASE_URL + "/user/login"); httpPost.addHeader(new BasicHeader("Content-Type", "application/json")); final StringEntity httpEntity = new StringEntity( "{\"username\":\"dev1@shareta.sk\"," + "\"password\":\"password\"}"); System.out.println(EntityUtils.toString(httpEntity)); httpPost.setEntity(httpEntity);//from w w w. ja va 2 s . c o m //when final HttpResponse response = client.execute(httpPost); //then Assert.assertEquals(HttpStatus.OK.value(), response.getStatusLine().getStatusCode()); client.getCookieStore().getCookies(); for (final Cookie cookie : client.getCookieStore().getCookies()) { if (cookie.getName().equals("JSESSIONID")) { DOMAIN = cookie.getDomain(); SESSIONID = cookie.getValue(); } } }
From source file:com.dajodi.scandic.ScandicSessionHelper.java
private static void login(String username, String password) throws Exception { DefaultHttpClient client = Singleton.INSTANCE.getHttpClient(); client.getCookieStore().clear();//from w ww. j av a 2 s. c o m HttpHead head = new HttpHead("https://www.scandichotels.com/Frequent-Guest-Programme/"); // only for user-agent Util.gzipify(head); HttpResponse response = client.execute(head); if (response.getStatusLine().getStatusCode() != 200) { throw new ScandicHtmlException("HEAD request to FG page did not return a 200, instead " + response.getStatusLine().getStatusCode()); } head.abort(); boolean found = false; // assume this cookie exists for (Cookie cookie : client.getCookieStore().getCookies()) { if (SESSION_ID_COOKIE_NAME.equals(cookie.getName())) { found = true; break; } } if (!found) { throw new ScandicHtmlException("Session id cookie not valid from head request, dying"); } List<NameValuePair> nvps = new LinkedList<NameValuePair>(); nvps.add(new BasicNameValuePair("ctl00$MenuLoginStatus$txtLoyaltyUsername", username)); nvps.add(new BasicNameValuePair("ctl00$MenuLoginStatus$txtLoyaltyPassword", password)); nvps.add(new BasicNameValuePair("ctl00$MenuLoginStatus$loginPopUpID", "LOGIN_POPUP_MODULE")); nvps.add(new BasicNameValuePair("ctl00$MenuLoginStatus$loginPopUpPageID", "LOGIN_POPUP_MODULE")); nvps.add(new BasicNameValuePair("__PREVIOUSPAGE", "")); nvps.add(new BasicNameValuePair("__EVENTTARGET", "ctl00$MenuLoginStatus$btnLogIn")); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvps); // now the post HttpPost post = new HttpPost("https://www.scandichotels.com/templates/Booking/Units/LoginValidator.aspx"); post.setHeader("Content-Type", "application/x-www-form-urlencoded"); // needed, we don't want redirecting here final HttpParams params = new BasicHttpParams(); HttpClientParams.setRedirecting(params, false); post.setParams(params); // not really needed, but why not Util.gzipify(post); post.setEntity(entity); response = client.execute(post); post.abort(); if (isLoggedIn()) { Log.d("Success! Logged in via Java code!"); } else if (response.getStatusLine().getStatusCode() == 302 && response.getFirstHeader("Location").getValue().contains("Login-Error")) { throw new InvalidLoginException(); } else { throw new RuntimeException("Could not login!"); } }
From source file:lib.pusher.Pusher.java
/** * Delivers a message to the Pusher API// w ww . ja v a2 s . co m * @param channel * @param event * @param jsonData * @param socketId * @return * @throws IOException * @throws ClientProtocolException */ public static String triggerPush(String channel, String event, String jsonData, String socketId) throws ClientProtocolException, IOException { //Build URI path String uriPath = buildURIPath(channel); //Build query String query = buildQuery(event, jsonData, socketId); //Generate signature String signature = buildAuthenticationSignature(uriPath, query); //Build URI String url = buildURI(uriPath, query, signature); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpContext cntxt = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Content-Type", "application/json"); httpPost.setEntity(new StringEntity(jsonData, "UTF-8")); org.apache.http.HttpResponse httpResponse = httpClient.execute(httpPost); //Start request try { return EntityUtils.toString(httpResponse.getEntity()); } catch (IOException e) { return null; } }