为HttpClient和HttpURLConnection添加中国移动代理
转自: http://www.2cto.com/kf/201111/112100.html
在android中,一般需要联网的时候前,都要做一次网络的判断,判断当前的网络状态!然后开始请求网络
当我们使用wap网络的时候,程序中必须要中国移动代理!这样的话,手机才能正常的访问internet!
在android中,有两种方式请求网络:HttpURLConnection和HttpClient请求方式,如果网络状态为wap的时候,都要为两种请求添加中国移动代理的!
第一种方式:HttpURLConnection
/**
* @author sky
* 使用HttpURLConnection请求Internet
* @param context context对象
* @param requestUrl 请求的URL
* @param param 请求的参数
* @return 返回一个inputstream流
*/
public static InputStream getHttpURLConnectionInputStream(Context context,
String requestUrl, Map<String, String> param) {
URL url;
HttpURLConnection conn = null;
InputStream input = null;
try {
url = new URL(requestUrl);
if (getAPNType(context) == NetWorkUtil.CMWAP) // 当请求的网络为wap的时候,就需要添加中国移动代理
{
Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,
new InetSocketAddress("10.0.0.172", ));
conn = (HttpURLConnection) url.openConnection(proxy);
}
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(); // 请求超时
conn.setRequestMethod("POST"); // 请求方式
conn.setReadTimeout(); // 读取超时
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded"); OutputStream os = conn.getOutputStream();
StringBuilder sb = new StringBuilder();
Iterator<String> it = param.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
String value = param.get(key);
sb.append(key).append("=").append(value).append("&");
}
String p = sb.toString().substring(, sb.length() - );
System.out.println("请求的参数" + p);
os.write(p.getBytes("utf-8"));
os.close();
if (conn != null){
input = conn.getInputStream();
}
} catch (Exception e) {
e.printStackTrace();
}
return input;
}
上面这种方式就是HttpURLConnection ,这种方式在android开发中也是比较常用的,希望朋友们也要熟悉的掌握!
第二种方式:HttpClient
/**
*
* @author sky
*
* 使用HttpURLConnection请求Internet
*
* @param context
* context对象
*
* @param requestUrl
* 请求的URL
*
* @param param
* 请求的参数
*
* @return 返回一个inputstream流
*/
public static InputStream getHttpClientInputStream(Context context,String requestUrl, Map<String, String> param)
throws Exception {
HttpClient client = new DefaultHttpClient();
if(getAPNType(context)==NetWorkUtil.CMWAP) //当请求的网络为wap的时候,就需要添加中国移动代理
{
HttpHost proxy = new HttpHost("10.0.0.172", );
client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}
HttpPost hp = new HttpPost(requestUrl);
hp.setHeader("Charset", "UTF-8");
hp.setHeader("Content-Type", "application/x-www-form-urlencoded");
List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
Iterator<String> it = param.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
list.add(new BasicNameValuePair(key, param.get(key)));
}
hp.setEntity(new UrlEncodedFormEntity(list,"UTF-8"));
HttpResponse response = null;
response = client.execute(hp);
return response.getEntity().getContent();
}
这个httpClient实现了android内置的DefaultHttpClient,所以使用起来还是很方便的!
但是我发现HttpClient 比HttpURLConnection 要好一些,因为HttpURLConnection 如果使用wap在上网请求的时候,存在很多问题的(我是深有体会的,比如请求无响应,信号不好都可能造成一些未知的错误)
为HttpClient和HttpURLConnection添加中国移动代理的更多相关文章
- Android 网络编程之---HttpClient 与 HttpURLConnection 共用cookie
HttpClient 与 HttpURLConnection 共用 SessionId HttpClient 与 HttpUrlConnection 是Android 中HTTP操作最常见的訪问方式. ...
- java分别通过httpclient和HttpURLConnection获取图片验证码内容
前面的文章,介绍了如何通过selenium+Tesseract-OCR来识别图片验证码,如果用接口来访问的话,再用selenium就闲的笨重,下面就介绍一下分别通过httpclient和HttpURL ...
- HttpClient和HttpURLConnection的使用和区别(下)
转自来自点击打开链接 接着上一篇,我们继续来分析HttpURLConnection的使用,以及两者的共同点和区别. 目录 用法 HttpURLConnection 区别 引用资料 用法 HttpURL ...
- HttpClient和HttpURLConnection整合汇总对比
性能 1.HttpUrlConnection直接支持GZIP压缩:HttpClient也支持,但要自己写代码处理. 2.HttpUrlConnection直接支持系统级连接池,即打开的连接不会直接关闭 ...
- Android 中HttpClient和HttpURLConnection选取
原文地址:http://android-developers.blogspot.com/2011/09/androids-http-clients.html 译文:http://yunfeng.sin ...
- Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点和性能对比
比较的指标: 1.cpu 2.流量 3.电量 4.内存占用 5.联网时间 功能点: 1.重试机制 2.提供的扩展功能 3.易用性 4.是否https 5.是否支持reflect api,OkHttp有 ...
- 关于HttpClient,HttpURLConnection,OkHttp的用法
1 HttpClient入门实例 1.1发送get请求 /** * HttpClient发送get请求 * @param url 请求地址 * @return * @throws IOExceptio ...
- HttpClient和HttpURLConnection的使用和区别(上)
转自:点击打开链接 相信很多Android开发者碰到涉及到Http协议的需求时,都和我一样在犹豫是使用HttpClient还是使用HttpURLConnection呢.我在网上也搜索了很多文章,来分析 ...
- HttpClient和HttpURLConnection的区别
总结了网上的一些资源,主要有以下两个观点: 分析一: 在研究Volley框架的源码中,发现它在HTTP请求的使用上比较有意思,在Android 2.3及以上版本,使用的是HttpURLConnecti ...
随机推荐
- 怎样使用SSH连接OpenStack上的云主机
转载请注明出处.否则将追究法律责任http://blog.csdn.net/xingjiarong/article/details/47021815 在上一篇博客中我介绍了怎样在OpenStack中创 ...
- Ubuntu使用adb连接android手机失败unknown的解决的方法
Ubuntu使用adb连接android手机失败unknown的解决的方法 Ubuntu下通过USB数据线连接G11手机后,adb可能无法识别到设备.依照一下步骤能够解决此问题. 1.在termi ...
- 鸡肋的JdbcRDD
今天准备将mysql的数据倒腾到RDD.非常早曾经就知道有一个JdbcRDD.就想着使用一下,结果发现却是鸡肋一个. 首先,看看JdbcRDD的定义: * An RDD tha ...
- [转] logback 常用配置详解(序)logback 简介
转载文章:原文出处:http://aub.iteye.com/blog/1101222 logback 简介 Ceki Gülcü在Java日志领域世界知名.他创造了Log4J ,这个最早的Java日 ...
- Error CREATEing SolrCore 'new_core': Unable to create core [new_core] Caused by: Can't find resource 'solrconfig.xml' in classpath or 'D:\solr\solr-7.2.1\server\solr\new_core'
\solr-7.2.1\server\solr\configsets\_default 下的conf 复制到: \solr-7.2.1\server\solr\new_core
- mysql12----explain
explain 可以帮助我们在不真正执行某个sql语句时,就执行mysql怎样执行,这样利用我们去分析sql指令.尽量避免全表扫描. Id: SELECT识别符.这是SELECT的查询序列号 ) \G ...
- Lightoj 1016 - Brush (II)
After the long contest, Samee returned home and got angry after seeing his room dusty. Who likes to ...
- YTU 2405: C语言习题 牛顿迭代法求根
2405: C语言习题 牛顿迭代法求根 时间限制: 1 Sec 内存限制: 128 MB 提交: 562 解决: 317 题目描述 用牛顿迭代法求根.方程为ax3+bx2+cx+d=0.系数a,b ...
- YTU 2572: 猜灯谜
2572: 猜灯谜 时间限制: 1 Sec 内存限制: 128 MB 提交: 154 解决: 91 题目描述 A 村的元宵节灯会上有一迷题: 请猜谜 * 请猜谜 = 请边赏灯边猜 小明想,一定是每 ...
- Django缓存系统选择之Memcached与Redis的区别与性能对比
Django支持使用Memcached和Redis这两种流行的内存型数据库作为缓存系统.我们今天来看Memcached和Redis的区别和性能对比. redis和memcached的区别 1.Redi ...