您的位置:首页 > 理论基础 > 计算机网络

Android 3G网络下 http refused 解决办法

2013-06-16 21:49 260 查看
public static String openUrl(Context context, String url, String method,
WeiboParameters params, String file, Token token) throws WeiboException {
String result = "";
try {
HttpClient client = getNewHttpClient(context);
HttpUriRequest request = null;
ByteArrayOutputStream bos = null;
if (method.equals("GET")) {
url = url + "?" + encodeUrl(params);
HttpGet get = new HttpGet(url);
request = get;
} else if (method.equals("POST")) {
HttpPost post = new HttpPost(url);
byte[] data = null;
bos = new ByteArrayOutputStream(1024 * 50);
if (!TextUtils.isEmpty(file)) {
Utility.paramToUpload(bos, params);
post.setHeader("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY);
Bitmap bf = BitmapFactory.decodeFile(file);

Utility.imageContentToUpload(bos, bf);

} else {
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
String postParam = encodeParameters(params);
data = postParam.getBytes("UTF-8");
bos.write(data);
}
data = bos.toByteArray();
bos.close();
// UrlEncodedFormEntity entity = getPostParamters(params);
ByteArrayEntity formEntity = new ByteArrayEntity(data);
post.setEntity(formEntity);
request = post;
} else if (method.equals("DELETE")) {
request = new HttpDelete(url);
}
setHeader(method, request, params, url, token);
HttpResponse response = client.execute(request);
StatusLine status = response.getStatusLine();
int statusCode = status.getStatusCode();

if (statusCode != 200) {
result = read(response);
String err = null;
int errCode = 0;
try {
JSONObject json = new JSONObject(result);
err = json.getString("error");
errCode = json.getInt("error_code");
} catch (JSONException e) {
e.printStackTrace();
}
throw new WeiboException(String.format(err), errCode);
}
// parse content stream from response
result = read(response);
return result;
} catch (IOException e) {
throw new WeiboException(e);
}
}

public static HttpClient getNewHttpClient(Context context) {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);

SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

HttpParams params = new BasicHttpParams();

HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);

HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));

ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

// Set the default socket timeout (SO_TIMEOUT) // in
// milliseconds which is the timeout for waiting for data.
HttpConnectionParams.setConnectionTimeout(params, Utility.SET_CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, Utility.SET_SOCKET_TIMEOUT);
HttpClient client = new DefaultHttpClient(ccm, params);
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
// 获取当前正在使用的APN接入点
Uri uri = Uri.parse("content://telephony/carriers/preferapn");
Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);
if (mCursor != null && mCursor.moveToFirst()) {
// 游标移至第一条记录,当然也只有一条
String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
if (proxyStr != null && proxyStr.trim().length() > 0) {
HttpHost proxy = new HttpHost(proxyStr, 80);
client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
}
mCursor.close();
}
}
return client;
} catch (Exception e) {
return new DefaultHttpClient();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 3G APN接入点
相关文章推荐