代碼如下,直接嵌入程式可用!
/**
* 針對圖片下載的代理設定;
* 擷取動態代理的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 "";
}