路过的大神帮忙看看HttpURLConnection异常

xiehui_top 2013-09-13 10:04:32
最近要在公司系统上做一个连接第三方接口进行身份证验证的功能 于是写了下面一个类
首先说明的是能验证成功 但是偶尔会发生405 not allowed 或者连接失败,数据接受错误 大神们看看有什么不合理的地方 为什么会发生405和连接失败呢???
package tangdi.util.pos;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import javax.inject.Named;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import sun.misc.BASE64Decoder;
import tangdi.engine.context.Etf;
import tangdi.engine.context.Log;

public class identity
{
public static final int RESPCODE_SUCCESS = 200;

@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)
{
String url = "http://user.icardpay.com/hk-frt-sys-web/F10041.front?chlcd=" +
chlCd +
"&MAC=" +
MAC +
"&TrDt=" +
TrDt +
"&cardNo=" +
cardNo +
"&cardName=" + cardName;
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(
60000);
client.getHttpConnectionManager().getParams().setSoTimeout(60000);
String message = "";
HttpURLConnection urlCon = openConnection(url);
System.out.println(urlCon);
try {
urlCon.setRequestMethod("POST");
Log.info("message:urlCon.setRequestMethod(POST);", new Object[0]);
urlCon.setDoOutput(true);
Log.info("message:urlCon.setDoOutput(true);", new Object[0]);
urlCon.setDoInput(true);
Log.info("message:urlCon.setDoInput(true);", new Object[0]);
receiveData(urlCon, url);
} catch (IOException ioe) {
message = "数据处理超时";
Log.info("message" + message + ioe.getMessage(), new Object[0]);
} catch (Exception e) {
message = "数据处理失败";
Log.info("cardno" + message, new Object[0]);
} finally {
urlCon.disconnect();
}
}

private static void receiveData(HttpURLConnection urlCon, String url) {
Log.info("message:receiveData();", new Object[0]);
String strResult = "";
try {
int respCode = urlCon.getResponseCode();
System.out.println("请求返回码:" + respCode);
Etf.setChildValue("status", String.valueOf(respCode));
Log.info("status" + String.valueOf(respCode), new Object[0]);
if (200 == respCode) {
InputStream is = urlCon.getInputStream();

BufferedReader br = new BufferedReader(
new InputStreamReader(is, "UTF-8"));
String line = null;
String resultDate = "";
while ((line = br.readLine()) != null) {
resultDate = resultDate + line;
}
JSONObject sobj = new JSONObject();
sobj = JSONObject.fromObject(resultDate);
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 = 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 = strResult + "姓名与身份证号码不一致";
Log.info("strResult" + strResult, new Object[0]);
} else {
strResult = strResult + "无此身份证号码";
Log.info("strResult" + strResult, new Object[0]);
}
} else {
String respMsg = urlCon.getResponseMessage();
strResult = MessageFormat.format(
"url:{0},resCode:{1}:resMsg:{2}。", new Object[] { url,
Integer.valueOf(respCode), respMsg });
System.out.println();
Log.info("strResult" + strResult, new Object[0]);
}
} catch (IOException e) {
strResult = "连接失败,数据接收错误";
Log.info("strResult" + strResult, new Object[0]);
} catch (Throwable e) {
strResult = "系统处理异常";
Log.info("strResult" + strResult, new Object[0]);
}
}

private static HttpURLConnection openConnection(String url) {
try {
return (HttpURLConnection)new URL(url).openConnection();
} catch (MalformedURLException mue) {
System.out.println("连接失败,无法连接对方主机:{}");
} catch (IOException ioe) {
System.out.println("连接失败通讯建链失败:{}");
}
return null;
}

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) {
int tmp36_34 = i;
byte[] tmp36_33 = bytes;
tmp36_33[tmp36_34] = (byte)(tmp36_33[tmp36_34] + 256);
}
}
OutputStream out = new FileOutputStream(imgFilePath);
out.write(bytes);
out.flush();
out.close();
return true;
} catch (Exception localException) {
}
return false;
}

public static void main(String[] args)
{
}
}
...全文
139 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
无聊找乐 2013-09-15
  • 打赏
  • 举报
回复
没有代码,没有异常log,没有请求的地址,你叫人家怎么判断啊。 根据异常的名字我只能告诉你 你的http协议中发送了一个错误的信号。 Class ClientProtocolException java.lang.Object java.lang.Throwable java.lang.Exception java.io.IOException org.apache.http.client.ClientProtocolException All Implemented Interfaces: Serializable Direct Known Subclasses: HttpResponseException @Immutable public class ClientProtocolException extends IOException Signals an error in the HTTP protocol. Since: 4.0 See Also: Serialized Form 详见:http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/ClientProtocolException.html
xiehui_top 2013-09-15
  • 打赏
  • 举报
回复
引用 2 楼 rainbowsix 的回复:
apache有个httpClient 的包, 用那个写会容易很多。 还是用那个吧
我用httpclient试了一下 ClientProtocolException 这个异常是怎么回事儿啊?
wyx100 2013-09-14
  • 打赏
  • 举报
回复
回复于: 2013-09-14 11:29:58 apache有个httpClient 的包, 用那个写会容易很多。
无聊找乐 2013-09-14
  • 打赏
  • 举报
回复
apache有个httpClient 的包, 用那个写会容易很多。 还是用那个吧
失落夏天 2013-09-14
  • 打赏
  • 举报
回复
是不是静态页面接收了post请求了? 你倒是发几个Log信息看一下啊

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧