关于调用全国身份认证返回Server returned HTTP response code: 500错误

首先说一下这问题的由来:由于做微信小程序项目需要做实名认证;所以在阿里云上购买了一个全国身份证认证的API,在本地测试调用API时完全没有问题,然而打包就会报错返回Server returned HTTP response code: 500
先看看实名认证的如何使用

/**
 * 实名认证工具
 * created by hzg on 2018.08.16
 * @version 1.0
 */
public class RealNameAuthenticationUtil {

    private static Logger logger = Logger.getLogger(RealNameAuthenticationUtil.class);

    private final static String appcode = "你购买的api的AppCode";
    private final static String url = "https://1.api.apistore.cn/idcard3";

    /**
     * APISTORE_GET
     * @param strUrl api调用的url
     * @param param  要实名认证的信息 >>>>> param = "realName=张三&cardNo=123"
     * @param appcode 你购买的api的AppCode
     * @return
     */
    public static String API(String strUrl, String param, String appcode, String Method) {
        logger.info("API开始strUrl=" + strUrl + "&¶m= " + param + "&&appcode= " + appcode + "&&Method= " + Method );
        String returnStr = null; // 返回结果定义
        URL url = null;
        HttpURLConnection httpURLConnection = null;
        try {
            logger.info("API的try");
            url = new URL(strUrl + "?" + param);
            logger.info("API的try的url = " + url);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
//            httpURLConnection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            httpURLConnection.setRequestProperty("Authorization", "APPCODE " + appcode);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestMethod(Method);
            httpURLConnection.setUseCaches(false); // 不用缓存
            httpURLConnection.connect();
            logger.info("API的try的httpURLConnection详细 = " + httpURLConnection.getResponseCode());
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
            logger.info("API的try的reader = " + httpURLConnection);
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            reader.close();
            returnStr = buffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("RealNameAuthenticationUtil  >>>>>  API  --->>>> Exception");
            logger.error(e.getMessage(),e);
            return null;
        } finally {
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
            }
        }
        logger.info("API的正常结束returnStr=" + returnStr);
        return returnStr;
    }
}

我个人在本地运行测试的时候是正常认证没有报错,然而在使用Maven打包工具打包后就会保如下错误,个人还没知道这是什么具体原因,以后发现原因会进行补充

2018-08-29 09:58:06,354  INFO RealNameAuthenticationUtil:74 - API的Exception结束java.io.IOException: Server returned HTTP response code: 500 for URL: https://1.api.apistore.cn/idcard3?realName=张三&cardNo=123456

对于上面的错误是因为使用阿里云的api其实他们处理的一般是utf-8编码的,然而在这里realName=张三不符合utf-8编码导致的
所以在进行认真的时候对realName以及cardNo进行编码在调api认证

String params = "realName="+java.net.URLEncoder.encode(realName,"UTF-8")+"&cardNo="+java.net.URLEncoder.encode(cardNo,"UTF-8");

在这里连cardNo都编码是因为有部分的身份证号码后面的会是x之类的,以防万一

你可能感兴趣的:(项目问题)