简体   繁体   中英

how to send post request and get response in big5 using Apache HttpClient 4.5

It is to send post request encodeing in big5 using Apache HttpClient 4.5. Java code is as follows, and the result shows the unreadable code like ???. Please give some suggestions to fix it.

   hpr803.getResps1("http://web-reg-server.803.org.tw/TRE/stepB1.asp");

  //the method to send post request and get response
  public void getResps1(String param) throws IOException{    
    ArrayList<NameValuePair> pairList = new ArrayList<NameValuePair>();

    // Post request example hospital 803
   pairList.add(new BasicNameValuePair("syear", "104"));
   pairList.add(new BasicNameValuePair("smonth", "7"));
   pairList.add(new BasicNameValuePair("sday", "20"));
   pairList.add(new BasicNameValuePair("eyear", "104"));
   pairList.add(new BasicNameValuePair("emonth", "8"));
   pairList.add(new BasicNameValuePair("eday", "5"));
   pairList.add(new BasicNameValuePair("HospNO", "1"));
   pairList.add(new BasicNameValuePair("SectNO", ""));
   pairList.add(new BasicNameValuePair("EmpNO", ""));

     HttpPost httpPost = new HttpPost(param);
     //big5 code
     StringEntity entity = new StringEntity(URLEncodedUtils.format(pairList, "big5"));
    httpPost.setEntity(entity);

    //httpPost.setEntity(new UrlEncodedFormEntity(pairList, "big5"));
    CloseableHttpClient demo = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(param);
    HttpResponse response = demo.execute(httpGet);
    String responseString = EntityUtils.toString(response.getEntity(), "big5");
    response = demo.execute(httpPost);
    responseString = EntityUtils.toString(response.getEntity());
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        System.out.println("responseString   big5 ~~~~~~~~~~     " +responseString);
    } else {
        System.out.println("response.getStatusLine   ``````````````````````  " +response.getStatusLine());
    }
}

The problem is not in your code. I have tried it and it works fine. It is in your console / Eclipse / IntelliJIdea, depends where you start it. I have modified you code to write in a file:

String responseString = EntityUtils.toString(response.getEntity(), "big5");
    response = demo.execute(httpPost);
    //responseString = EntityUtils.toString(response.getEntity());
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        FileOutputStream fos = new FileOutputStream("C:\\test\\Big5Test.html");
        response.getEntity().writeTo(fos);
        fos.close();
    } else {
        System.out.println("response.getStatusLine   ``````````````````````  " +response.getStatusLine());
    }

When I have open the file in Firefox everything was fine. Please try it on your system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM