From 329fb4a2899572098ac27baf60d757d25da118b8 Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Wed, 13 Dec 2017 21:55:56 +0300 Subject: [PATCH] =?UTF-8?q?issue=20#30:=20=D0=98=D1=81=D0=BA=D0=BB=D1=8E?= =?UTF-8?q?=D1=87=D0=B8=D1=82=D1=8C=20=D0=BA=D0=BE=D0=BC=D0=BF=D0=BE=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D1=82=20SimpleBrowser?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/browser/SimpleBrowser.java | 99 ------------------- 1 file changed, 99 deletions(-) delete mode 100644 src/main/java/kinosearch/core/browser/SimpleBrowser.java diff --git a/src/main/java/kinosearch/core/browser/SimpleBrowser.java b/src/main/java/kinosearch/core/browser/SimpleBrowser.java deleted file mode 100644 index 0d8077f..0000000 --- a/src/main/java/kinosearch/core/browser/SimpleBrowser.java +++ /dev/null @@ -1,99 +0,0 @@ -package kinosearch.core.browser; - -import java.io.*; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.HashMap; -import java.util.Map; - -public class SimpleBrowser implements Browser { - private Map headers = new HashMap<>(); - private String encoding = "UTF-8"; - - @Override - public Browser setHeader(String name, String value) { - headers.put(name, value); - return this; - } - - @Override - public String get(String url) { - String result = ""; - try { - HttpURLConnection connection = create_connection(url); - connection.setRequestMethod("GET"); - - if (connection.getResponseCode() != 200) { - return result; - } - - result = readOutput(connection.getInputStream()); - connection.disconnect(); - } catch (IOException ignore) { - // ignore - } - - return result; - } - - @Override - public String post(String url, String data) { - String result = ""; - try { - HttpURLConnection connection = create_connection(url); - connection.setRequestMethod("POST"); - connection.setRequestProperty("Content-Length", String.valueOf(data.length())); - - connection.setDoOutput(true); - DataOutputStream dos = new DataOutputStream(connection.getOutputStream()); - dos.writeBytes(data); - dos.flush(); - dos.close(); - - if (connection.getResponseCode() != 200) { - return ""; - } - - result = readOutput(connection.getInputStream()); - connection.disconnect(); - } catch (IOException ignore) { - // ignore - } - - return result; - } - - @Override - public void setEncoding(String encoding) { - this.encoding = encoding; - } - - private HttpURLConnection create_connection(String strUrl) throws IOException { - URL url = new URL(strUrl); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - - connection.setUseCaches(false); //TODO а надо ли? - - connection.setRequestProperty("Connection", "close"); - connection.setRequestProperty("Accept-Encoding", "deflate"); - connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Linux; Android 4.2.2; GT-I9505 Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36"); - - for (Map.Entry entry : headers.entrySet()) { - connection.setRequestProperty(entry.getKey(), entry.getValue()); - } - - return connection; - } - - private String readOutput(InputStream in) throws IOException { - StringBuilder sb = new StringBuilder(); - String str; - BufferedReader br = new BufferedReader(new InputStreamReader(in, encoding)); - while ((str = br.readLine()) != null) { - sb.append(str).append('\n'); - } - br.close(); - - return sb.toString(); - } -}