issue #30: Исключить компонент SimpleBrowser
This commit is contained in:
@@ -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<String, String> 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<String, String> 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user