首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Apache HttpClient 4.5在big5中发送post请求和get响应

如何使用Apache HttpClient 4.5在big5中发送post请求和get响应
EN

Stack Overflow用户
提问于 2015-07-14 20:51:15
回答 1查看 426关注 0票数 0

它使用Apache HttpClient 4.5在big5中发送post请求编码。Java代码如下所示,结果显示了不可读的代码,如?请给出一些修复它的建议。

代码语言:javascript
复制
   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());
    }
}
EN

回答 1

Stack Overflow用户

发布于 2015-07-15 16:02:58

问题不在您的代码中。我试过了,它工作得很好。它在您的控制台/ Eclipse / IntelliJIdea中,这取决于您从哪里启动它。我修改了你的代码,把它写到一个文件中:

代码语言:javascript
复制
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());
    }

当我在Firefox中打开这个文件时,一切都很好。请在您的系统上试用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31407115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档