标签:
因为公司的网络必须要通过代理才能访问外网, 给开发带了极大的不便。总共有两种一中是使用java自带的HttpURLConnection还有一种是使用Android平台中已经继承了的HttpClient
1
2
3
4
5
6
7
8
9
10
11
12
13
|
private
HttpClient client = null ; private
String url = PropertiesUtil.getUrl(); private
Gson gson; public
RestTemplate () { HttpParams
httpParams = new
BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams,
50
* 1000 ); HttpConnectionParams.setSoTimeout(httpParams,
20
* 1000 ); HttpHost
proxy = new
HttpHost( "10.50.0.100" ,
8080 ,
"http" ); client
= new
DefaultHttpClient(httpParams); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
proxy); gson
= new
Gson(); } |
1
2
3
4
5
6
7
|
URL
url = new
URL(strUrl); Proxy
proxy = new
Proxy(Proxy.Type.HTTP, new
InetSocketAddress( "10.50.0.100" , 8080 )); HttpURLConnection
conn=(HttpURLConnection) url.openConnection(proxy); conn.setUseCaches( false ); conn.setRequestMethod(METHOD_GET); conn.setConnectTimeout(CONNECTION_TIMEOUT); conn.setReadTimeout(READ_TIMEOUT); |
标签:
原文地址:http://blog.csdn.net/u014311042/article/details/42805385