/** * 获取网络连接 * * @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; }
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; }
/** * 检测wap状态的httphost * * @param context * @return @Title: checkHttpHost */ @SuppressWarnings("deprecation") public static HttpHost checkHttpHost(Context context) { if (isWap(context)) { String h = android.net.Proxy.getHost(context); int port = android.net.Proxy.getPort(context); if (h == null || TextUtils.isEmpty(h.trim())) { h = CheckRequestState.DEFAULT_PROXY_HOST; } if (port == -1) { port = 80; } return new HttpHost(h, port); } return null; }
/** * 检测wap状态 UrlConnection的代理 * * @param context * @return @Title: checkUrlConnectionProxy */ @SuppressWarnings("deprecation") public static java.net.Proxy checkUrlConnectionProxy(Context context) { if (isWap(context)) { String h = android.net.Proxy.getHost(context); int port = android.net.Proxy.getPort(context); if (h == null || TextUtils.isEmpty(h.trim())) { h = CheckRequestState.DEFAULT_PROXY_HOST; } if (port == -1) { port = 80; } return new Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress(h, port)); } return null; }
/** * 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; }
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; }
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; }
/** * Get network type name * * @param context * @return */ public static String getNetworkTypeName(Context context) { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo; String type = NETWORK_TYPE_DISCONNECT; if (manager == null || (networkInfo = manager.getActiveNetworkInfo()) == null) { return type; } ; if (networkInfo.isConnected()) { String typeName = networkInfo.getTypeName(); if ("WIFI".equalsIgnoreCase(typeName)) { type = NETWORK_TYPE_WIFI; } else if ("MOBILE".equalsIgnoreCase(typeName)) { String proxyHost = android.net.Proxy.getDefaultHost(); type = TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORK_TYPE_3G : NETWORK_TYPE_2G) : NETWORK_TYPE_WAP; } else { type = NETWORK_TYPE_UNKNOWN; } } return type; }
/** * 连接服务器, 获取连接 * * @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); } }
/** 获取网络状态,wifi,wap,2g,3g. */ public static String netWorkType(Context context) { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = manager.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { String type = networkInfo.getTypeName(); if (type.equalsIgnoreCase("WIFI")) { return "WIFI"; } else if (type.equalsIgnoreCase("MOBILE")) { String proxyHost = android.net.Proxy.getDefaultHost(); return TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? "3G" : "2G") : "WAP"; } } return "-"; }
/** * Init callback server and start XHR if running local app. * * <p>If PhoneGap app is loaded from file://, then we can use XHR otherwise we have to use polling * due to cross-domain security restrictions. * * @param url The URL of the PhoneGap app being loaded */ public void init(String url) { // System.out.println("CallbackServer.start("+url+")"); // Determine if XHR or polling is to be used if ((url != null) && !url.startsWith("file://")) { this.usePolling = true; this.stopServer(); } else if (android.net.Proxy.getDefaultHost() != null) { this.usePolling = true; this.stopServer(); } else { this.usePolling = false; this.startServer(); } }
// 获取网络的状态 public static int getNetWorkStatus(Context context) { int mNetWorkType = 0; ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivityManager != null) { NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { String type = networkInfo.getTypeName(); if (type.equalsIgnoreCase("WIFI")) { mNetWorkType = NETWORKTYPE_WIFI; } else if (type.equalsIgnoreCase("MOBILE")) { String proxyHost = android.net.Proxy.getDefaultHost(); mNetWorkType = isFastMobileNetWork(context); } } } return mNetWorkType; }
/** * is current connected network is cmwap * * @return */ public static boolean isNetworkCmwap(Context ctx) { // 以下为支持cmwap部分,移动和联通的cmwap代理貌似都是10.0.0.172,有待测试 String proxyHost = android.net.Proxy.getDefaultHost(); // 判断网络状况,如果有其它通路的话就不要再使用cmwap的代理 try { if (proxyHost != null && !isNetworkWifi(ctx)) { ConnectivityManager manager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = manager.getActiveNetworkInfo(); String extraInfo = null; if (info != null) extraInfo = info.getExtraInfo(); if (extraInfo != null) { String extraTrim = extraInfo.trim().toLowerCase(); if (extraTrim.equals("cmwap") || extraTrim.equals("uniwap") || extraTrim.equals("ctnet") || extraTrim.equals("ctwap")) return true; } } } catch (Exception e) { } return false; }
@SuppressWarnings("deprecation") private static int getDefaultPort() { return android.net.Proxy.getDefaultPort(); }
@SuppressWarnings("deprecation") private static String getDefaultHost() { return android.net.Proxy.getDefaultHost(); }
public static String getProxyHost(Context context) { String res = Proxy.getHost(context); if (res == null) res = Proxy.getDefaultHost(); return res; }
/** * 检测当前网络是否为wap接入方式 * * @param context * @return true是wap网,false 不是 */ public static boolean isMobileWap(Context context) { String proxyHost = android.net.Proxy.getDefaultHost(); return !TextUtils.isEmpty(proxyHost) && isMobile(context); }
public static int getProxyPort(Context context) { int res = Proxy.getPort(context); if (res == -1) res = Proxy.getDefaultPort(); return res; }