use of org.apache.http.protocol.BasicHttpContext in project dropwizard by dropwizard.
the class HttpClientBuilderTest method checkProxy.
private CloseableHttpClient checkProxy(HttpClientConfiguration config, HttpHost target, @Nullable HttpHost expectedProxy) throws Exception {
CloseableHttpClient httpClient = builder.using(config).build("test");
HttpRoutePlanner routePlanner = (HttpRoutePlanner) getInaccessibleField(httpClient.getClass(), "routePlanner").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.protocol.BasicHttpContext in project Anki-Android by Ramblurr.
the class HttpFetcher method fetchThroughHttp.
public static String fetchThroughHttp(String address, String encoding) {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(address);
HttpResponse response = httpClient.execute(httpGet, localContext);
if (!response.getStatusLine().toString().contains("OK")) {
return "FAILED";
}
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), Charset.forName(encoding)));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
return stringBuilder.toString();
} catch (Exception e) {
return "FAILED with exception: " + e.getMessage();
}
}
use of org.apache.http.protocol.BasicHttpContext 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.protocol.BasicHttpContext in project undertow by undertow-io.
the class SimpleConfidentialRedirectTestCase method simpleRedirectTestCase.
@Test
public void simpleRedirectTestCase() throws IOException, GeneralSecurityException {
TestHttpClient client = new TestHttpClient();
// create our own context to force http-request.config
// notice that, if we just create http context, the config is ovewritten before request is sent
// if we add the config to the HttpClient instead, it is ignored
HttpContext httpContext = new BasicHttpContext() {
private final RequestConfig config = RequestConfig.copy(RequestConfig.DEFAULT).setNormalizeUri(false).build();
@Override
public void setAttribute(final String id, final Object obj) {
if ("http.request-config".equals(id))
return;
super.setAttribute(id, obj);
}
@Override
public Object getAttribute(final String id) {
if ("http.request-config".equals(id))
return config;
return super.getAttribute(id);
}
};
client.setSSLContext(DefaultServer.getClientSSLContext());
try {
sendRequest(client, httpContext, "/foo", null);
sendRequest(client, httpContext, "/foo+bar", null);
sendRequest(client, httpContext, "/foo+bar;aa", null);
sendRequest(client, httpContext, "/foo+bar;aa", "x=y");
sendRequest(client, httpContext, "/foo+bar%3Aaa", "x=%3Ablah");
} finally {
client.getConnectionManager().shutdown();
}
}
use of org.apache.http.protocol.BasicHttpContext in project SmartAndroidSource by jaychou2012.
the class AbstractAjaxCallback method httpDo.
private void httpDo(HttpUriRequest hr, String url, Map<String, String> headers, AjaxStatus status) throws ClientProtocolException, IOException {
if (AGENT != null) {
hr.addHeader("User-Agent", AGENT);
}
if (headers != null) {
for (String name : headers.keySet()) {
hr.addHeader(name, headers.get(name));
}
}
if (GZIP && (headers == null || !headers.containsKey("Accept-Encoding"))) {
hr.addHeader("Accept-Encoding", "gzip");
}
String cookie = makeCookie();
if (cookie != null) {
hr.addHeader("Cookie", cookie);
}
if (ah != null) {
ah.applyToken(this, hr);
}
DefaultHttpClient client = getClient();
HttpParams hp = hr.getParams();
if (proxy != null)
hp.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
if (timeout > 0) {
AQUtility.debug("timeout param", CoreConnectionPNames.CONNECTION_TIMEOUT);
hp.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
hp.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
}
HttpContext context = new BasicHttpContext();
CookieStore cookieStore = new BasicCookieStore();
context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
request = hr;
if (abort) {
throw new IOException("Aborted");
}
HttpResponse response = client.execute(hr, context);
byte[] data = null;
String redirect = url;
int code = response.getStatusLine().getStatusCode();
String message = response.getStatusLine().getReasonPhrase();
String error = null;
HttpEntity entity = response.getEntity();
Header[] hs = response.getAllHeaders();
HashMap<String, String> responseHeaders = new HashMap<String, String>(hs.length);
for (Header h : hs) {
responseHeaders.put(h.getName(), h.getValue());
}
setResponseHeaders(responseHeaders);
File file = null;
if (code < 200 || code >= 300) {
try {
if (entity != null) {
InputStream is = entity.getContent();
byte[] s = toData(getEncoding(entity), is);
error = new String(s, "UTF-8");
AQUtility.debug("error", error);
}
} catch (Exception e) {
AQUtility.debug(e);
}
} else {
HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
redirect = currentHost.toURI() + currentReq.getURI();
int size = Math.max(32, Math.min(1024 * 64, (int) entity.getContentLength()));
OutputStream os = null;
InputStream is = null;
try {
file = getPreFile();
if (file == null) {
os = new PredefinedBAOS(size);
} else {
file.createNewFile();
os = new BufferedOutputStream(new FileOutputStream(file));
}
// AQUtility.time("copy");
copy(entity.getContent(), os, getEncoding(entity), (int) entity.getContentLength());
// AQUtility.timeEnd("copy", 0);
os.flush();
if (file == null) {
data = ((PredefinedBAOS) os).toByteArray();
} else {
if (!file.exists() || file.length() == 0) {
file = null;
}
}
} finally {
AQUtility.close(is);
AQUtility.close(os);
}
}
AQUtility.debug("response", code);
if (data != null) {
AQUtility.debug(data.length, url);
}
status.code(code).message(message).error(error).redirect(redirect).time(new Date()).data(data).file(file).client(client).context(context).headers(response.getAllHeaders());
}
Aggregations