Переход на Apache HTTP Client
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -43,6 +43,11 @@
|
|||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
<version>2.4</version>
|
<version>2.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpclient</artifactId>
|
||||||
|
<version>4.5.2</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package kinosearch.core;
|
package kinosearch.core;
|
||||||
|
|
||||||
import kinosearch.core.browser.Browser;
|
import kinosearch.core.browser.Browser;
|
||||||
import kinosearch.core.browser.SimpleBrowser;
|
import kinosearch.core.browser.ApacheHttpBrowser;
|
||||||
import kinosearch.core.warez.Hdrezka;
|
import kinosearch.core.warez.Hdrezka;
|
||||||
import kinosearch.core.warez.KinoWarez;
|
import kinosearch.core.warez.KinoWarez;
|
||||||
import kinosearch.core.warez.Onlinelife;
|
import kinosearch.core.warez.Onlinelife;
|
||||||
@@ -17,7 +17,7 @@ public class Tools {
|
|||||||
private static Set<KinoWarez> kinoWarezSet;
|
private static Set<KinoWarez> kinoWarezSet;
|
||||||
|
|
||||||
public static Browser createBrowser() {
|
public static Browser createBrowser() {
|
||||||
return new SimpleBrowser();
|
return new ApacheHttpBrowser();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String SafeUrlEncode(String string, String encode) {
|
public static String SafeUrlEncode(String string, String encode) {
|
||||||
|
|||||||
98
src/main/java/kinosearch/core/browser/ApacheHttpBrowser.java
Normal file
98
src/main/java/kinosearch/core/browser/ApacheHttpBrowser.java
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 2017-01-03
|
||||||
|
*/
|
||||||
|
package kinosearch.core.browser;
|
||||||
|
|
||||||
|
import org.apache.http.Header;
|
||||||
|
import org.apache.http.HttpMessage;
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.client.HttpClient;
|
||||||
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.client.methods.HttpPost;
|
||||||
|
import org.apache.http.entity.StringEntity;
|
||||||
|
import org.apache.http.impl.client.HttpClients;
|
||||||
|
import org.apache.http.message.BasicHeader;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class ApacheHttpBrowser implements Browser {
|
||||||
|
private Set<Header> headers = new HashSet<>();
|
||||||
|
private String encoding = "UTF-8";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Browser setHeader(String name, String value) {
|
||||||
|
headers.add(new BasicHeader(name, value));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String get(String url) {
|
||||||
|
HttpClient client = HttpClients.createDefault();
|
||||||
|
HttpGet request = new HttpGet(url);
|
||||||
|
|
||||||
|
setup_headers(request);
|
||||||
|
|
||||||
|
String result = "";
|
||||||
|
try {
|
||||||
|
HttpResponse response = client.execute(request);
|
||||||
|
result = readOutout(response.getEntity().getContent());
|
||||||
|
} catch (IOException ignore) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String post(String url, String data) {
|
||||||
|
HttpClient client = HttpClients.createDefault();
|
||||||
|
HttpPost request = new HttpPost(url);
|
||||||
|
|
||||||
|
setup_headers(request);
|
||||||
|
request.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||||
|
|
||||||
|
String result = "";
|
||||||
|
try {
|
||||||
|
request.setEntity(new StringEntity(data));
|
||||||
|
HttpResponse response = client.execute(request);
|
||||||
|
result = readOutout(response.getEntity().getContent());
|
||||||
|
} catch (IOException ignore) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setEncoding(String encoding) {
|
||||||
|
this.encoding = (encoding == null ? "UTF-8" : encoding);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String readOutout(InputStream inputStream) throws IOException {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
String str;
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, encoding));
|
||||||
|
while ((str = br.readLine()) != null) {
|
||||||
|
sb.append(str).append('\n');
|
||||||
|
}
|
||||||
|
br.close();
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setup_headers(HttpMessage httpMessage) {
|
||||||
|
httpMessage.addHeader("Connection", "close");
|
||||||
|
httpMessage.addHeader("Accept-Encoding", "deflate");
|
||||||
|
httpMessage.addHeader("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 (Header header : headers) {
|
||||||
|
httpMessage.addHeader(header);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@ package core;
|
|||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import kinosearch.core.browser.Browser;
|
import kinosearch.core.browser.Browser;
|
||||||
import kinosearch.core.browser.SimpleBrowser;
|
import kinosearch.core.browser.ApacheHttpBrowser;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ public class TestBrowser {
|
|||||||
private final String header_name = "Mytestheader";
|
private final String header_name = "Mytestheader";
|
||||||
|
|
||||||
private Browser getBrowser() {
|
private Browser getBrowser() {
|
||||||
return new SimpleBrowser();
|
return new ApacheHttpBrowser();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setup_headers(Browser browser) {
|
private void setup_headers(Browser browser) {
|
||||||
|
|||||||
Reference in New Issue
Block a user