首页 新闻 搜索 专区 学院

请大神帮忙 post向https接口发送数据 部署到服务器为什么运行一会后就会报空指针

0
悬赏园豆:200 [已解决问题] 解决于 2021-01-26 16:13

post向https接口发送数据 部署到服务器刚开始运行正常为什么运行一会后就会报空指针

BufferedReader in = null;
StringBuilder result = new StringBuilder();
HttpsURLConnection conn = null;
ResourceBundle resource = ResourceBundle.getBundle("com/ismp/test");
String key = resource.getString("url");
String appId = resource.getString("appId");
String secret = resource.getString("secret");
ServiceOrderCT ct = serviceOrder.getServiceOrderCT();
Product product = productInstance.getProduct();
String synctime1 = df.format(new Date());
String synctime = df2.format(new Date());
String be = df.format(productInstance.getCreateDate());
String en = df.format(productInstance.getEndDate());
try {
serviceOrder.getOrg().getId();
OrderInfo orderInfo = new OrderInfo();
orderInfo.setTimeStamp(synctime);
orderInfo.setTransactionID(String.valueOf(ct.getId()));
orderInfo.setActionType(String.valueOf(actiontype));
UserInfo userInfo = new UserInfo();
userInfo.setAccNbr(productInstance.getAccountNumber());
userInfo.setUserId(String.valueOf(userinst.getId()));
userInfo.setLoginName(ct.getUserName());
userInfo.setPassword(PasswordHelper.encrypt(ct.getPassword()));
userInfo.setCustId(Long.valueOf(productInstance.getOrg().getId()));
userInfo.setCustLoginName(ct.getUserName());
userInfo.setCustName(ct.getCustomerName());
userInfo.setRealName(ct.getCustomerName());
userInfo.setSex("");
ProdInfo prodInfo = new ProdInfo();
prodInfo.setCustId(String.valueOf(serviceOrder.getOrg().getId()));
prodInfo.setProdInstId(Long.valueOf(productInstance.getId()));
prodInfo.setProductId(Integer.valueOf((int)product.getId()));
prodInfo.setProductCode(product.getCode());
prodInfo.setStartDate(be);
prodInfo.setEndDate(en);
prodInfo.setAttributes(ct.getProperties());
userInfo.setProdInfo(prodInfo);
orderInfo.setUserInfo(userInfo);
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
String sign = SecuritySHA1Utils.shaEncode(gson.toJson(orderInfo)+"&"+appId+"&"+secret+"&"+synctime1);
System.out.println(gson.toJson(orderInfo)+"&"+appId+"&"+secret+"&"+synctime1);
System.out.println(gson.toJson(orderInfo));
System.out.println(sign);
URL url = new URL(key);
conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(new TLSSocketConnectionFactory());
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
//发送POST请求必须设置为true
conn.setDoOutput(true);
conn.setDoInput(true);
//设置连接超时时间和读取超时时间
conn.setConnectTimeout(30000);
conn.setReadTimeout(10000);
conn.setRequestProperty("appId", appId);
conn.setRequestProperty("timestamp", synctime1);
conn.setRequestProperty("sign", sign);
conn.setRequestProperty("Content-Type", "application/json");

        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
        out.write(gson.toJson(orderInfo));
        out.flush();
        out.close();

java.lang.NullPointerException
at sun.net.www.protocol.https.HttpsClient.checkURLSpoofing(HttpsClient.java:604)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:573)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1283)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1258)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
at com.ismp.group.client.SyncProductNewlh.restNewlh(SyncProductNewlh.java:120)
at com.ismp.service.order.impl.ServiceOrderService.callBackSI(ServiceOrderService.java:3060)
at com.ismp.service.order.impl.ServiceOrderService$$FastClassByCGLIB$$de2232bb.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)
at com.ismp.service.order.impl.ServiceOrderService$$EnhancerByCGLIB$$9c14c04.callBackSI(<generated>)
at com.ismp.web.servlet.ApIsmpReportServlet.doApReport(ApIsmpReportServlet.java:257)
at com.ismp.web.servlet.ApIsmpReportServlet.doPost(ApIsmpReportServlet.java:164)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)

具体就是这一句
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
实在不知道什么原因 本地也复现不出来问题 请大伙们帮忙瞅一眼

花甲粉丝丶的主页 花甲粉丝丶 | 初学一级 | 园豆:22
提问于:2021-01-22 15:55
< >
分享
最佳答案
0

你应该封装一个方法专门用来post,然后再去调用这个方法

OrderInfo orderInfo = new OrderInfo();
orderInfo.set各种数据

String postUrl=“api地址”;
String postData=把orderInfo转换为json字符串。
String result = HttpRequestUtil.sendPost(postUrl, postData);

以下是HttpRequestUtil.java代码:

public class HttpRequestUtil {
public static String sendPost(String url, String postData) {
String data = null;
try {
URL dataUrl = new URL(url);
HttpURLConnection con = (HttpURLConnection) dataUrl.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-type", "application/json");
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);

        OutputStream os = con.getOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        if (postData != null) {
            dos.write(postData.getBytes("UTF-8"));
        }
        dos.flush();
        dos.close();

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
        StringBuffer buffer = new StringBuffer();
        String line;
        while ((line = in.readLine()) != null) {
            buffer.append(line);
        }
        data = buffer.toString();
        con.disconnect();
    } catch (Exception ex) {
    	LogerHelper.WebLog(ex);
    }
    return data;
}


public static String sendGet(String url,String charsetName) {
    String data = null;
    try {
        URL dataUrl = new URL(url);
        HttpURLConnection con = (HttpURLConnection) dataUrl.openConnection();
        con.setRequestMethod("GET");
        con.setDoInput(true);
        con.setUseCaches(false);
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), charsetName));
        StringBuffer buffer = new StringBuffer();
        String line;
        while ((line = in.readLine()) != null) {
            buffer.append(line);
        }
        data = buffer.toString();
        con.disconnect();
    } catch (Exception ex) {
    	LogerHelper.WebLog(ex);
    }
    return data;
}

public static String sendGet(String url) {
    return sendGet(url,"UTF-8");
}

}

收获园豆:150
为乐而来 | 小虾三级 |园豆:1081 | 2021-01-22 17:21
其他回答(1)
0

OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
如果是这一句,那么就是 conn 为空了,可以考虑一下链接断开的问题。楼上想法是对的单独开辟一个方法调用,每次调用完毕即关闭

收获园豆:50
南小鸟 | 园豆:618 (小虾三级) | 2021-01-22 17:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册