Advertisement
Kosheen

getURL function

May 4th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. public String getURL(String url) {
  2.             String result = "";
  3.             try {
  4.                 URL u = new URL(url);
  5.                 // создаём соединение
  6.                 HttpURLConnection urlConnection = (HttpURLConnection) u.openConnection();
  7.                
  8.                 String data = "format=plain&lang=en-ru";
  9.                 String key = "YOUR_KEY_HERE";
  10.                 String text_to_translate = "Hello & Goodbye";
  11.  
  12.                 text_to_translate = URLEncoder.encode(text_to_translate, "UTF-8");
  13.                 // нужно добавить в к строке data поля key, text_to_translate
  14.                
  15.                 urlConnection.setDoOutput(true); // setting POST method
  16.                 OutputStream out = urlConnection.getOutputStream();
  17.                 out.write(data.getBytes());
  18.                
  19.                 // создаём потом ввода и читаем ответ сервера
  20.                 InputStream in = urlConnection.getInputStream();
  21.                 Scanner sc = new Scanner(in);
  22.                 // 2) считать содержимое ответа от сервера
  23.                 // записать в переменную result
  24.                 urlConnection.disconnect();
  25.  
  26.             } catch (IOException e ) {}
  27.             return result;
  28.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement