Java Code Examples for org.apache.http.impl.client.DefaultHttpClient

The following examples show how to use org.apache.http.impl.client.DefaultHttpClient. These examples are extracted from open source projects.
Example 1
Project: zstack   File: InventoryIndexManagerImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean start() {
    try {
        httpClient = new DefaultHttpClient(new PoolingClientConnectionManager());
        bulkUri = makeURI(elasticSearchBaseUrl, "_bulk");

        /* only for debugging */
        if (deleteAllIndexWhenStart) {
            try {
                deleteIndex(null);
            } catch (Exception ex) {
                logger.warn(String.format("Failed to delete all index"), ex);
            }
        }

        populateExtensions();
        populateTriggerVOs();
        populateInventoryIndexer();
        dumpInventoryIndexer();
        createIndexIfNotExists();
        bus.registerService(this);
    } catch (Exception e) {
        throw new CloudRuntimeException(e);
    }
    return true;
}
 
Example 2
Project: selenium-grid2-api   File: GridInfoExtractor.java    License: Apache License 2.0 6 votes vote down vote up
public static GridInfo getHostNameAndPort(String hubHost, int hubPort, SessionId session) {

    GridInfo retVal = null;

    try {
      HttpHost host = new HttpHost(hubHost, hubPort);
      DefaultHttpClient client = new DefaultHttpClient();
      URL sessionURL = new URL("http://" + hubHost + ":" + hubPort + "/grid/api/testsession?session=" + session);
      BasicHttpEntityEnclosingRequest basicHttpEntityEnclosingRequest = new BasicHttpEntityEnclosingRequest("POST", sessionURL.toExternalForm());
      HttpResponse response = client.execute(host, basicHttpEntityEnclosingRequest);
      if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        JSONObject object = extractObject(response);
        retVal = new GridInfo(object);
      } else {
        System.out.println("Problem connecting to Grid Server");
      }
    } catch (JSONException | IOException  e) {
      throw new RuntimeException("Failed to acquire remote webdriver node and port info", e);
    }
    return retVal;
  }
 
Example 3
Project: sakai   File: BitlyUrlService.java    License: Educational Community License v2.0 6 votes vote down vote up
/**
 * Makes a GET request to the given address. Any query string should be appended already.
 * @param address	the fully qualified URL to make the request to
 * @return
 */
private String doGet(String address){
	try {
		
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(address);
		HttpResponse response = httpclient.execute(httpget);
		
		//check reponse code
		StatusLine status = response.getStatusLine();
		if(status.getStatusCode() != 200) {
			log.error("Error shortening URL. Status: " + status.getStatusCode() + ", reason: " + status.getReasonPhrase());
			return null;
		}
		
		HttpEntity entity = response.getEntity();
		if (entity != null) {
		    return EntityUtils.toString(entity);
		}
		
	} catch (Exception e) {
		log.error(e.getClass() + ":" + e.getMessage());
	} 
	return null;
}
 
Example 4
Project: micro-integrator   File: SPARQLServiceTestCase.java    License: Apache License 2.0 6 votes vote down vote up
private boolean isExternalEndpointAvailable() throws IOException {
    HttpClient httpClient = new DefaultHttpClient();
    String url = "http://semantic.eea.europa.eu/sparql?query=";
    String query = "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>\n"
            + "PREFIX cr:<http://cr.eionet.europa.eu/ontologies/contreg.rdf#>\n"
            + "SELECT * WHERE {  ?bookmark a cr:SparqlBookmark;rdfs:label ?label} LIMIT 50";
    url = url + URLEncoder.encode(query, "UTF-8");
    HttpGet httpGet = new HttpGet(url);
    httpClient.getParams().setParameter("http.socket.timeout", 300000);
    httpGet.setHeader("Accept", "text/xml");
    HttpResponse httpResponse = httpClient.execute(httpGet);
    if (httpResponse.getStatusLine().getStatusCode() == 200) {
        return true;
    }
    return false;
}
 
Example 5
Project: java-client-api   File: TestServerBootstrapper.java    License: Apache License 2.0 6 votes vote down vote up
private void invokeBootstrapExtension() throws ClientProtocolException, IOException {
  final String params = Common.BALANCED ? "?rs%3Abalanced=true" : "";

  DefaultHttpClient client = new DefaultHttpClient();

  client.getCredentialsProvider().setCredentials(
    new AuthScope(host, port),
    new UsernamePasswordCredentials(username, password));

  HttpPost post = new HttpPost("http://" + host + ":" + port
    + "/v1/resources/bootstrap" + params);

  HttpResponse response = client.execute(post);
  @SuppressWarnings("unused")
  HttpEntity entity = response.getEntity();
  System.out.println("Invoked bootstrap extension.  Response is "
    + response.toString());
}
 
Example 6
Project: karyon   File: WebAdminTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testRestEndPoints() throws Exception {
    HttpClient client = new DefaultHttpClient();
    for (Map.Entry<String, String> restEndPoint : REST_END_POINTS.entrySet()) {
        final String endPoint = restEndPoint.getKey();
        LOG.info("REST endpoint " + endPoint);
        HttpGet restGet = new HttpGet(endPoint);
        HttpResponse response = client.execute(restGet);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals(restEndPoint.getValue(), response.getEntity().getContentType().getValue());

        // need to consume full response before make another rest call with
        // the default SingleClientConnManager used with DefaultHttpClient
        EntityUtils.consume(response.getEntity());
    }
}
 
Example 7
Project: goprohero   File: OCTurnOff.java    License: MIT License 6 votes vote down vote up
public static String GET(String url){
    InputStream inputStream = null;
    String result = "";
    try {

        // create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // make GET request to the given URL
        HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

        // receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        // convert inputstream to string
        if(inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    return result;
}
 
Example 8
Project: java-client-api   File: WBFailover.java    License: Apache License 2.0 6 votes vote down vote up
private boolean isRunning(String host) {
	try {

		DefaultHttpClient client = new DefaultHttpClient();
		client.getCredentialsProvider().setCredentials(new AuthScope(host, 7997),
				new UsernamePasswordCredentials("admin", "admin"));

		HttpGet get = new HttpGet("http://" + host + ":7997?format=json");
		HttpResponse response = client.execute(get);
		ResponseHandler<String> handler = new BasicResponseHandler();
		String body = handler.handleResponse(response);
		if (body.contains("Healthy")) {
			return true;
		}

	} catch (Exception e) {
		return false;
	}
	return false;
}
 
Example 9
Project: MiBandDecompiled   File: HttpAndroidClientFactory.java    License: Apache License 2.0 6 votes vote down vote up
public HttpClient createHttpClient(ClientConfiguration clientconfiguration)
{
    BasicHttpParams basichttpparams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(basichttpparams, clientconfiguration.getConnectionTimeout());
    HttpConnectionParams.setSoTimeout(basichttpparams, clientconfiguration.getSocketTimeout());
    HttpConnectionParams.setStaleCheckingEnabled(basichttpparams, true);
    HttpConnectionParams.setTcpNoDelay(basichttpparams, true);
    int i = clientconfiguration.getSocketBufferSizeHints()[0];
    int j = clientconfiguration.getSocketBufferSizeHints()[1];
    if (i > 0 || j > 0)
    {
        HttpConnectionParams.setSocketBufferSize(basichttpparams, Math.max(i, j));
    }
    SchemeRegistry schemeregistry = new SchemeRegistry();
    schemeregistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    if (clientconfiguration.getProtocol() == Protocol.HTTPS)
    {
        schemeregistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    }
    return new DefaultHttpClient(new SingleClientConnManager(basichttpparams, schemeregistry), basichttpparams);
}
 
Example 10
Project: Android-Material-Icons   File: HttpWrapper.java    License: Apache License 2.0 6 votes vote down vote up
static String makeRequest(String uri) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;
    String responseString = null;
    try {
        response = httpclient.execute(new HttpGet(uri));
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            responseString = out.toString();
            out.close();
        } else {
            //Close the connection.
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return responseString;
}
 
Example 11
Project: goprohero   File: StopActivity.java    License: MIT License 6 votes vote down vote up
public static String GET(String url){
    InputStream inputStream = null;
    String result = "";
    try {

        // create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // make GET request to the given URL
        HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

        // receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        // convert inputstream to string
        if(inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    return result;
}
 
Example 12
Project: Popeens-DSub   File: UpdateApp.java    License: GNU General Public License v3.0 6 votes vote down vote up
public String readJson(String url) {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        }
    } catch(Exception e) {
        e.printStackTrace();
    }
    return builder.toString();
}
 
Example 13
Project: product-ei   File: ESBJAVA5103CorrelateOnExpressionTestCase.java    License: Apache License 2.0 6 votes vote down vote up
@Test(groups = "wso2.esb", description = "Test CorrelateOn in Aggregate mediator ")
public void testAggregateWithCorrelateExpression() throws IOException{
    String expectedOutput1 = "<result><value>value1</value><value>value2</value></result>";
    String expectedOutput2 = "<result><value>value2</value><value>value1</value></result>";

    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(getApiInvocationURL("testAggregate"));

    try {
        HttpResponse httpResponse = httpclient.execute(httpget);
        HttpEntity entity = httpResponse.getEntity();
        BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
        String result = "";
        String line;
        while ((line = rd.readLine()) != null) {
            result += line;
        }
        Assert.assertTrue(expectedOutput1.equals(result) || expectedOutput2.equals(result), "Aggregated response is not correct.");
    }
    finally {
        httpclient.clearRequestInterceptors();
    }
}
 
Example 14
Project: BigApp_Discuz_Android   File: HttpClientUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static String httpPostWithWCF(String serverUrl, String method,
                                     JSONObject params) {
    String responseStr = null;
    try {
        String url = serverUrl + method;
        DefaultHttpClient client = new DefaultHttpClient();

        HttpPost request = new HttpPost(url);

        request.setEntity(new StringEntity(params.toString(), CODE));
        request.setHeader(HTTP.CONTENT_TYPE, "text/json");

        HttpResponse response = client.execute(request);

        responseStr = EntityUtils.toString(response.getEntity(), CODE);
        // System.out.println("responseStr:" + responseStr);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return responseStr;
}
 
Example 15
Project: redalert-android   File: HTTP.java    License: Apache License 2.0 6 votes vote down vote up
public static String get(String URL) throws Exception {
    // Get custom http client
    DefaultHttpClient client = getHTTPClient();

    // Create GET request
    HttpGet request = new HttpGet(URL);

    // Execute the request
    HttpResponse response = client.execute(request, new BasicHttpContext());

    // Return response as string
    String responseText = EntityUtils.toString(response.getEntity());

    // Failed?
    if (response.getStatusLine() == null || response.getStatusLine().getStatusCode() != 200) {
        // Throw it out
        throw new Exception(response.getStatusLine().toString() + "\n" + responseText);
    }

    // We're good
    return responseText;
}
 
Example 16
Project: Criteria2Query   File: ATLASUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static String getConcept(JSONObject jjj)
		throws UnsupportedEncodingException, IOException, ClientProtocolException {
	HttpPost httppost = new HttpPost(vocubularyurl);
	// httppost.setHeader("X-GWT-Permutation",
	// "3DE824138FE65400740EC1816A73CACC");
	httppost.setHeader("Content-Type", "application/json");
	StringEntity se = new StringEntity(jjj.toString());
	httppost.setEntity(se);
	startTime = System.currentTimeMillis();
	HttpResponse httpresponse = new DefaultHttpClient().execute(httppost);
	endTime = System.currentTimeMillis();
	System.out.println("statusCode:" + httpresponse.getStatusLine().getStatusCode());
	System.out.println("Call API time (unit:millisecond):" + (endTime - startTime));

	if (httpresponse.getStatusLine().getStatusCode() == 200) {
		// System.out.println("succeed!");
		String strResult = EntityUtils.toString(httpresponse.getEntity());
		return strResult;
		// httppost.
	} else {
		return null;
	}
}
 
Example 17
Project: hadoop-connectors   File: HttpTransportFactory.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Create an {@link ApacheHttpTransport} for calling Google APIs with an optional HTTP proxy.
 *
 * @param proxyUri Optional HTTP proxy URI to use with the transport.
 * @param proxyCredentials Optional HTTP proxy credentials to authenticate with the transport
 *     proxy.
 * @return The resulting HttpTransport.
 * @throws IOException If there is an issue connecting to Google's certification server.
 * @throws GeneralSecurityException If there is a security issue with the keystore.
 */
public static ApacheHttpTransport createApacheHttpTransport(
    @Nullable URI proxyUri, @Nullable Credentials proxyCredentials)
    throws IOException, GeneralSecurityException {
  checkArgument(
      proxyUri != null || proxyCredentials == null,
      "if proxyUri is null than proxyCredentials should be null too");

  ApacheHttpTransport transport =
      new ApacheHttpTransport.Builder()
          .trustCertificates(GoogleUtils.getCertificateTrustStore())
          .setProxy(
              proxyUri == null ? null : new HttpHost(proxyUri.getHost(), proxyUri.getPort()))
          .build();

  if (proxyCredentials != null) {
    ((DefaultHttpClient) transport.getHttpClient())
        .getCredentialsProvider()
        .setCredentials(new AuthScope(proxyUri.getHost(), proxyUri.getPort()), proxyCredentials);
  }

  return transport;
}
 
Example 18
Project: buddycloud-android   File: BuddycloudHTTPHelper.java    License: Apache License 2.0 6 votes vote down vote up
public static void reqArrayNoSSL(String url, Context parent,
		ModelCallback<JSONArray> callback) {
	RequestAsyncTask<JSONArray> task = new RequestAsyncTask<JSONArray>(
			"get", url, null, false, false, parent, callback) {
		@Override
		protected JSONArray toJSON(String responseStr) throws JSONException {
			return new JSONArray(responseStr);
		}
	};
	task.client = new DefaultHttpClient();
	task.client.getParams().setParameter(
			ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
	task.headers = new HashMap<String, String>();
	task.headers.put("User-Agent", BROWSER_LIKE_USER_AGENT);
	executeOnExecutor(task, HIGH_PRIORITY_EXECUTOR);
}
 
Example 19
Project: speech-android-sdk   File: MainActivity.java    License: Apache License 2.0 6 votes vote down vote up
public String getToken() {

            Log.d(TAG, "attempting to get a token from: " + m_strTokenFactoryURL);
            try {
                // DISCLAIMER: the application developer should implement an authentication mechanism from the mobile app to the
                // server side app so the token factory in the server only provides tokens to authenticated clients
                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(m_strTokenFactoryURL);
                HttpResponse executed = httpClient.execute(httpGet);
                InputStream is = executed.getEntity().getContent();
                StringWriter writer = new StringWriter();
                IOUtils.copy(is, writer, "UTF-8");
                String strToken = writer.toString();
                Log.d(TAG, strToken);
                return strToken;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
 
Example 20
Project: sync-service   File: SwiftManager.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void deleteWorkspace(Workspace workspace) throws Exception {

    if (!isTokenActive()) {
        login();
    }

    HttpClient httpClient = new DefaultHttpClient();

    String url = this.storageUrl + "/" + workspace.getSwiftContainer();

    try {

        HttpDelete request = new HttpDelete(url);
        request.setHeader(SwiftResponse.X_AUTH_TOKEN, authToken);

        HttpResponse response = httpClient.execute(request);

        SwiftResponse swiftResponse = new SwiftResponse(response);

        if (swiftResponse.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
            throw new UnauthorizedException("401 User unauthorized");
        }

        if (swiftResponse.getStatusCode() < 200 || swiftResponse.getStatusCode() >= 300) {
            throw new UnexpectedStatusCodeException("Unexpected status code: " + swiftResponse.getStatusCode());
        }

    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}
 
Example 21
Project: basiclti-util-java   File: IMSPOXRequest.java    License: Apache License 2.0 5 votes vote down vote up
public static void sendReplaceResult(String url, String key, String secret, String sourcedid, String score, String resultData, Boolean isUrl) throws IOException, OAuthException, GeneralSecurityException {
	HttpPost request = buildReplaceResult(url, key, secret, sourcedid, score, resultData, isUrl);
	DefaultHttpClient client = new DefaultHttpClient();
	HttpResponse response = client.execute(request);
	if (response.getStatusLine().getStatusCode() >= 400) {
		throw new HttpResponseException(response.getStatusLine().getStatusCode(),
				response.getStatusLine().getReasonPhrase());
	}
}
 
Example 22
Project: expper   File: MailService.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void sendViaSendcloudApi(EmailParams emailParams) throws IOException {
    String url = jHipsterProperties.getSendcloud().getUrl();
    String key = jHipsterProperties.getSendcloud().getKey();
    String user = jHipsterProperties.getSendcloud().getUser();
    String from = jHipsterProperties.getSendcloud().getFrom();
    String fromName = jHipsterProperties.getSendcloud().getFromName();

    List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("api_user", user));
    params.add(new BasicNameValuePair("api_key", key));
    params.add(new BasicNameValuePair("to", emailParams.getTo()));
    params.add(new BasicNameValuePair("from", from));
    params.add(new BasicNameValuePair("fromname", fromName));
    params.add(new BasicNameValuePair("subject", emailParams.getSubject()));
    params.add(new BasicNameValuePair("html", emailParams.getContent()));
    params.add(new BasicNameValuePair("resp_email_id", "true"));

    HttpPost httpost = new HttpPost(url);
    HttpClient httpclient = new DefaultHttpClient();

    httpost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

    HttpResponse response = httpclient.execute(httpost);

    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        log.debug("Send email via Sendcloud successfully. response: " + EntityUtils.toString(response.getEntity()));
    } else {
        log.error("Send email via Sendcloud failed.");
    }

    httpost.releaseConnection();
}
 
Example 23
Project: neembuu-uploader   File: BoxUploaderPlugin.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static void loginBox() throws Exception {
        HttpParams params = new BasicHttpParams();
        params.setParameter(
                "http.useragent",
                "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6");
        DefaultHttpClient httpclient = new DefaultHttpClient(params);
//https://www.box.net/api/1.0/auth/jtear5a2djtfxs9598apynmea62vko5d
        System.out.println("Trying to log in to box.com");
        HttpPost httppost = new HttpPost("https://www.box.net/api/1.0/auth/" + ticket);
        httppost.setHeader("Cookie", zcookie + ";" + visitorcookie);
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
//        formparams.add(new BasicNameValuePair("action", "login"));
        formparams.add(new BasicNameValuePair("login", "[email protected]"));
        formparams.add(new BasicNameValuePair("password", ""));
        formparams.add(new BasicNameValuePair("__login", "1"));
        formparams.add(new BasicNameValuePair("dologin", "1"));
        formparams.add(new BasicNameValuePair("reg_step", ""));
        formparams.add(new BasicNameValuePair("submit1", "1"));
        formparams.add(new BasicNameValuePair("folder", ""));
        formparams.add(new BasicNameValuePair("skip_framework_login", "1"));
        formparams.add(new BasicNameValuePair("login_or_register_mode", "login"));
        formparams.add(new BasicNameValuePair("new_login_or_register_mode", ""));
        formparams.add(new BasicNameValuePair("request_token", request_token));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
        httppost.setEntity(entity);
        HttpResponse httpresponse = httpclient.execute(httppost);
        System.out.println("Gonna print the response");
        loginresponse = EntityUtils.toString(httpresponse.getEntity());
        if (loginresponse.contains("Invalid username or password")) {
            System.out.println("DropBox login failed");
        } else {
            System.out.println("DropbBox login successful :)");
        }

    }
 
Example 24
Project: java-client-api   File: JavaApiBatchSuite.java    License: Apache License 2.0 5 votes vote down vote up
public static void createRESTAppServer(String restServerName, int restPort) {
  try {
    DefaultHttpClient client = new DefaultHttpClient();

    client.getCredentialsProvider().setCredentials(
        new AuthScope("localhost", 8002),
        new UsernamePasswordCredentials("admin", "admin"));
    HttpPost post = new HttpPost("http://localhost:8002" + "/v1/rest-apis?format=json");
    String JSONString =
        "{ \"rest-api\": {\"name\":\"" +
            restServerName +
            "\",\"port\":\"" +
            restPort +
            "\"}}";
    // System.out.println(JSONString);
    post.addHeader("Content-type", "application/json");
    post.setEntity(new StringEntity(JSONString));

    HttpResponse response = client.execute(post);
    HttpEntity respEntity = response.getEntity();

    if (respEntity != null) {
      // EntityUtils to get the response content
      String content = EntityUtils.toString(respEntity);
      System.out.println(content);
    }
  } catch (Exception e) {
    // writing error to Log
    e.printStackTrace();
  }
}
 
Example 25
Project: hbc   File: RestartableHttpClient.java    License: Apache License 2.0 5 votes vote down vote up
public void setup() {
  DefaultHttpClient defaultClient = new DefaultHttpClient(new PoolingClientConnectionManager(schemeRegistry), params);

  auth.setupConnection(defaultClient);

  if (enableGZip) {
    underlying.set(new DecompressingHttpClient(defaultClient));
  } else {
    underlying.set(defaultClient);
  }
}
 
Example 26
Project: endpoints-codelab-android   File: Util.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static InputStream getInputStreamFromUrl(String url)
        throws ClientProtocolException, IOException {
    HttpGet request = new HttpGet(url);
    DefaultHttpClient client = new DefaultHttpClient(getTimeoutHttpParams());
    HttpResponse response = client.execute(request);
    int statusCode = response.getStatusLine().getStatusCode();

    if (statusCode != 200) {
        Log.e(TAG, "Failed to get stream for: " + url);
        throw new IOException("Failed to get stream for: " + url);
    }

    return response.getEntity().getContent();
}
 
Example 27
Project: kylin   File: RestService.java    License: Apache License 2.0 5 votes vote down vote up
private HttpClient getHttpClient(int connectionTimeout, int readTimeout) {
    final HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setSoTimeout(httpParams, readTimeout);
    HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeout);

    return new DefaultHttpClient(httpParams);
}
 
Example 28
Project: pushfish-android   File: HttpClientConfigurer.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void configureCredentials(DefaultHttpClient httpClient, PasswordCredentials credentials) {
    String username = credentials.getUsername();
    if (username != null && username.length() > 0) {
        useCredentials(httpClient, credentials, AuthScope.ANY_HOST, AuthScope.ANY_PORT);

        // Use preemptive authorisation if no other authorisation has been established
        httpClient.addRequestInterceptor(new PreemptiveAuth(new BasicScheme()), 0);
    }
}
 
Example 29
Project: product-ei   File: BPMNTestUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns HTTP GET response from given url using admin credentials
 *
 * @param url request url suffix
 * @return
 * @throws IOException
 */
public static HttpResponse getRequestResponse(String url) throws IOException {

    String restUrl = getRestEndPoint(url);
    log.info("Sending HTTP GET request: " + restUrl);
    client = new DefaultHttpClient();
    request = new HttpGet(restUrl);

    request.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials("admin", "admin"),
            Charset.defaultCharset().toString(), false));
    client.getConnectionManager().closeExpiredConnections();
    HttpResponse response = client.execute(request);
    return response;
}
 
Example 30
Project: micro-integrator   File: ODataTenantUserTestCase.java    License: Apache License 2.0 5 votes vote down vote up
private static int sendDELETE(String endpoint, String acceptType) throws IOException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpDelete httpDelete = new HttpDelete(endpoint);
    httpDelete.setHeader("Accept", acceptType);
    HttpResponse httpResponse = httpClient.execute(httpDelete);
    return httpResponse.getStatusLine().getStatusCode();
}
 
Example 31
Project: karyon   File: HealthCheckResourceTest.java    License: Apache License 2.0 5 votes vote down vote up
private void checkHealth(HealthCheckHandler healthCheckHandler, int respStatus) throws Exception {
    final AdminResourcesContainer adminResourcesContainer = buildAdminResourcesContainer(healthCheckHandler);
    adminResourcesContainer.init();
    final int adminPort = adminResourcesContainer.getServerPort();

    HttpClient client = new DefaultHttpClient();
    HttpGet healthGet =
            new HttpGet(String.format("http://localhost:%d/jr/healthcheck", adminPort));
    HttpResponse response = client.execute(healthGet);
    assertEquals("admin resource health check resource failed.", respStatus, response.getStatusLine().getStatusCode());

    adminResourcesContainer.shutdown();
}
 
Example 32
Project: pushfish-android   File: HttpClientConfigurer.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void useCredentials(DefaultHttpClient httpClient, PasswordCredentials credentials, String host, int port) {
    Credentials basicCredentials = new UsernamePasswordCredentials(credentials.getUsername(), credentials.getPassword());
    httpClient.getCredentialsProvider().setCredentials(new AuthScope(host, port), basicCredentials);

    NTLMCredentials ntlmCredentials = new NTLMCredentials(credentials);
    Credentials ntCredentials = new NTCredentials(ntlmCredentials.getUsername(), ntlmCredentials.getPassword(), ntlmCredentials.getWorkstation(), ntlmCredentials.getDomain());
    httpClient.getCredentialsProvider().setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM, AuthPolicy.NTLM), ntCredentials);

    LOGGER.debug("Using {} and {} for authenticating against '{}:{}'", new Object[]{credentials, ntlmCredentials, host, port});
}
 
Example 33
Project: micro-integrator   File: ESBJAVA3676EnrichSourcePropNoClone.java    License: Apache License 2.0 5 votes vote down vote up
@Test(groups = "wso2.esb", description = "Tests Enrich Source OM Property without clone")
public void testEnrichSourceTypePropertyAndCloneFalse() throws Exception {

    String endpoint = getProxyServiceURLHttp(PROXY_NAME);

    String requestXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
            + "   <soapenv:Header/>\n" + "   <soapenv:Body>\n" + "      <payload>\n" + "         <content>\n"
            + "            <abc>\n" + "               <def>123</def>\n" + "            </abc>\n"
            + "         </content>\n" + "      </payload>\n" + "   </soapenv:Body>\n" + "</soapenv:Envelope>";

    DefaultHttpClient httpclient = new DefaultHttpClient();

    HttpPost httpPost = new HttpPost(endpoint);
    httpPost.addHeader("SOAPAction", "\"urn:mediate\"");
    httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "text/xml;charset=UTF-8");
    HttpEntity stringEntity = new StringEntity(requestXml, CharEncoding.UTF_8);
    httpPost.setEntity(stringEntity);
    HttpResponse response = httpclient.execute(httpPost);
    HttpEntity resEntity = response.getEntity();

    BufferedReader rd = new BufferedReader(new InputStreamReader(resEntity.getContent()));
    String result = "";
    String line;
    while ((line = rd.readLine()) != null) {
        result += line;
    }
    Assert.assertTrue(!result.contains("payload"));
}
 
Example 34
Project: container   File: HttpServiceImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public HttpResponse Put(final String uri, final HttpEntity httpEntity, final String username,
                        final String password) throws IOException {
    DefaultHttpClient client = new DefaultHttpClient();
    client.setRedirectStrategy(new LaxRedirectStrategy());
    client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
    final HttpPut put = new HttpPut(uri);
    put.setEntity(httpEntity);
    final HttpResponse response = client.execute(put);
    return response;
}
 
Example 35
Project: pushfish-android   File: HttpClientConfigurer.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void configureRetryHandler(DefaultHttpClient httpClient) {
    httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
            return false;
        }
    });
}
 
Example 36
Project: hbase   File: Client.java    License: Apache License 2.0 5 votes vote down vote up
private void initialize(Cluster cluster, boolean sslEnabled) {
  this.cluster = cluster;
  this.sslEnabled = sslEnabled;
  extraHeaders = new ConcurrentHashMap<>();
  String clspath = System.getProperty("java.class.path");
  LOG.debug("classpath " + clspath);
  this.httpClient = new DefaultHttpClient();
  this.httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 2000);
}
 
Example 37
Project: camunda-bpm-platform   File: AbstractWebIT.java    License: Apache License 2.0 5 votes vote down vote up
public void createClient(String ctxPath) throws Exception {
  testProperties = new TestProperties();

  APP_BASE_PATH = testProperties.getApplicationPath("/" + ctxPath);
  LOGGER.info("Connecting to application "+APP_BASE_PATH);

  ClientConfig clientConfig = new DefaultApacheHttpClient4Config();
  clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
  client = ApacheHttpClient4.create(clientConfig);

  defaultHttpClient = (DefaultHttpClient) client.getClientHandler().getHttpClient();
  HttpParams params = defaultHttpClient.getParams();
  HttpConnectionParams.setConnectionTimeout(params, 3 * 60 * 1000);
  HttpConnectionParams.setSoTimeout(params, 10 * 60 * 1000);
}
 
Example 38
Project: WayHoo   File: NetUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * post方式从服务器获取json数组
 * 
 * @return
 * @throws IOException
 * @throws ClientProtocolException
 * @throws JSONException
 */
public static JSONArray getJSONArrayByPost(String uri)
		throws ClientProtocolException, IOException, JSONException {
	Log.i(TAG, "<getJSONArrayByPost> uri:" + uri, Log.APP);
	StringBuilder builder = new StringBuilder();
	HttpParams httpParameters = new BasicHttpParams();
	// Set the default socket timeout (SO_TIMEOUT) in milliseconds which is
	// the timeout for waiting for data.
	HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
	HttpConnectionParams.setSoTimeout(httpParameters, 10000);
	HttpClient client = new DefaultHttpClient(httpParameters);
	HttpPost post = new HttpPost(uri);

	HttpResponse response = client.execute(post);

	BufferedReader reader = new BufferedReader(new InputStreamReader(
			response.getEntity().getContent()));
	for (String s = reader.readLine(); s != null; s = reader.readLine()) {
		builder.append(s);
	}

	String jsonString = new String(builder.toString());

	if ("{}".equals(jsonString)) {
		return null;
	}
	Log.i(TAG, "<getJSONArrayByPost> jsonString:" + jsonString, Log.DATA);
	return new JSONArray(jsonString);
}
 
Example 39
Project: java-client-api   File: ConnectedRESTQA.java    License: Apache License 2.0 5 votes vote down vote up
public static void deleteElementRangeIndexTemporalCollection(String dbName, String collectionName)
		throws Exception {
	DefaultHttpClient client = new DefaultHttpClient();
	client.getCredentialsProvider().setCredentials(new AuthScope(host_name, getAdminPort()),
			new UsernamePasswordCredentials("admin", "admin"));

	HttpDelete del = new HttpDelete("http://" + host_name + ":" + admin_port + "/manage/v2/databases/" + dbName
			+ "/temporal/collections?collection=" + collectionName + "&format=json");

	del.addHeader("Content-type", "application/json");
	del.addHeader("accept", "application/json");

	HttpResponse response = client.execute(del);
	HttpEntity respEntity = response.getEntity();
	if (response.getStatusLine().getStatusCode() == 400) {
		HttpEntity entity = response.getEntity();
		String responseString = EntityUtils.toString(entity, "UTF-8");
		System.out.println(responseString);
	} else if (respEntity != null) {
		// EntityUtils to get the response content
		String content = EntityUtils.toString(respEntity);
		System.out.println(content);
	} else {
		System.out.println("Collection: " + collectionName + " deleted");
		System.out.println("==============================================================");
	}
	client.getConnectionManager().shutdown();
}
 
Example 40
Project: product-ei   File: ODataTestUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static Object[] sendPOST(String endpoint, String content, Map<String, String> headers) throws IOException {
	HttpClient httpClient = new DefaultHttpClient();
	HttpPost httpPost = new HttpPost(endpoint);
	for (String headerType : headers.keySet()) {
		httpPost.setHeader(headerType, headers.get(headerType));
	}
	if (null != content) {
		HttpEntity httpEntity = new ByteArrayEntity(content.getBytes("UTF-8"));
		if (headers.get("Content-Type") == null) {
			httpPost.setHeader("Content-Type", "application/json");
		}
		httpPost.setEntity(httpEntity);
	}
	HttpResponse httpResponse = httpClient.execute(httpPost);
	if (httpResponse.getEntity() != null) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
		String inputLine;
		StringBuilder response = new StringBuilder();

		while ((inputLine = reader.readLine()) != null) {
			response.append(inputLine);
		}
		reader.close();
		return new Object[] { httpResponse.getStatusLine().getStatusCode(), response.toString() };
	} else {
		return new Object[] { httpResponse.getStatusLine().getStatusCode() };
	}
}