browser update
This commit is contained in:
@@ -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;
|
||||
|
||||
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>
|
||||
* 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;
|
||||
}
|
||||
@@ -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();
|
||||
Reference in New Issue
Block a user