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;
}
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;
}
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);
}
}
}
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;
}
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);
}
}
Aggregations