The program logs on the forum and edits the message. (Everything works)
I would like to bring to the separate class the part that is responsible for editing the message.
When making errors occur in:
cookieStore SCHEME (new UrlEncodedFormEntity(nvps)) new UrlEncodedFormEntity(nvps) response execute EntityUtils.consume(entity); class code:
//httpcomponents-client-4.5.2 package com.company; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.CookieStore; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; public class Main { private static final String SCHEME = "http"; private static final String HOST = "site.ru/login.php"; private static final String USERNAME = "Megatron666"; private static final String PASSWORD = "123456"; private static final String LOGIN = "%C2%F5%EE%E4"; public static void main(String[] args) throws IOException, URISyntaxException { CookieStore cookieStore = new BasicCookieStore(); String sid = null; try (CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build()) { URI uri = new URIBuilder() .setScheme(SCHEME) .setHost(HOST) .build(); HttpPost httpPost = new HttpPost(uri); System.out.print("uri1 "); System.out.println(uri); List<NameValuePair> nvps = new ArrayList<>(); nvps.add(new BasicNameValuePair("username", USERNAME)); nvps.add(new BasicNameValuePair("password", PASSWORD)); nvps.add(new BasicNameValuePair("login", LOGIN)); System.out.print("nvps ");System.out.println(nvps); httpPost.setEntity(new UrlEncodedFormEntity(nvps));// ΠΎΡΠΏΡΠ°Π²ΠΊΠ° ΠΏΠ°Ρ Π½Π° uri1 try (CloseableHttpResponse response = httpClient.execute(httpPost)) {//ΠΌΠ°Π³ΠΈΡ HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); } } ///////////// List<Cookie> cookies = cookieStore.getCookies(); for (Cookie cookie : cookies) { if (cookie.getName().equals("sitenew_sid")) { sid = cookie.getValue(); // Π²ΡΡΠ°ΡΠΊΠΈΠ²Π°Π½ΠΈΠ΅ ΡΠΈΠ΄Π° ΠΈΠ· ΠΊΡΠΊΠΈ break; } } System.out.print("sid "); System.out.println(sid); //////////// try (CloseableHttpClient httpClient = HttpClients.createDefault()) { URI uri = new URIBuilder() //ΡΠ±ΠΎΡΠΊΠ° ΡΡΡΠ»ΠΊΠΈ .setScheme(SCHEME) .setHost(HOST) .setParameter("sid", sid).build(); System.out.print("uri2 ");System.out.println(uri); boolean flag=true; HttpGet httpGet = new HttpGet(uri);// ΠΎΡΠΏΡΠ°Π²ΠΊΠ° uri2 try (CloseableHttpResponse response = httpClient.execute(httpGet)) { try (BufferedReader br = new BufferedReader( new InputStreamReader((response.getEntity().getContent()), "windows-1251"))) { String output; while ((output = br.readLine()) != null) { //ΡΡΠ΅Π½ΠΈΠ΅ Ρ
ΡΠΌΠ» if((output.indexOf(USERNAME) != -1 )&& (flag == true))//ΠΏΠΎΠΈΡΠΊ Π½ΠΈΠΊΠ° Π² ΡΡΠΈΡΡΠ²Π°Π΅ΠΌΡΡ
ΡΡΡΠΎΠΊΠ°Ρ
{ flag=false; System.out.println("ΠΡ
ΠΎΠ΄ Π²ΡΠΏΠΎΠ»Π½Π΅Π½"); } } } } }//ΠΊΠΎΠ½Π΅Ρ ΡΠ±ΠΎΡΠΊΠΈ - ΠΏΡΠΎΠ²Π΅ΡΠΊΠΈ try (CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build()) { URI uri = new URIBuilder() .setScheme(SCHEME) .setHost("site.ru/posting.php?mode=editpost&p=13368810") .build(); HttpPost httpPost = new HttpPost(uri); System.out.print("uri1 "); System.out.println(uri); List<NameValuePair> nvps = new ArrayList<>(); nvps.add(new BasicNameValuePair("mode", "editpost")); nvps.add(new BasicNameValuePair("message", "123456")); nvps.add(new BasicNameValuePair("post", "send")); System.out.print("nvps ");System.out.println(nvps); httpPost.setEntity(new UrlEncodedFormEntity(nvps));// ΠΎΡΠΏΡΠ°Π²ΠΊΠ° ΠΏΠ°Ρ Π½Π° uri1 try (CloseableHttpResponse response = httpClient.execute(httpPost)) {//ΠΌΠ°Π³ΠΈΡ HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); } } //////////// } }