2015年7月13日
package com.zhihe.xqsh.utils;
import java.io.file;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.io.unsupportedencodingexception;
import java.util.date;
import java.util.list;
import org.apache.http.httpentity;
import org.apache.http.httpresponse;
import org.apache.http.httpstatus;
import org.apache.http.httpversion;
import org.apache.http.namevaluepair;
import org.apache.http.client.clientprotocolexception;
import org.apache.http.client.httpclient;
import org.apache.http.client.entity.urlencodedformentity;
import org.apache.http.client.methods.httpget;
import org.apache.http.client.methods.httppost;
import org.apache.http.conn.clientconnectionmanager;
import org.apache.http.conn.params.connmanagerparams;
import org.apache.http.conn.params.connrouteparams;
import org.apache.http.conn.scheme.plainsocketfactory;
import org.apache.http.conn.scheme.scheme;
import org.apache.http.conn.scheme.schemeregistry;
import org.apache.http.conn.ssl.sslsocketfactory;
import org.apache.http.impl.client.defaulthttpclient;
import org.apache.http.impl.conn.tsccm.threadsafeclientconnmanager;
import org.apache.http.impl.cookie.basicclientcookie;
import org.apache.http.params.basichttpparams;
import org.apache.http.params.httpconnectionparams;
import org.apache.http.params.httpparams;
import org.apache.http.params.httpprotocolparams;
import org.apache.http.util.entityutils;
import com.zhihe.xqsh.network.servererrorexception;
import android.accounts.networkerrorexception;
import android.annotation.suppresslint;
import android.util.log;
public class customerhttpclient {
private static final string tag = customerhttpclient.class.getsimplename();
private static defaulthttpclient customerhttpclient;
private customerhttpclient() {
}
public static synchronized httpclient gethttpclient() {
if (null == customerhttpclient) {
httpparams params = new basichttpparams();
// 设置�?��基本参数
httpprotocolparams.setversion(params, httpversion.http_1_1);
httpprotocolparams.setcontentcharset(params, "utf-8");
httpprotocolparams.setuseexpectcontinue(params, true);
httpprotocolparams.setuseragent(params, "mozilla/5.0(linux;u;android 2.2.1;en-us;nexus one build.frg83) "
"applewebkit/553.1(khtml,like gecko) version/4.0 mobile safari/533.1");
// 超时设置
/* 从连接池中取连接的超时时�?*/
connmanagerparams.settimeout(params, 2000);
connmanagerparams.setmaxtotalconnections(params, 800);
/* 连接超时 */
httpconnectionparams.setconnectiontimeout(params, 5000);
/* 请求超时 */
httpconnectionparams.setsotimeout(params, 10000);
// 设置我们的httpclient支持http和https两种模式
schemeregistry schreg = new schemeregistry();
schreg.register(new scheme("http", plainsocketfactory.getsocketfactory(), 80));
schreg.register(new scheme("https", sslsocketfactory.getsocketfactory(), 443));
// 使用线程安全的连接管理来创建httpclient
clientconnectionmanager conmgr = new threadsafeclientconnmanager(params, schreg);
// �?��连接数:connmanagerparams.setmaxtotalconnections(params, 50);
customerhttpclient = new defaulthttpclient(conmgr, params);
}
return customerhttpclient;
}
/**
* 以get方式提交数据
*
* @param url 提交地址
* @param params 参数
* @return 响应结果
* @throws servererrorexception 请求失败
* @throws networkerrorexception 连接失败
*/
public static string get(string url, string params) throws servererrorexception, networkerrorexception {
int trytimes = 0;
nullpointerexception ex;
do {
try {
return tryget(url, params);
} catch (nullpointerexception e) {
ex = e;
trytimes ;
}
} while (trytimes < 3);
throw ex;
}
/**
* 以get方式提交数据
*
* @param url 提交地址
* @param params 参数
* @return 响应结果
* @throws servererrorexception 请求失败
* @throws networkerrorexception 连接失败
*/
public static string tryget(string url, string params) throws servererrorexception, networkerrorexception {
try {
httpget request = new httpget(url params);
/*if (lotteryapplication.iscmwap()) {
org.apache.http.httphost proxy = new org.apache.http.httphost("10.0.0.172", 80, "http");
httpparams httpparams = new basichttpparams();
connrouteparams.setdefaultproxy(httpparams, proxy);
request.setparams(httpparams);
}*/
httpclient client = gethttpclient();
httpresponse response = client.execute(request);
if (response.getstatusline().getstatuscode() != httpstatus.sc_ok) {
throw new servererrorexception("��������æ�����ժ�����");
}
httpentity resentity = response.getentity();
string result = (resentity == null) ? null : entityutils.tostring(resentity, "utf-8");
return result;
} catch (unsupportedencodingexception e) {
logw(e.getmessage());
return null;
} catch (clientprotocolexception e) {
logw(e.getmessage());
return null;
} catch (ioexception e) {
throw new networkerrorexception("���ӳ��ɹ���������������", e);
}
}
private static void logw(string string) {
if (string != null) {
log.w(tag, string);
}
}
/**
* 以post方式提交数据
*
* @param url 提交地址
* @param params 参数
* @return 响应结果
* @throws servererrorexception 请求失败
* @throws networkerrorexception 连接失败
*/
public static string post(string url, list params) throws servererrorexception, networkerrorexception {
return post(url, params, null);
}
/**
* 以post方式提交数据
*
* @param url 提交地址
* @param params 参数
* @param sotimeout 响应超时时间,单位毫�?
* @return 响应结果
* @throws servererrorexception 请求失败
* @throws networkerrorexception 连接失败
*/
public static string post(string url, list params, int sotimeout) throws servererrorexception,
networkerrorexception {
httpparams httpparams;
if (sotimeout <= 0) {
httpparams = null;
} else {
httpparams = new basichttpparams();
httpconnectionparams.setsotimeout(httpparams, sotimeout);
}
return post(url, params, httpparams);
}
/**
* 以post方式提交数据
*
* @param url 提交地址
* @param params 参数
* @param httpparams http参数
* @return 响应结果
* @throws servererrorexception 请求失败
* @throws networkerrorexception 连接失败
*/
public static string post(string url, list params, httpparams httpparams) throws servererrorexception,
networkerrorexception {
int trytimes = 0;
nullpointerexception ex;
do {
try {
return trypost(url, params, httpparams);
} catch (nullpointerexception e) {
ex = e;
trytimes ;
}
} while (trytimes < 3);
throw ex;
}
/**
* 以post方式提交数据
*
* @param url 提交地址
* @param params 参数
* @param httpparams http参数
* @return 响应结果
* @throws servererrorexception 请求失败
* @throws networkerrorexception 连接失败
*/
public static string trypost(string url, list params, httpparams httpparams) throws servererrorexception,
networkerrorexception {
try {
httppost request = new httppost(url);
if (params != null && params.size() > 0) {
request.setentity(new urlencodedformentity(params, "utf-8"));
}
//if (lotteryapplication.iscmwap()) {
//org.apache.http.httphost proxy = new org.apache.http.httphost("10.0.0.172", 80, "http");
//if (httpparams == null)
//httpparams = new basichttpparams();
//connrouteparams.setdefaultproxy(httpparams, proxy);
//}
if (httpparams != null)
request.setparams(httpparams);
//log.v("cs", params.tostring());
httpclient client = gethttpclient();
httpresponse response = client.execute(request);
if (response.getstatusline().getstatuscode() != httpstatus.sc_ok) {
//log.v("cs", params.tostring());
//log.v("cs", response.getstatusline().getstatuscode() "");
request.abort();
throw new servererrorexception("��������æ�����ժ�����");
}
if (response.getstatusline ().getstatuscode () != 200) {
request.abort(); //�ж�����,���������կ�ʼ��һ������
return null;
}
httpentity resentity = response.getentity();
string result = (resentity == null) ? null : entityutils.tostring(resentity, "utf-8");
//log.v("cs", params.tostring() "||||" result);
return result;
} catch (unsupportedencodingexception e) {
logw(e.getmessage());
return null;
} catch (clientprotocolexception e) {
logw(e.getmessage());
return null;
} catch (ioexception e) {
throw new networkerrorexception(e.getmessage(), e);
//throw new networkerrorexception("连接不成功,请检查网络设�?, e);
}
}
@suppresslint("sdcardpath")
public static string download(string url) throws servererrorexception, networkerrorexception {
try {
//log.i("http-download", url);
httppost request = new httppost(url);
httpclient client = gethttpclient();
httpresponse response = client.execute(request);
if (response.getstatusline().getstatuscode() != httpstatus.sc_ok) {
throw new servererrorexception("��������æ�����ժ�����");
}
httpentity entity = response.getentity();
inputstream is = entity.getcontent();
if (is == null)
throw new servererrorexception("stream is null ");
string fileext = url.substring(url.lastindexof(".") 1, url.length()).tolowercase();
string filename = url.substring(url.lastindexof("/") 1, url.lastindexof("."));
file tempfile = new file("/sdcard/" filename "." fileext);
if (!tempfile.exists())
tempfile.createnewfile();
fileoutputstream fileoutputstream = new fileoutputstream(tempfile);
byte[] buf = new byte[1024];
int ch;
while ((ch = is.read(buf)) != -1) {
fileoutputstream.write(buf, 0, ch);
}
fileoutputstream.flush();
fileoutputstream.close();
return tempfile.getabsolutepath();
} catch (unsupportedencodingexception e) {
logw(e.getmessage());
return null;
} catch (clientprotocolexception e) {
logw(e.getmessage());
return null;
} catch (ioexception e) {
throw new networkerrorexception(e.getmessage(), e);
}
}
/**
* 清空cookie
*/
public static void clearcookie() {
if (customerhttpclient != null)
customerhttpclient.getcookiestore().clear();
}
/**
* 清除指定cookie
*
* @param name cookie名称
*/
public static void clearcookie(string name) {
if (customerhttpclient == null)
return;
basicclientcookie expiredcookie = new basicclientcookie(name, "null");
expiredcookie.setexpirydate(new date(system.currenttimemillis() - 1000));
customerhttpclient.getcookiestore().addcookie(expiredcookie);
}
}
posted @ terry zou 阅读(247) | |
http://yunpan.cn/ccdbtgqaya4u7
posted @ terry zou 阅读(127) | |