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

Closed due to the fact that off-topic by the participants zRrr , cheops , aleksandr barakin , VAndrJ , Pavel Parshin 18 May '16 at 7:31 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • β€œQuestions asking for help with debugging (β€œ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - zRrr, cheops, aleksandr barakin, VAndrJ, Pavel Parshin
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Show the full text of the error. - Sergey Gornostaev

0