java - how to send post request and get response in big5 using Apache HttpClient 4.5 -
it send post request encodeing in big5 using apache httpclient 4.5. java code follows, , result shows unreadable code ???. please give suggestions fix it.
hpr803.getresps1("http://web-reg-server.803.org.tw/tre/stepb1.asp"); //the method send post request , 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 not in code. have tried , works fine. in console / eclipse / intellijidea, depends start it. have modified code write in 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 have open file in firefox fine. please try on system.
Comments
Post a Comment