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

來源:互聯網
上載者:User

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


/**
* 針對圖片下載的代理設定;
* 擷取動態代理的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 "";

}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.