Android 设置代理(验证用户名和密码)
2015-06-29 16:44
501 查看
这几天在研究在Android中,解析网页,但是公司内容,链接外网需要代理,并需要验证用户名和密码,十分头疼,网上查了下,没有头绪,最后总算在一个外国博客中看到类似的,记录下
URL url = new URL(urlString);
String host=android.net.Proxy.getDefaultHost();
int port=android.net.Proxy.getDefaultPort();
SocketAddress address=null;
try {
address=new InetSocketAddress(host, port);
} catch (Exception e) { System.out.println(e.getMessage()); }
Proxy proxy=new java.net.Proxy(java.net.Proxy.Type.HTTP, address);
String credentials = "用户名:密码";
byte[] toencode = null;
try {
toencode = credentials.getBytes("UTF-8");
} catch (UnsupportedEncodingException e1) { e1.printStackTrace(); }
HttpURLConnection ucon=null;
try {
ucon =(HttpURLConnection)url.openConnection(proxy);
ucon.setConnectTimeout(50000) ;
ucon.setRequestProperty("Proxy-Authorization", "Basic " + Base64.encodeToString(toencode, Base64.DEFAULT));
//ucon.connect();
} catch (Exception e) { e.printStackTrace(); }
//ucon.connect();
int responseCode = ucon.getResponseCode();
InputStream instr=null;
if(responseCode == HttpURLConnection.HTTP_OK){
instr = ucon.getInputStream();
}
BufferedInputStream bis = new BufferedInputStream(instr);
ByteArrayBuffer baf = new ByteArrayBuffer(1024);
int current = 0;
while ((current = bis.read()) != -1) {
//baf..append(new String(baf,0,current,"UTF-8"));
baf.append((byte) current);
}
instr.close();
bis.close();
ucon.disconnect();
return EncodingUtils.getString(baf.toByteArray(), "gb2312");
URL url = new URL(urlString);
String host=android.net.Proxy.getDefaultHost();
int port=android.net.Proxy.getDefaultPort();
SocketAddress address=null;
try {
address=new InetSocketAddress(host, port);
} catch (Exception e) { System.out.println(e.getMessage()); }
Proxy proxy=new java.net.Proxy(java.net.Proxy.Type.HTTP, address);
String credentials = "用户名:密码";
byte[] toencode = null;
try {
toencode = credentials.getBytes("UTF-8");
} catch (UnsupportedEncodingException e1) { e1.printStackTrace(); }
HttpURLConnection ucon=null;
try {
ucon =(HttpURLConnection)url.openConnection(proxy);
ucon.setConnectTimeout(50000) ;
ucon.setRequestProperty("Proxy-Authorization", "Basic " + Base64.encodeToString(toencode, Base64.DEFAULT));
//ucon.connect();
} catch (Exception e) { e.printStackTrace(); }
//ucon.connect();
int responseCode = ucon.getResponseCode();
InputStream instr=null;
if(responseCode == HttpURLConnection.HTTP_OK){
instr = ucon.getInputStream();
}
BufferedInputStream bis = new BufferedInputStream(instr);
ByteArrayBuffer baf = new ByteArrayBuffer(1024);
int current = 0;
while ((current = bis.read()) != -1) {
//baf..append(new String(baf,0,current,"UTF-8"));
baf.append((byte) current);
}
instr.close();
bis.close();
ucon.disconnect();
return EncodingUtils.getString(baf.toByteArray(), "gb2312");
相关文章推荐
- Android Studio Attach source code
- Android NDK开发之Jni调用Java对象
- Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用
- Android 通过Get/Post提交请求数据到服务器
- Android中实现静态的默认安装和卸载应用
- android 制作天气预报软件
- Android常见问题及开发经验总结(二)
- Android动画效果translate、scale、alpha、rotate详解(基础篇)
- android ndk/jni
- android 视频开发2分之2(仿美拍,糗百)
- Android 资源(resource)学习小结
- Android Activity子类
- android打包apk
- Android性能优化之布局优化篇
- android 防止bitmap 内存溢出
- android 视频开发2分之1(仿美拍,糗百)
- Android intent-filter
- android进阶之路——我的毕设讲解 第一章
- 对base-adapter-helper的简单分析
- Android应用开发多语言drawable文件夹