HttpClient Authentication, Login

So my goal is to enter the forum using httpclient and then post the response to the thread on this forum. I log in to the system in order, but then when I go to the message in the stream, it says that I have not logged in. Any ideas? I tried to create httpClient using cookies, but then I looked at them after I logged in and there was nothing. So I tried using the same httpclient for both calls, but still did not work.

String username, password, threadNum, bumpMessage, bumpTimer;
HttpClient http;

    public void login() throws ClientProtocolException, IOException, NoSuchAlgorithmException
    {

        this.http = HttpClients.createDefault();



        HttpPost httppost = new HttpPost("http://www.sythe.org/login.php?do=login");


        // Request parameters and other properties.
        List<NameValuePair> params = new ArrayList<NameValuePair>(2);
        params.add(new BasicNameValuePair("do", "login"));
        params.add(new BasicNameValuePair("url", ""));
        params.add(new BasicNameValuePair("vb_login_md5password", this.password));
        params.add(new BasicNameValuePair("vb_login_md5password_utf", this.password));
        params.add(new BasicNameValuePair("s", ""));
        params.add(new BasicNameValuePair("vb_login_username", this.username));
        params.add(new BasicNameValuePair("vb_login_password", ""));



        httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

        //Execute and get the response.
        HttpResponse response = http.execute(httppost);



        HttpEntity entity = response.getEntity();

    }

    public void bump() throws ClientProtocolException, IOException
    {

        HttpPost httppost = new HttpPost("http://www.sythe.org/newreply.php?do=postreply&t=" + this.threadNum);

        // Request parameters and other properties.
        List<NameValuePair> params = new ArrayList<NameValuePair>(2);
        params.add(new BasicNameValuePair("title", ""));
        params.add(new BasicNameValuePair("message", this.bumpMessage));
        params.add(new BasicNameValuePair("wysiwyg", "0"));
        params.add(new BasicNameValuePair("iconid", "0"));
        params.add(new BasicNameValuePair("s", ""));
        params.add(new BasicNameValuePair("posthash", ""));
        params.add(new BasicNameValuePair("poststarttime", ""));
        params.add(new BasicNameValuePair("loggedinuser", ""));
        params.add(new BasicNameValuePair("multiquoteempty", ""));
        params.add(new BasicNameValuePair("sbutton", "Submit Reply"));
        params.add(new BasicNameValuePair("signature", "1"));
        params.add(new BasicNameValuePair("parseurl", "1"));
        params.add(new BasicNameValuePair("emailupdate", "9999"));



        httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

        //Execute and get the response.
        HttpResponse response = this.http.execute(httppost);

        HttpEntity entity = response.getEntity();

        PrintWriter writer = new PrintWriter("filename.html","UTF-8");

        if (entity != null) {
            InputStream instream = entity.getContent();
            String line;
            BufferedReader br = null;

            try {
                br = new BufferedReader(new InputStreamReader(instream));
                while ((line = br.readLine()) != null) {
                    writer.println(line);
                }
            } finally {
                instream.close();
            }
        }
        writer.close();
    }
}

Edit: I got it in python!

jar = cookielib.FileCookieJar("cookies")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
opener.addheaders = [('User-agent', 'Mozilla/5.0')]

def login():
    logindata = urllib.urlencode({'do' : 'login',
                                  'url':'',
                                  'vb_login_md5password' : password,
                                  'vb_login_md5password_utf' : password,
                                  's' : '',
                                  'vb_login_username': username,
                                  'vb_login_password': '' })

    response = opener.open("http://www.sythe.org/login.php?do=login", logindata)



def bumpThread():

    if(username == "Not set" or password == "Not set"):
        print "Please setup password!"
        return

    if(threadNum == 0 or message == "Not set" or bumpTime == 0):
        print threadNum
        print message
        print "Please setup thread!"
        return

    logindata = urllib.urlencode({'title' : 'The Thread',
                                  'message' : message,
                                  'wysiwyg' : '0',
                                  'iconid' : '0',
                                  's' : '',
                                  'do' : 'postreply',
                                  't' : threadNum,
                                  'p' : '',
                                  'posthash' : '',
                                  'poststarttime' : '',
                                  'loggedinuser' : '',
                                  'multiquoteempty' : '',
                                  'sbutton' : 'Submit Reply',
                                  'signature' : '1',
                                  'parseurl' : '1',
                                  'emailupdate' : '9999' })

    thread = opener.open("http://www.sythe.org/newreply.php?do=postreply&t=" + str(threadNum),logindata)
+4
source share
1 answer

, , - "closeable" httpclient (CloseableHttpClient). , BasicCookieStore. httpclient, , httpclient.

, :

, , , POST, GET, , .

:

  • CredentialsProvider
  • cookie BasicCookieStore
  • HttpClient CloseableHttpClient
  • POST: , , ,
  • POST /login.html .
  • GET /user, ,
  • //////// SETUP
    //Setup Proxy (If needed)
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope("proxy.mycompany", 8080),
                new UsernamePasswordCredentials("my_username", "my_password"));
    //Setup Cookies
        BasicCookieStore cookieStore = new BasicCookieStore();
    
    //Build HttpClient
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCredentialsProvider(credsProvider)
                .setDefaultCookieStore(cookieStore)
                .build();
        HttpHost target = new HttpHost("www.mycompany.com", 80, "http");
        HttpHost proxy = new HttpHost("proxy.mycompany", 8080);
        RequestConfig config = RequestConfig.custom()
                .setProxy(proxy)
                .build();
    try {
    //////// LOGIN REQUEST, POST TO /login.html
    //Start POST Request
        HttpPost httppost = new HttpPost('/login.html');
    //Add parameters to POST Request
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();   
        nameValuePairs.add(new BasicNameValuePair('username','my_username'));  
        nameValuePairs.add(new BasicNameValuePair('password','my_password'));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    //Set Config for POST Request
        httppost.setConfig(config);
    //Execute POST Request
        httpresponse = httpclient.execute(target, httppost);
    //Print Response Code
        try {
           System.out.println("Status Code: "+httpresponse.getStatusLine().getStatusCode());
        } finally {
    //Close HTTP Reponse (must be in a 'finally' block)
            httpresponse.close();
        }
    
    //////// CHECK LOGIN STATUS, GET TO /user
    //Start GET Request
        HttpGet httpget = new HttpGet('/user');
    //Set Config for GET Request
        httpget.setConfig(config);
    //Execute GET Request
        httpresponse = httpclient.execute(target, httpget);
    //Print Response Code
        try {
           System.out.println("Status Code: "+httpresponse.getStatusLine().getStatusCode());
        } finally {
    //Close HTTP Reponse (must be in a 'finally' block)
            httpresponse.close();
        }
    } finally {
        httpclient.close();
    }
    
0

Source: https://habr.com/ru/post/1619768/


All Articles