0
try
{
String xmlReq = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><request_inquiry><partner_id>0999</ partner_id><terminal_type>6012</ terminal_ type><product_code>4001</product _code><date_time>20130715115100</date_time><trx_id>SDFSF11234424ADFA</trx_id><data><cust_id>030913320611</cust_id></data></request_inquiry>";
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
HttpPost httpPost = new HttpPost("202.169.43.53:52056/transaction");
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "text/xml;charset=ISO");
// httpPost.setHeader(HttpHeaders.CONTENT_LENGTH, Integer.toString(xmlReq.length()));
StringEntity se = new StringEntity(xmlReq, ContentType.TEXT_XML);
httpPost.setEntity(se);
System.out.println("Request>>"+httpPost);
StringBuilder html = new StringBuilder("");
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode() != 200) {
InputStream in = httpResponse.getEntity().getContent();
byte b[] = new byte[1024] ;
while(in.read(b) != -1) {
html.append((new String(b)).toString());
b = new byte[1024];
}
System.out.println("Output HTML>> "+html.toString());
}
else{
InputStream in = httpResponse.getEntity().getContent();
byte b[] = new byte[1024] ;
while(in.read(b) != -1) {
html.append((new String(b)).toString());
b = new byte[1024];
}
System.out.println(html);
}
} catch (Exception ex) {
throw new SystemException(Common.ERROR_OTHER, ex.getMessage());
}
}
catch(Exception ex) {
System.out.println("Exception>>"+ex.getMessage());
}
我試過很多方式發送XML請求到服務器。而其中一種方式就是喜歡上面的代碼。我不知道爲什麼會拋出NullException?我的代碼有問題嗎?感謝幫助。XML請求與HttpClient拋出空異常
哪條線投擲的懷疑 –