Search in sources :

Example 26 with HttpGet

use of org.apache.http.client.methods.HttpGet in project dropwizard by dropwizard.

the class HttpClientBuilderTest method checkProxy.

private CloseableHttpClient checkProxy(HttpClientConfiguration config, HttpHost target, HttpHost expectedProxy) throws Exception {
    CloseableHttpClient httpClient = builder.using(config).build("test");
    HttpRoutePlanner routePlanner = (HttpRoutePlanner) FieldUtils.getField(httpClient.getClass(), "routePlanner", true).get(httpClient);
    HttpRoute route = routePlanner.determineRoute(target, new HttpGet(target.toURI()), new BasicHttpContext());
    assertThat(route.getProxyHost()).isEqualTo(expectedProxy);
    assertThat(route.getTargetHost()).isEqualTo(target);
    assertThat(route.getHopCount()).isEqualTo(expectedProxy != null ? 2 : 1);
    return httpClient;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpRoute(org.apache.http.conn.routing.HttpRoute) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpGet(org.apache.http.client.methods.HttpGet)

Example 27 with HttpGet

use of org.apache.http.client.methods.HttpGet in project Fling by entertailion.

the class HttpRequestHelper method sendGet.

public String sendGet(String scheme, String host, int port, String path, String... query) throws URISyntaxException {
    String queryParams = buildgetData(query);
    URI uri = URIUtils.createURI(scheme, host, port, path, queryParams, null);
    httpGet = new HttpGet(uri);
    try {
        response = httpClient.execute(httpGet);
    } catch (Throwable e) {
        Log.e(TAG, "sendGet exception: ", e);
    }
    // we assume that the response body contains the error message
    try {
        ret = EntityUtils.toString(response.getEntity());
    } catch (Throwable e) {
        Log.e(TAG, "sendGet exception: ", e);
    }
    return ret;
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) URI(java.net.URI)

Example 28 with HttpGet

use of org.apache.http.client.methods.HttpGet in project Fling by entertailion.

the class RampClient method closeCurrentApp.

public void closeCurrentApp() {
    if (dialServer != null) {
        try {
            DefaultHttpClient defaultHttpClient = HttpRequestHelper.createHttpClient();
            CustomRedirectHandler handler = new CustomRedirectHandler();
            defaultHttpClient.setRedirectHandler(handler);
            BasicHttpContext localContext = new BasicHttpContext();
            // check if any app is running
            HttpGet httpGet = new HttpGet(dialServer.getAppsUrl());
            httpGet.setHeader(HEADER_CONNECTION, HEADER_CONNECTION_VALUE);
            httpGet.setHeader(HEADER_USER_AGENT, HEADER_USER_AGENT_VALUE);
            httpGet.setHeader(HEADER_ACCEPT, HEADER_ACCEPT_VALUE);
            httpGet.setHeader(HEADER_DNT, HEADER_DNT_VALUE);
            httpGet.setHeader(HEADER_ACCEPT_ENCODING, HEADER_ACCEPT_ENCODING_VALUE);
            httpGet.setHeader(HEADER_ACCEPT_LANGUAGE, HEADER_ACCEPT_LANGUAGE_VALUE);
            HttpResponse httpResponse = defaultHttpClient.execute(httpGet);
            if (httpResponse != null) {
                int responseCode = httpResponse.getStatusLine().getStatusCode();
                Log.d(LOG_TAG, "get response code=" + httpResponse.getStatusLine().getStatusCode());
                if (responseCode == 204) {
                // nothing is running
                } else if (responseCode == 200) {
                    // app is running
                    // Need to get real URL after a redirect
                    // http://stackoverflow.com/a/10286025/594751
                    String lastUrl = dialServer.getAppsUrl();
                    if (handler.lastRedirectedUri != null) {
                        lastUrl = handler.lastRedirectedUri.toString();
                        Log.d(LOG_TAG, "lastUrl=" + lastUrl);
                    }
                    String response = EntityUtils.toString(httpResponse.getEntity());
                    Log.d(LOG_TAG, "get response=" + response);
                    parseXml(new StringReader(response));
                    Header[] headers = httpResponse.getAllHeaders();
                    for (int i = 0; i < headers.length; i++) {
                        Log.d(LOG_TAG, headers[i].getName() + "=" + headers[i].getValue());
                    }
                    // stop the app instance
                    HttpDelete httpDelete = new HttpDelete(lastUrl);
                    httpResponse = defaultHttpClient.execute(httpDelete);
                    if (httpResponse != null) {
                        Log.d(LOG_TAG, "delete response code=" + httpResponse.getStatusLine().getStatusCode());
                        response = EntityUtils.toString(httpResponse.getEntity());
                        Log.d(LOG_TAG, "delete response=" + response);
                    } else {
                        Log.d(LOG_TAG, "no delete response");
                    }
                }
            } else {
                Log.i(LOG_TAG, "no get response");
                return;
            }
        } catch (Exception e) {
            Log.e(LOG_TAG, "closeCurrentApp", e);
        }
    }
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpGet(org.apache.http.client.methods.HttpGet) StringReader(java.io.StringReader) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ProtocolException(org.apache.http.ProtocolException)

Example 29 with HttpGet

use of org.apache.http.client.methods.HttpGet in project Fling by entertailion.

the class RampClient method getAppStatus.

/**
	 * Do HTTP GET for app status to determine response code and response body
	 * 
	 * @param defaultHttpClient
	 * @param url
	 * @return
	 */
private int getAppStatus(DefaultHttpClient defaultHttpClient, String url) {
    int responseCode = 200;
    try {
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = defaultHttpClient.execute(httpGet);
        if (httpResponse != null) {
            responseCode = httpResponse.getStatusLine().getStatusCode();
            Log.d(LOG_TAG, "get response code=" + responseCode);
            response = EntityUtils.toString(httpResponse.getEntity());
            Log.d(LOG_TAG, "get response=" + response);
        } else {
            Log.i(LOG_TAG, "no get response");
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, "getAppStatus", e);
    }
    return responseCode;
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) ProtocolException(org.apache.http.ProtocolException)

Example 30 with HttpGet

use of org.apache.http.client.methods.HttpGet in project android-app by eoecn.

the class CustomHttpClient method getFromWebByHttpClient.

public static String getFromWebByHttpClient(Context context, String url, NameValuePair... nameValuePairs) throws Exception {
    log.d("getFromWebByHttpClient url = " + url);
    try {
        // http地址
        // String httpUrl =
        // "http://192.168.1.110:8080/httpget.jsp?par=HttpClient_android_Get";
        StringBuilder sb = new StringBuilder();
        sb.append(url);
        if (nameValuePairs != null && nameValuePairs.length > 0) {
            sb.append("?");
            for (int i = 0; i < nameValuePairs.length; i++) {
                if (i > 0) {
                    sb.append("&");
                }
                sb.append(String.format("%s=%s", nameValuePairs[i].getName(), nameValuePairs[i].getValue()));
            }
        }
        // HttpGet连接对象
        HttpGet httpRequest = new HttpGet(sb.toString());
        // 取得HttpClient对象
        HttpClient httpclient = getHttpClient(context);
        // 请求HttpClient,取得HttpResponse
        HttpResponse httpResponse = httpclient.execute(httpRequest);
        // 请求成功
        if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            throw new RuntimeException(context.getResources().getString(R.string.httpError));
        }
        return EntityUtils.toString(httpResponse.getEntity());
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        throw new RuntimeException(context.getResources().getString(R.string.httpError), e);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        log.e("IOException ");
        e.printStackTrace();
        throw new RuntimeException(context.getResources().getString(R.string.httpError), e);
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) ParseException(org.apache.http.ParseException) IOException(java.io.IOException)

Aggregations

HttpGet (org.apache.http.client.methods.HttpGet)1143 HttpResponse (org.apache.http.HttpResponse)717 Test (org.junit.Test)504 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)242 TestHttpClient (io.undertow.testutils.TestHttpClient)239 IOException (java.io.IOException)200 HttpClient (org.apache.http.client.HttpClient)185 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)179 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)176 HttpEntity (org.apache.http.HttpEntity)152 Header (org.apache.http.Header)133 InputStream (java.io.InputStream)103 URI (java.net.URI)83 JsonNode (com.fasterxml.jackson.databind.JsonNode)68 ArrayList (java.util.ArrayList)60 MockResponse (com.google.mockwebserver.MockResponse)54 HttpPost (org.apache.http.client.methods.HttpPost)54 Deployment (org.activiti.engine.test.Deployment)49 ClientProtocolException (org.apache.http.client.ClientProtocolException)46 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)45