use of org.apache.http.client.HttpClient in project neo4j by neo4j.
the class ConfigureBaseUriIT method shouldUseRequestUriWhenNoXForwardHeadersPresent.
@Test
public void shouldUseRequestUriWhenNoXForwardHeadersPresent() throws Exception {
URI rootUri = functionalTestHelper.baseUri();
HttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet(rootUri);
httpget.setHeader("Accept", "application/json");
HttpResponse response = httpclient.execute(httpget);
String length = response.getHeaders("CONTENT-LENGTH")[0].getValue();
byte[] data = new byte[Integer.valueOf(length)];
response.getEntity().getContent().read(data);
String responseEntityBody = new String(data);
assertFalse(responseEntityBody.contains("https://foobar.com"));
assertFalse(responseEntityBody.contains(":0"));
assertTrue(responseEntityBody.contains("http://localhost"));
} finally {
httpclient.getConnectionManager().shutdown();
}
}
use of org.apache.http.client.HttpClient in project UltimateAndroid by cymcsg.
the class HttpUtils_Deprecated method GetJson_Cookie.
/**
* 处理httpResponse信息,返回json,保存cookie
*
* @param
* @return String
*/
public static String GetJson_Cookie(String httpUrl) {
String strResult = null;
try {
// HttpGet连接对象
HttpGet httpRequest = new HttpGet(httpUrl);
// 取得HttpClient对象
HttpClient httpclient = new DefaultHttpClient();
// 请求HttpClient,取得HttpResponse
HttpResponse httpResponse = httpclient.execute(httpRequest);
//保存cookie
StringBuffer sb = new StringBuffer();
String inputLine = "";
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
Header[] hds = httpResponse.getAllHeaders();
int isok = 0;
for (int index = 0; index < hds.length; index++) {
if ("Set-Cookie".equals(hds[index].getName())) {
String value = hds[index].getValue();
String[] vs = value.split(";");
for (int i = 0; i < vs.length; i++) {
String[] vss = vs[i].split("=");
if ("member".equals(vss[0])) {
rst = vs[i] + ";";
Log.d("Chen", "cookie信息:" + rst);
isok++;
}
}
}
}
}
// 请求成功
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得返回的字符串
strResult = EntityUtils.toString(httpResponse.getEntity());
} else {
Log.i(TAG, "请求错误");
}
} catch (Exception e) {
Log.i(TAG, "请求网络异常");
strResult = "net_ex";
}
return strResult;
}
use of org.apache.http.client.HttpClient in project UltimateAndroid by cymcsg.
the class CommonHttpClient method getNewInstance.
// 每次返回同一实例
// public static synchronized HttpClient getInstance(Context mContext){
//
// if(null == singleStance){
// singleStance = getNewInstance(mContext);
// }
// return singleStance ;
// }
// 每次都返回新的HttpClient实例
public static HttpClient getNewInstance(Context mContext) {
HttpClient newInstance;
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(params, true);
// 自定义三个timeout参数
/*
* 1.set a timeout for the connection manager,it defines how long we
* should wait to get a connection out of the connection pool managed by
* the connection manager
*/
ConnManagerParams.setTimeout(params, 5000);
/*
* 2.The second timeout value defines how long we should wait to make a
* connection over the network to the server on the other end
*/
HttpConnectionParams.setConnectionTimeout(params, TIMEOUT);
/*
* 3.we set a socket timeout value to 4 seconds to define how long we
* should wait to get data back for our request.
*/
HttpConnectionParams.setSoTimeout(params, TIMEOUT_SOCKET);
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
newInstance = new DefaultHttpClient(conMgr, params);
switch(checkNetworkTypeDeprecated(mContext)) {
case TYPE_CT_WAP:
{
// 通过代理解决中国移动联通GPRS中wap无法访问的问题
HttpHost proxy = new HttpHost("10.0.0.200", 80, "http");
newInstance.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
Logs.v("当前网络类型为cm_cu_wap,设置代理10.0.0.200访问www");
}
break;
case TYPE_CM_CU_WAP:
{
// 通过代理解决中国移动联通GPRS中wap无法访问的问题
HttpHost proxy = new HttpHost("10.0.0.172", 80, "http");
newInstance.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
Logs.v("当前网络类型为cm_cu_wap,设置代理10.0.0.172访问www");
}
break;
}
return newInstance;
}
use of org.apache.http.client.HttpClient in project Xponents by OpenSextant.
the class SharepointClient method getClient.
/**
* Sharepoint requires NTLM. This client requires a non-null user/passwd/domain.
*
*/
@Override
public HttpClient getClient() {
if (currentConn != null) {
return currentConn;
}
HttpClientBuilder clientHelper = HttpClientBuilder.create();
if (proxyHost != null) {
clientHelper.setProxy(proxyHost);
}
RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
CredentialsProvider creds = new BasicCredentialsProvider();
creds.setCredentials(AuthScope.ANY, new NTCredentials(user, passwd, server, domain));
clientHelper.setDefaultCredentialsProvider(creds);
HttpClient httpClient = clientHelper.setDefaultRequestConfig(globalConfig).build();
return httpClient;
}
use of org.apache.http.client.HttpClient in project Xponents by OpenSextant.
the class WebClient method getClient.
/**
* TODO: Update to use HTTP client "HttpClients....build()" method of creating and tailoring HttpClient
* using the proxy and cookie settings, as well as any other tuning.
*
* Override if your context requires a different style of HTTP client.
*
* @return HttpClient 4.x object
*/
public HttpClient getClient() {
HttpClientBuilder clientHelper = null;
if (this.useSystemProperties) {
clientHelper = HttpClientBuilder.create().useSystemProperties();
} else {
clientHelper = HttpClientBuilder.create();
if (proxyHost != null) {
clientHelper.setProxy(proxyHost);
}
}
RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
HttpClient httpClient = clientHelper.setDefaultRequestConfig(globalConfig).build();
return httpClient;
}
Aggregations