大神们 httpclient偶尔报IO异常 该如何处理 在线等。。。
最近在公司项目上做了一个调用第三方接口的功能,经测试 偶尔会发生IO异常 很不稳定 路过的大神们帮忙看看 哪里需要改进???在线等啊。。。
package tangdi.util.pos;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.sf.json.JSONObject;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import javax.inject.Named;
import sun.misc.BASE64Decoder;
import tangdi.engine.context.Etf;
import tangdi.engine.context.Log;
@SuppressWarnings("deprecation")
public class identity {
@SuppressWarnings( { "unchecked" })
@Named("IdentityValidator")
public static void IdentityValidator(@Named("chlCd") String chlCd,
@Named("MAC") String MAC, @Named("TrDt") String TrDt,
@Named("cardNo") String cardNo, @Named("cardName") String cardName) {
Map params = new HashMap();
params.put("chlcd", chlCd);
params.put("MAC", MAC);
params.put("TrDt", TrDt);
params.put("cardNo", cardNo);
params.put("cardName", cardName);
String smsUrl = "*****************************";
HttpClient httpclient = new DefaultHttpClient(
new ThreadSafeClientConnManager());
httpclient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
60000);
HttpPost httppost = new HttpPost(smsUrl);
String strResult = "";
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
httppost.addHeader("Content-type",
"application/x-www-form-urlencoded");
if (params != null && params.keySet().size() > 0) {
Iterator iterator = params.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Entry) iterator.next();
nameValuePairs.add(new BasicNameValuePair((String) entry
.getKey(), (String) entry.getValue()));
}
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,
Consts.UTF_8));
HttpResponse response = httpclient.execute(httppost);
int status = response.getStatusLine().getStatusCode();
Etf.setChildValue("status", String.valueOf(status));
if (status == 200) {
String conResult = EntityUtils.toString(response.getEntity());
JSONObject sobj = new JSONObject();
sobj = JSONObject.fromObject(conResult);
Log.info("sobj" + sobj, new Object[0]);
String result = sobj.getString("RESULT");
Etf.setChildValue("result", result);
String cardname = sobj.getString("CARDNAME");
String cardno = sobj.getString("CARDNO");
String photo = sobj.getString("PHOTO");
if (result.equals("0")) {
strResult += "身份证号码合法";
long photo_s = System.currentTimeMillis();
Log.info("strResult" + strResult, new Object[0]);
Log.info("result" + result, new Object[0]);
Log.info("cardname" + cardname, new Object[0]);
Log.info("cardno" + cardno, new Object[0]);
Log.info("photo" + photo, new Object[0]);
Log.info("photo_s" + photo_s + cardno, new Object[0]);
Etf.setChildValue("photos", photo_s + cardno);
GenerateImage(photo,
"/usr/local/pos/resin-pro-4.0.25/webapps/posm/upload/identity/"
+ photo_s + cardno + ".jpg");
} else if (result.equals("1")) {
strResult += "姓名与身份证号码不一致";
Log.info("strResult" + strResult, new Object[0]);
} else {
strResult += "无此身份证号码";
Log.info("strResult" + strResult, new Object[0]);
}
} else {
String err = response.getStatusLine().getStatusCode() + "";
strResult += "通讯失败:" + err;
Log.info("strResult" + strResult, new Object[0]);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
Log.info("strResult ClientProtocolException异常" + e.getMessage(),
new Object[0]);
} catch (IOException e) {
e.printStackTrace();
Log
.info("strResult IOException异常" + e.getMessage(),
new Object[0]);
} finally {
httppost.releaseConnection();
httpclient.getConnectionManager().shutdown();
}
}
public static boolean GenerateImage(String imgStr, String imgFilePath) {
if (imgStr == null)
return false;
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] bytes = decoder.decodeBuffer(imgStr);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {
bytes[i] += 256;
}
}
OutputStream out = new FileOutputStream(imgFilePath);
out.write(bytes);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
public static void main(String[] args) {
}
}