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");
实在不知道什么原因 本地也复现不出来问题 请大伙们帮忙瞅一眼
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
如果是这一句,那么就是 conn 为空了,可以考虑一下链接断开的问题。楼上想法是对的单独开辟一个方法调用,每次调用完毕即关闭
你应该封装一个方法专门用来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);
}