/** * 获取网络连接 * * @param path * @return * @throws Exception */ public static HttpURLConnection getConnection(Context context, String path) throws Exception { HttpURLConnection conn = null; URL url = new URL(path); boolean isProxy = false; // 网络检测 ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (cm != null) { NetworkInfo nInfo = cm.getActiveNetworkInfo(); if (nInfo != null) { if (!nInfo.getTypeName().equalsIgnoreCase("WIFI")) { isProxy = true; } } } if (isProxy) { // 设置代理 String host = android.net.Proxy.getDefaultHost(); int port = android.net.Proxy.getDefaultPort(); SocketAddress sa = new InetSocketAddress(host, port); java.net.Proxy proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, sa); conn = (HttpURLConnection) url.openConnection(proxy); } else { conn = (HttpURLConnection) url.openConnection(); } return conn; }
public synchronized ApnWrapper getCurrApn() { if (mSDKVersion >= VersionUtils.Android_SDK_4_2) { return null; } NetworkInfo nInfo = mCm.getActiveNetworkInfo(); if (nInfo == null || nInfo.getType() != ConnectivityManager.TYPE_MOBILE) return null; if (mApnWrapper != null) return mApnWrapper; if (mCursor == null) { ini(); } if (mCursor != null && mCursor.moveToFirst()) { ApnWrapper aw = new ApnWrapper(); aw.apn = mCursor.getString(mCursor.getColumnIndex("apn")); aw.name = mCursor.getString(mCursor.getColumnIndex("name")); String port = mCursor.getString(mCursor.getColumnIndex("port")); try { aw.port = Integer.parseInt(port); } catch (NumberFormatException e) { aw.port = Proxy.getDefaultPort(); // e.printStackTrace(); } aw.proxy = mCursor.getString(mCursor.getColumnIndex("proxy")); mApnWrapper = aw; return aw; } return null; }
protected HttpURLConnection getConnect(Context context, URL url) throws Exception { if (null == url) { throw new IOException(LOG_TAG + "|getConnect|url is empty"); } final HttpUtil.NetworkState state = HttpUtil.getNetworkState(mContext); if (state == HttpUtil.NetworkState.UNAVAIL) { throw new HttpUnavailableException("net is unavailable"); } initPorxyUser(state); HttpURLConnection conn = null; mIsLimited = false; if (state == HttpUtil.NetworkState.MOBILE) { String proxyHost = android.net.Proxy.getDefaultHost(); if (proxyHost != null && proxyHost.length() > 0) { if (isCMCCServer(proxyHost)) { mIsLimited = true; StringBuffer new_address = new StringBuffer(80); new_address.append("http://"); new_address.append(android.net.Proxy.getDefaultHost()); String file = url.getFile(); if (file != null && file.startsWith("?")) { new_address.append("/"); } new_address.append(file); URL new_url = new URL(new_address.toString()); conn = (HttpURLConnection) new_url.openConnection(); conn.setRequestProperty("X-Online-Host", url.getHost()); } else { java.net.Proxy p = null; p = new java.net.Proxy( java.net.Proxy.Type.HTTP, new InetSocketAddress( android.net.Proxy.getDefaultHost(), android.net.Proxy.getDefaultPort())); conn = (HttpURLConnection) url.openConnection(p); if (mProxyUser != null) { conn.setRequestProperty("Proxy-Authorization", mProxyUser); } } } } if (conn == null) { conn = (HttpURLConnection) url.openConnection(); } return conn; }
/** �?��代理,是否cnwap接入 */ private void detectProxy() { ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); if (ni != null && ni.isAvailable() && ni.getType() == ConnectivityManager.TYPE_MOBILE) { String proxyHost = android.net.Proxy.getDefaultHost(); int port = android.net.Proxy.getDefaultPort(); if (proxyHost != null) { final InetSocketAddress sa = new InetSocketAddress(proxyHost, port); mProxy = new Proxy(Proxy.Type.HTTP, sa); } } }
public static boolean isCWWAPConnect(Context context) { boolean result = false; ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = connectivity.getActiveNetworkInfo(); if (info != null && info.getType() == ConnectivityManager.TYPE_MOBILE) { if ((Proxy.getDefaultHost() != null || Proxy.getHost(context) != null) && (Proxy.getPort(context) != -1 || Proxy.getDefaultPort() != -1)) { result = true; } } return result; }
/** * create a connection from a url. and add proxy if network is cmwap * * @param mUrl the url to build connection * @return the connection of mUrl * @throws java.io.IOException */ public static synchronized HttpURLConnection getHttpUrlConnection(Context mContext, URL mUrl) throws IOException { HttpURLConnection conn = null; if (NetUtil.isNetworkCmwap(mContext)) { java.net.Proxy p = new java.net.Proxy( java.net.Proxy.Type.HTTP, new InetSocketAddress( android.net.Proxy.getDefaultHost(), android.net.Proxy.getDefaultPort())); conn = (HttpURLConnection) mUrl.openConnection(p); } else conn = (HttpURLConnection) mUrl.openConnection(); conn.setConnectTimeout(DEFAULT_TIMEOUT_TIME); conn.setReadTimeout(DEFAULT_TIMEOUT_TIME); return conn; }
/** returns true on success, false if the user must correct something */ boolean saveToDb() { String hostname = mHostnameField.getText().toString().trim(); String portStr = mPortField.getText().toString().trim(); int port = -1; int result = validate(hostname, portStr); if (result > 0) { showError(result); return false; } if (portStr.length() > 0) { try { port = Integer.parseInt(portStr); } catch (NumberFormatException ex) { return false; } } // FIXME: The best solution would be to make a better UI that would // disable editing of the text boxes if the user chooses to use the // default settings. i.e. checking a box to always use the default // carrier. http:/b/issue?id=756480 // FIXME: This currently will not work if the default host is blank and // the user has cleared the input boxes in order to not use a proxy. // This is a UI problem and can be solved with some better form // controls. // FIXME: If the user types in a proxy that matches the default, should // we keep that setting? Can be fixed with a new UI. ContentResolver res = getContentResolver(); if (hostname.equals(Proxy.getDefaultHost()) && port == Proxy.getDefaultPort()) { // If the user hit the default button and didn't change any of // the input boxes, treat it as if the user has not specified a // proxy. hostname = null; } if (!TextUtils.isEmpty(hostname)) { hostname += ':' + portStr; } Settings.Secure.putString(res, Settings.Secure.HTTP_PROXY, hostname); Settings.Secure.putInt( res, Settings.Secure.HTTP_PROXY_WIFI_ONLY, mProxyWifiOnly.isChecked() ? 1 : 0); sendBroadcast(new Intent(Proxy.PROXY_CHANGE_ACTION)); return true; }
private boolean initProxySetting() { ConnectivityManager ConnMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = ConnMgr.getActiveNetworkInfo(); if (info != null) { if (info.getType() == ConnectivityManager.TYPE_WIFI) { Constants.mProxyPort = 0; Constants.mProxyHost = null; } else { Constants.mProxyHost = android.net.Proxy.getDefaultHost(); Constants.mProxyPort = android.net.Proxy.getDefaultPort(); } return (!TextUtils.isEmpty(Constants.mProxyHost) && Constants.mProxyPort != 0); } return false; }
/** * 连接服务器, 获取连接 * * @param url 地址 * @return HttpURLConnection null表示失败 */ private HttpURLConnection getConnect(URL url) { NetTpyeEnmu state = BdNetUtil.getNetType(); try { if (state == NetTpyeEnmu.UNAVAIL) { return null; } else if (state == NetTpyeEnmu.NET || state == NetTpyeEnmu.WAP) { String proxyHost = android.net.Proxy.getDefaultHost(); if (proxyHost != null && proxyHost.length() > 0) { if (isCMCCServer(proxyHost)) { StringBuilder new_address = new StringBuilder(80); new_address.append("http://"); new_address.append(android.net.Proxy.getDefaultHost()); String file = url.getFile(); if (file != null && file.startsWith("?")) { new_address.append("/"); } new_address.append(file); URL new_url = new URL(new_address.toString()); mConn = (HttpURLConnection) new_url.openConnection(); context.getRequest().addHeadData("X-Online-Host", url.getHost()); } else { java.net.Proxy p = null; p = new java.net.Proxy( java.net.Proxy.Type.HTTP, new InetSocketAddress( android.net.Proxy.getDefaultHost(), android.net.Proxy.getDefaultPort())); mConn = (HttpURLConnection) url.openConnection(p); } } } if (mConn == null) { mConn = (HttpURLConnection) url.openConnection(); } } catch (Exception ex) { ex.printStackTrace(); } return mConn; }
void populateFields(boolean useDefault) { String hostname = null; int port = -1; if (useDefault) { // Use the default proxy settings provided by the carrier hostname = Proxy.getDefaultHost(); port = Proxy.getDefaultPort(); } else { // Use the last setting given by the user hostname = Proxy.getHost(this); port = Proxy.getPort(this); } if (hostname == null) { hostname = ""; } mHostnameField.setText(hostname); String portStr = port == -1 ? "" : Integer.toString(port); mPortField.setText(portStr); mProxyWifiOnly.setChecked(Proxy.isProxyForWifiOnly(this)); Intent intent = getIntent(); String buttonLabel = intent.getStringExtra("button-label"); if (!TextUtils.isEmpty(buttonLabel)) { mOKButton.setText(buttonLabel); } String title = intent.getStringExtra("title"); if (!TextUtils.isEmpty(title)) { setTitle(title); } }
@SuppressWarnings("deprecation") private static int getDefaultPort() { return android.net.Proxy.getDefaultPort(); }
public static int getProxyPort(Context context) { int res = Proxy.getPort(context); if (res == -1) res = Proxy.getDefaultPort(); return res; }