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 リクエストをサーバーに送信する方法はたくさん試しました。方法の 1 つは、上記のコードのように見えることです。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://またはのようなプロトコルがないためです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();
}

注: 例外をログに記録する場合は、スタック トレースもログに記録してください。これにより、例外の原因となったクラス、メソッド、および行など、例外に関する詳細が得られます。

于 2013-07-16T03:05:22.850 に答える