refac: использование Apache HttpClient
This commit is contained in:
@@ -27,6 +27,7 @@ ext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
implementation("org.apache.httpcomponents.client5:httpclient5:5.5")
|
||||||
implementation("com.fasterxml.jackson.core:jackson-databind:$jacksonVersion")
|
implementation("com.fasterxml.jackson.core:jackson-databind:$jacksonVersion")
|
||||||
|
|
||||||
testImplementation(platform("org.junit:junit-bom:$junitVersion"))
|
testImplementation(platform("org.junit:junit-bom:$junitVersion"))
|
||||||
|
|||||||
@@ -1,85 +1,53 @@
|
|||||||
package ru.di9.ihc;
|
package ru.di9.ihc;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||||
|
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
|
||||||
|
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||||
|
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
|
||||||
|
import org.apache.hc.core5.http.message.BasicNameValuePair;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.CookieManager;
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.http.HttpClient;
|
import java.util.List;
|
||||||
import java.net.http.HttpRequest;
|
|
||||||
import java.net.http.HttpResponse;
|
|
||||||
import java.time.Duration;
|
|
||||||
|
|
||||||
public class IhcClient {
|
public class IhcClient {
|
||||||
private final String baseUrl;
|
private final String baseUrl;
|
||||||
private final HttpClient httpClient;
|
private final CloseableHttpClient httpClient;
|
||||||
private final ObjectMapper mapper;
|
private final ObjectMapper mapper;
|
||||||
|
|
||||||
public IhcClient(String baseUrl) {
|
public IhcClient(String baseUrl) {
|
||||||
this.baseUrl = baseUrl;
|
this.baseUrl = baseUrl;
|
||||||
|
this.httpClient = HttpClientBuilder.create().build();
|
||||||
this.httpClient = HttpClient.newBuilder()
|
|
||||||
.version(HttpClient.Version.HTTP_2)
|
|
||||||
.connectTimeout(Duration.ofSeconds(10))
|
|
||||||
.cookieHandler(new CookieManager())
|
|
||||||
.build();
|
|
||||||
|
|
||||||
this.mapper = new ObjectMapper();
|
this.mapper = new ObjectMapper();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean auth(String login, String password) {
|
public boolean auth(String login, String password) {
|
||||||
var request = HttpRequest.newBuilder()
|
HttpPost httpPost = new HttpPost(URI.create("%s/j_spring_security_check?ajax=true".formatted(baseUrl)));
|
||||||
.POST(HttpRequest.BodyPublishers.ofString(
|
httpPost.setHeader("Accept", "application/json");
|
||||||
"j_username=%s&j_password=%s&recaptcha=&ihccaptcha=".formatted(login, password)
|
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
||||||
))
|
httpPost.setHeader("Referer", "%s/login/auth".formatted(baseUrl));
|
||||||
.uri(URI.create("%s/j_spring_security_check?ajax=true".formatted(baseUrl)))
|
httpPost.setEntity(new UrlEncodedFormEntity(List.of(
|
||||||
.setHeader("Accept", "application/json")
|
new BasicNameValuePair("j_username", login),
|
||||||
.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
|
new BasicNameValuePair("j_password", password),
|
||||||
.setHeader("Referer", "%s/login/auth".formatted(baseUrl))
|
new BasicNameValuePair("recaptcha", ""),
|
||||||
.build();
|
new BasicNameValuePair("ihccaptcha", "")
|
||||||
|
)));
|
||||||
|
|
||||||
HttpResponse<String> response;
|
boolean authResult;
|
||||||
try {
|
try {
|
||||||
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
authResult = httpClient.execute(httpPost, resp -> {
|
||||||
} catch (IOException | InterruptedException e) {
|
if (resp.getCode() != 200) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
AuthResponse authResponse = mapper.readValue(resp.getEntity().getContent(), AuthResponse.class);
|
||||||
|
return "none".equalsIgnoreCase(authResponse.alert().type());
|
||||||
|
});
|
||||||
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.statusCode() != 200) {
|
return authResult;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
AuthResponse authResponse;
|
|
||||||
try {
|
|
||||||
String json = response.body();
|
|
||||||
authResponse = mapper.readValue(json, AuthResponse.class);
|
|
||||||
} catch (JsonProcessingException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return "none".equalsIgnoreCase(authResponse.alert().type());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void getDomainList() {
|
|
||||||
var request = HttpRequest.newBuilder()
|
|
||||||
.GET()
|
|
||||||
.uri(URI.create("%s/dnsZone/list".formatted(baseUrl)))
|
|
||||||
.setHeader("Accept", "application/json")
|
|
||||||
.build();
|
|
||||||
|
|
||||||
HttpResponse<String> response;
|
|
||||||
try {
|
|
||||||
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
|
||||||
} catch (IOException | InterruptedException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (response.statusCode() != 200) {
|
|
||||||
throw new RuntimeException("not 200");
|
|
||||||
}
|
|
||||||
|
|
||||||
String body = response.body();
|
|
||||||
System.out.println(body);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user