Archived
0

browser update

This commit is contained in:
2017-04-11 00:04:59 +03:00
parent 2b21c914f9
commit 42afbabd30
6 changed files with 129 additions and 3 deletions

18
pom.xml
View File

@@ -8,7 +8,7 @@
<groupId>ru.dmitriymx</groupId>
<artifactId>vkapiuni</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -30,6 +30,22 @@
<artifactId>gson</artifactId>
<version>2.8.0</version>
</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>
<build>

View File

@@ -8,6 +8,8 @@ import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.dmitriymx.vkapi.browser.Browser;
import ru.dmitriymx.vkapi.browser.Response;
import java.io.IOException;
import java.util.Map;

View 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);
}
}

View 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;
}
}

View File

@@ -2,11 +2,12 @@
* DmitriyMX <mail@dmitriymx.ru>
* 2017-04-10
*/
package ru.dmitriymx.vkapi;
package ru.dmitriymx.vkapi.browser;
import java.io.IOException;
public interface Browser {
void setUserAgent(String userAgent);
Response get(String url) throws IOException;
Response post(String url, String data) throws IOException;
}

View File

@@ -2,7 +2,7 @@
* DmitriyMX <mail@dmitriymx.ru>
* 2017-04-10
*/
package ru.dmitriymx.vkapi;
package ru.dmitriymx.vkapi.browser;
public interface Response {
int getStatus();