java servlet http url请求

时间:2015-01-28 21:26:47

标签: java servlets

当我从Java应用程序运行此代码时,我得到正确的响应(UTF-8编码)。

问题是,当我从我的servlet运行它时,我正在试图:

  

“פש×〜×•×ª×”×™× ×“×ª× - ×>×•× ×”×ž×•×©×œ×   ×œ× ו×רדוהיסרפיירודהוי××|'×™

知道怎么解决吗?

  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 "";

1 个答案:

答案 0 :(得分:0)

设置“JAVA_OPTS =%JAVA_OPTS%-Dfile.encoding = UTF8”

我从我的tomcat \ bin中的* .bat文件中运行它 它解决了问题似乎我必须为tomcat / jvm设置编码 不是100%肯定,但它现在有效:)