The Code is as follows. The embedded program is available!
/**
* Proxy settings for image download;
* Get the HttpURLConnection of the dynamic proxy
* @ Param url: the url of the request;
* @ Return HttpURLConnection can be used for direct data operations;
* @ Throws IOException
*
* Set proxy
* Set the header
* Set the connection;
* Set the statistical time;
*/
Public HttpURLConnection getDynamicNetWorkHttpURLConnection (HttpURLConnection httpURLConnection, String url) throws IOException {
URL mURL = new URL (url );
If (DynamicNetWork. mConnectType = 0 ){
If (mCurrentApnName = null ){
MCurrentApnName = getCurrentApnInUse (context );
}
/** Mobile proxy connection */
If (mCurrentApnName. startsWith (CMWAP )){
String mDomainName = getDomainName (url );
Url = url. replace (mDomainName, mWAPLocalHost );
HttpURLConnection = (HttpURLConnection) mURL. openConnection ();
HttpURLConnection. setRequestProperty ("X-Online-Host", mDomainName );
/** Unicom proxy connection */
} 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/** proxy connection for telecom */
If (mCurrentApnName. startsWith (CTWAP )){
InetSocketAddress inetAddress = new InetSocketAddress (mcmalocalhost, 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;
}
/** Get the current network name;
* @ 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;
}
/** Get the current proxy
* @ 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 "";
}