简体   繁体   中英

java servlet http url request

When I run this code from Java app I get correct response (UTF-8 encoded).

The problem is, when I run it from my servlet, I'm geting:

"פשטות ×”×™× ×”×ª×—×›×•× ×”×ž×•×©×œ× " ×œ× ×•× ×¨×“×• די סר פיירו דה ×•×™× ×¦'×™

Any idea how to fix it?

  URL url;
  HttpURLConnection conn;
  BufferedReader rd;
  String line;
  String result = "";
  try {
     url=new URL("http://www.walla.co.il");
      conn = (HttpURLConnection) url.openConnection();
     conn.setRequestMethod("GET");
     rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
       StringBuffer sb = new StringBuffer("");
            String s1="";
            String NL = System.getProperty("line.separator");
            while ((s1 = rd.readLine()) != null) 
                sb.append(s1+NL);
      System.out.println(sb);
     rd.close();
     return sb.toString();
  } catch (IOException e) {
     e.printStackTrace();
  } catch (Exception e) {
     e.printStackTrace();
  }
return "";

set "JAVA_OPTS=%JAVA_OPTS% -Dfile.encoding=UTF8"

i run this from *.bat file in my tomcat\\bin and it fix the problem seems like i had to set the encode for tomcat/jvm not 100% sure but it works now :)

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