android網絡類型 wap代理設置(電信,移動,聯通)

代碼如下,直接嵌入程序可用!

/**
* 針對圖片下載的代理設置;
* 獲取動態代理的HttpURLConnection
* @param url  請求的url地址;
* @return HttpURLConnection 可以直接進行數據操作;
* @throws IOException
*
* 設置代理
* 設置頭
* 設置連接;
* 設置統計時間;
*/
public HttpURLConnection getDynamicNetWorkHttpURLConnection( HttpURLConnection httpURLConnection, String url ) throws IOException {

URL mURL = new URL( url );
if( DynamicNetWork.mConnectType == 0 ) {

if(mCurrentApnName == null){
mCurrentApnName = getCurrentApnInUse( context );
}

/** 移動的代理連接 */
if( mCurrentApnName.startsWith( CMWAP ) ) {
String mDomainName = getDomainName( url );
url = url.replace(mDomainName, mWAPLocalHost );
httpURLConnection = ( HttpURLConnection )mURL.openConnection();
httpURLConnection.setRequestProperty( "X-Online-Host", mDomainName );
/** 聯通的代理連接 */
}else if( mCurrentApnName.startsWith( UNIWAP ) || mCurrentApnName.startsWith( G3WAP )){
InetSocketAddress inetAddress = new InetSocketAddress( mWAPLocalHost, 80 );
java.net.Proxy.Type proxyType = java.net.Proxy.Type.valueOf( mURL.getProtocol().toUpperCase() );
java.net.Proxy javaProxy = new java.net.Proxy( proxyType, inetAddress );
httpURLConnection = ( HttpURLConnection )mURL.openConnection( javaProxy );
}else /** 電信的代理連接 */
if( mCurrentApnName.startsWith( CTWAP ) ) {
InetSocketAddress inetAddress = new InetSocketAddress( mCDMALocalHost, 80 );
java.net.Proxy.Type proxyType = java.net.Proxy.Type.valueOf( mURL.getProtocol().toUpperCase() );
java.net.Proxy javaProxy = new java.net.Proxy( proxyType, inetAddress );
httpURLConnection = ( HttpURLConnection )mURL.openConnection( javaProxy );
}
} else {
httpURLConnection = ( HttpURLConnection )mURL.openConnection();
}
httpURLConnection.connect();
return httpURLConnection;
}

/**獲取當前網絡名稱;
* @param mcontext
* @return
*/
public static String getCurrentApnInUse( Context mcontext ) {
String name = "no";
ConnectivityManager manager = ( ConnectivityManager )mcontext.getSystemService( Context.CONNECTIVITY_SERVICE );
try {
NetworkInfo activeNetInfo = manager.getActiveNetworkInfo();
if( activeNetInfo != null && activeNetInfo.isAvailable() ) {
name = activeNetInfo.getExtraInfo();
}
} catch( Exception e ) {
e.printStackTrace();
}
return name;
}

/**獲取當前代理
* @param currentName
* @return
*/
public static String getApnProxy( String currentName ) {
if( "".equals( currentName ) || null == currentName ) {
return "";
}
currentName = currentName.toLowerCase();
if( currentName.startsWith( CMWAP ) || currentName.startsWith( UNIWAP ) || currentName.startsWith( G3WAP ) )
return CMCC_WAPPROXY;
else if( currentName.startsWith( CTWAP ) )
return CMCC_CDMAPROXY;
else
return "";

}

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。