browser update
This commit is contained in:
18
pom.xml
18
pom.xml
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
<groupId>ru.dmitriymx</groupId>
|
<groupId>ru.dmitriymx</groupId>
|
||||||
<artifactId>vkapiuni</artifactId>
|
<artifactId>vkapiuni</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.1-SNAPSHOT</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
@@ -30,6 +30,22 @@
|
|||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
<version>2.8.0</version>
|
<version>2.8.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-io</groupId>
|
||||||
|
<artifactId>commons-io</artifactId>
|
||||||
|
<version>2.5</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpclient</artifactId>
|
||||||
|
<version>4.5.2</version>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>commons-logging</groupId>
|
||||||
|
<artifactId>commons-logging</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import com.google.gson.Gson;
|
|||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import ru.dmitriymx.vkapi.browser.Browser;
|
||||||
|
import ru.dmitriymx.vkapi.browser.Response;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|||||||
75
src/main/java/ru/dmitriymx/vkapi/browser/ApacheBroswe.java
Normal file
75
src/main/java/ru/dmitriymx/vkapi/browser/ApacheBroswe.java
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
|
* 2017-04-10
|
||||||
|
*/
|
||||||
|
package ru.dmitriymx.vkapi.browser;
|
||||||
|
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
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.methods.HttpGet;
|
||||||
|
import org.apache.http.client.methods.HttpPost;
|
||||||
|
import org.apache.http.client.methods.HttpRequestBase;
|
||||||
|
import org.apache.http.entity.StringEntity;
|
||||||
|
import org.apache.http.impl.client.HttpClients;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
public class ApacheBroswe implements Browser {
|
||||||
|
private final HttpClient client = HttpClients.createDefault();
|
||||||
|
private String userAgent = "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";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setUserAgent(String userAgent) {
|
||||||
|
this.userAgent = userAgent;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Response get(String url) throws IOException {
|
||||||
|
return request(new HttpGet(url));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Response post(String url, String data) throws IOException {
|
||||||
|
HttpPost requestPost = new HttpPost(url);
|
||||||
|
requestPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||||
|
if (data != null) {
|
||||||
|
requestPost.setEntity(new StringEntity(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
return request(requestPost);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Response request(HttpRequestBase request) throws IOException {
|
||||||
|
setup_headers(request);
|
||||||
|
HttpResponse response = client.execute(request);
|
||||||
|
|
||||||
|
try (InputStream streamContent = response.getEntity().getContent()) {
|
||||||
|
String contentRaw = IOUtils.toString(streamContent, "UTF-8");
|
||||||
|
String contentType = "text/plain";
|
||||||
|
for (Header header : response.getAllHeaders()) {
|
||||||
|
if (header.getName().equalsIgnoreCase("Content-Type")) {
|
||||||
|
contentType = header.getValue();
|
||||||
|
if (contentType.contains(";")) {
|
||||||
|
contentType = contentType.split(";")[0];
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ApacheResponse(
|
||||||
|
response.getStatusLine().getStatusCode(),
|
||||||
|
contentType, contentRaw
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setup_headers(HttpMessage request) {
|
||||||
|
request.addHeader("Connection", "close");
|
||||||
|
request.addHeader("Accept-Encoding", "deflate");
|
||||||
|
request.addHeader("User-Agent", userAgent);
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/main/java/ru/dmitriymx/vkapi/browser/ApacheResponse.java
Normal file
32
src/main/java/ru/dmitriymx/vkapi/browser/ApacheResponse.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
|
* 2017-04-10
|
||||||
|
*/
|
||||||
|
package ru.dmitriymx.vkapi.browser;
|
||||||
|
|
||||||
|
public class ApacheResponse implements Response {
|
||||||
|
private final int code;
|
||||||
|
private final String contentType;
|
||||||
|
private final String content;
|
||||||
|
|
||||||
|
ApacheResponse(int code, String contentType, String content) {
|
||||||
|
this.code = code;
|
||||||
|
this.contentType = contentType;
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getStatus() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getContentType() {
|
||||||
|
return contentType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,11 +2,12 @@
|
|||||||
* DmitriyMX <mail@dmitriymx.ru>
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
* 2017-04-10
|
* 2017-04-10
|
||||||
*/
|
*/
|
||||||
package ru.dmitriymx.vkapi;
|
package ru.dmitriymx.vkapi.browser;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public interface Browser {
|
public interface Browser {
|
||||||
|
void setUserAgent(String userAgent);
|
||||||
Response get(String url) throws IOException;
|
Response get(String url) throws IOException;
|
||||||
Response post(String url, String data) throws IOException;
|
Response post(String url, String data) throws IOException;
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
* DmitriyMX <mail@dmitriymx.ru>
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
* 2017-04-10
|
* 2017-04-10
|
||||||
*/
|
*/
|
||||||
package ru.dmitriymx.vkapi;
|
package ru.dmitriymx.vkapi.browser;
|
||||||
|
|
||||||
public interface Response {
|
public interface Response {
|
||||||
int getStatus();
|
int getStatus();
|
||||||
Reference in New Issue
Block a user