正确地访问网络资源----Proxy的使用

2024-02-28 20:38

本文主要是介绍正确地访问网络资源----Proxy的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://blog.csdn.net/sodino/article/details/6302313

 

天朝有很多独特的东西,今天要涉及到的是CMWAP与CMNET。

 

现在仍有许多Android手机用的是CMWAP连接网络的,如果只是简单滴使用URL.openConnection()开启一个HttpURLConnection,则会发现有一个UnknownHostException被抛出。唉...身为悲剧的程序员,就要开始解决这种bug了。

 

Sodino还发现一个现象,为什么自己的程序会崩掉,而Google浏览器就不会呢?与国产软件不同,国产软件身处天朝的软硬件环境下成长,可能会将代理IP(10.0.0.172)写进程序中,Sodino也确实反编译了几个程序后发现他们也是这样做来保持CMWAP下的正常联接。但是,Google浏览器可不是国人写的,那帮老外可不考虑天朝还有个CMWAP。那他们是如何保证网络的正常联接呢?一番鼓捣下,有了以下代码,可以在WIFI/CMWAP/CMNET/CTNET下正常访问网络,附带如何使用ping使用。

先上效果图:

connect

 

 

[java]  view plain copy
  1. package lab.sodino.network;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.net.HttpURLConnection;  
  5. import java.net.InetSocketAddress;  
  6. import java.net.MalformedURLException;  
  7. import java.net.URL;  
  8. import java.net.UnknownHostException;  
  9. import android.app.Activity;  
  10. import android.content.Context;  
  11. import android.net.ConnectivityManager;  
  12. import android.net.NetworkInfo;  
  13. import android.os.Bundle;  
  14. import android.os.Handler;  
  15. import android.os.Message;  
  16. import android.util.Log;  
  17. import android.view.View;  
  18. import android.widget.Button;  
  19. import android.widget.TextView;  
  20. public class NetworkAct extends Activity {  
  21.     public static final int CLEAR_TEXT = 0;  
  22.     public static final int APPEND_TEXT = 1;  
  23.     private TextView txtInfo;  
  24.     private Button btnPing;  
  25.     private Button btnConnect;  
  26.     private Button btnClear;  
  27.     private BtnListener btnListener;  
  28.     private Handler handler = new Handler() {  
  29.         public void handleMessage(Message msg) {  
  30.             switch (msg.what) {  
  31.             case APPEND_TEXT:  
  32.                 String content = msg.obj.toString();  
  33.                 txtInfo.setText("/n" + content);  
  34.                 break;  
  35.             case CLEAR_TEXT:  
  36.                 txtInfo.setText("");  
  37.                 break;  
  38.             }  
  39.         }  
  40.     };  
  41.     public void onCreate(Bundle savedInstanceState) {  
  42.         super.onCreate(savedInstanceState);  
  43.         setContentView(R.layout.main);  
  44.         btnListener = new BtnListener();  
  45.         txtInfo = (TextView) findViewById(R.id.txtInfo);  
  46.         btnPing = (Button) findViewById(R.id.btnPing);  
  47.         btnPing.setOnClickListener(btnListener);  
  48.         btnConnect = (Button) findViewById(R.id.btnConnect);  
  49.         btnConnect.setOnClickListener(btnListener);  
  50.         btnClear = (Button) findViewById(R.id.btnClear);  
  51.         btnClear.setOnClickListener(btnListener);  
  52.     }  
  53.     /** 
  54.      * @param param 
  55.      *            指定的域名如(www.google.com)或IP地址。 
  56.      */  
  57.     private void doPing(final String param) {  
  58.         new Thread() {  
  59.             public void run() {  
  60.                 String line = "";  
  61.                 InputStream is = null;  
  62.                 try {  
  63.                     line = "/nping -c 1 " + param;  
  64.                     // -c 1:表示ping的次数为1次。  
  65.                     Process p = Runtime.getRuntime().exec("ping -c 1 www.google.com");  
  66.                     // 等待该命令执行完毕。  
  67.                     int status = p.waitFor();  
  68.                     if (status == 0) {  
  69.                         // 正常退出  
  70.                         line += "Pass";  
  71.                     } else {  
  72.                         // 异常退出  
  73.                         line += "Fail: Host unreachable";  
  74.                     }  
  75.                     is = p.getInputStream();  
  76.                     byte[] data = new byte[is.available()];  
  77.                     is.read(data);  
  78.                     line += "/n" + new String(data);  
  79.                 } catch (UnknownHostException e) {  
  80.                     line += "Fail: Unknown Host";  
  81.                 } catch (IOException e) {  
  82.                     line += "Fail: IOException";  
  83.                 } catch (InterruptedException e) {  
  84.                     line += "Fail: InterruptedException";  
  85.                 }  
  86.                 Message msg = new Message();  
  87.                 msg.what = APPEND_TEXT;  
  88.                 msg.obj = line;  
  89.                 handler.sendMessage(msg);  
  90.             }  
  91.         }.start();  
  92.     }  
  93.     private void go2Network() {  
  94.         new Thread() {  
  95.             public void run() {  
  96.                 String line = "";  
  97.                 URL url = null;  
  98.                 HttpURLConnection httpConn = null;  
  99.                 InputStream is = null;  
  100.                 try {  
  101.                     url = new URL("http://www.sodino.com/index.html");  
  102.                     // Log.d("ANDROID_LAB", "Protocol=" + new  
  103.                     // URL("https://www.sodino.com").getProtocol());  
  104.                     // 需要android.permission.ACCESS_NETWORK_STATE  
  105.                     NetworkInfo networkInfo = ((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE))  
  106.                             .getActiveNetworkInfo();  
  107.                     // 如果是使用的运营商网络  
  108.                     if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {  
  109.                         // 获取默认代理主机ip  
  110.                         String host = android.net.Proxy.getDefaultHost();  
  111.                         // 获取端口  
  112.                         int port = android.net.Proxy.getDefaultPort();  
  113.                         if (host != null && port != -1) {  
  114.                             line += "/nhost[" + host + "] port[" + port + "]";  
  115.                             // 封装代理連接主机IP与端口号。  
  116.                             InetSocketAddress inetAddress = new InetSocketAddress(host, port);  
  117.                             // 根据URL链接获取代理类型,本链接适用于TYPE.HTTP  
  118.                             java.net.Proxy.Type proxyType = java.net.Proxy.Type.valueOf(url  
  119.                                     .getProtocol().toUpperCase());  
  120.                             java.net.Proxy javaProxy = new java.net.Proxy(proxyType, inetAddress);  
  121.                             httpConn = (HttpURLConnection) url.openConnection(javaProxy);  
  122.                         } else {  
  123.                             httpConn = (HttpURLConnection) url.openConnection();  
  124.                         }  
  125.                     } else {  
  126.                         httpConn = (HttpURLConnection) url.openConnection();  
  127.                     }  
  128.                     httpConn.connect();  
  129.                     int length = httpConn.getContentLength();  
  130.                     byte[] data = new byte[length];  
  131.                     String encoding = httpConn.getContentEncoding();  
  132.                     line += "/nlength=" + length + " encoding=" + encoding;  
  133.                     is = httpConn.getInputStream();  
  134.                     is.read(data);  
  135.                     String content = null;  
  136.                     if (encoding != null) {  
  137.                         content = new String(data, encoding);  
  138.                     } else {  
  139.                         content = new String(data);  
  140.                     }  
  141.                     line += "/nContent[/n" + content + "/n]";  
  142.                     Message msg = new Message();  
  143.                     msg.what = APPEND_TEXT;  
  144.                     msg.obj = line;  
  145.                     handler.sendMessage(msg);  
  146.                 } catch (IOException ie) {  
  147.                     ie.printStackTrace();  
  148.                 } finally {  
  149.                     try {  
  150.                         if (is != null) {  
  151.                             is.close();  
  152.                         }  
  153.                         if (httpConn != null) {  
  154.                             httpConn.disconnect();  
  155.                         }  
  156.                     } catch (IOException ie) {  
  157.                         ie.printStackTrace();  
  158.                     }  
  159.                 }  
  160.             }  
  161.         }.start();  
  162.     }  
  163.     class BtnListener implements Button.OnClickListener {  
  164.         public void onClick(View view) {  
  165.             if (view == btnPing) {  
  166.                 // doPing("www.google.com");  
  167.                 doPing("10.0.0.172");  
  168.             } else if (view == btnConnect) {  
  169.                 go2Network();  
  170.             } else if (view == btnClear) {  
  171.                 Message msg = new Message();  
  172.                 msg.what = CLEAR_TEXT;  
  173.                 handler.sendMessage(msg);  
  174.             }  
  175.         }  
  176.     }  
  177. }  

 

 

如此,悲剧的程序员解决了不同网络状态下如何保证正常链接的问题。

最后补充下:Android手机在CMWAP上要能够正常上网,除了设置一大堆代理数据外,还需进入拨号器下按"*#*#4636#*#*"→Phone Information下拉到最后后有个“Toggle DNS Check”,将其设置为allow

这篇关于正确地访问网络资源----Proxy的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/756561

相关文章

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.