2013-07-16 128 views
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拋出空異常

+2

哪條線投擲的懷疑 –

回答

0

實際的例外是在該行

HttpPost httpPost = new HttpPost("202.169.43.53:52056/transaction"); 

java.lang.IllegalArgumentException 
    at java.net.URI.create(URI.java:841) 
    at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:76) 
    at Test.main(Test.java:22) 
Caused by: java.net.URISyntaxException: Illegal character in scheme name at index 0: 202.169.43.53:52056/transaction 
    at java.net.URI$Parser.fail(URI.java:2810) 
    at java.net.URI$Parser.checkChars(URI.java:2983) 
    at java.net.URI$Parser.checkChar(URI.java:2993) 
    at java.net.URI$Parser.parse(URI.java:3009) 
    at java.net.URI.<init>(URI.java:577) 
    at java.net.URI.create(URI.java:839) 
    ... 2 more 

這是因爲URI缺少像http://https://

防爆協議:

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, 30); 
    httpClient.getParams().setParameter(
      CoreConnectionPNames.SO_TIMEOUT, 30); 
    HttpPost httpPost = new HttpPost("http://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(""); 
    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) { 
    ex.printStackTrace(); 
} 

注意:當你正在記錄異常時,確保你也記錄了堆棧跟蹤,因爲它會給你更多關於異常的細節,比如哪個類,方法和行導致異常。