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?我的代码有问题吗?感谢帮助。

4

1 回答 1

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://orhttps://

前任:

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();
}

注意:当您记录异常时,请确保您也记录堆栈跟踪,因为它会为您提供有关异常的更多详细信息,例如哪个类、方法和行导致了异常。

于 2013-07-16T03:05:22.850 回答