简体   繁体   中英

authentication with Java and apache HttpClient 4.5.1

my problem is, that i don't get, how to log in with Java and Apache HttpComponents (HttpClient v4.5.1) into a specific site: Site im trying to log in . I have the username (test_admin) and the password (testing) to log in but i think this is not enough and i need something more. I think this has something to do with the field security_token i see when i make a get request to the uri, but i dont know how to keep that or how to save that and what to do with it afterwards. There is also a hidden input field with the name login-ticket, but i dont know what's that for either. I want to login, because i need to see the courses and add some new ones. After trying with several code implementations im stick with this code:

    public static void setGet(CloseableHttpClient httpClient) throws UnsupportedOperationException, IOException
{
    HttpGet httpGet = new HttpGet("http://demo.studip.de/dispatch.php/admin/courses");
    CloseableHttpResponse httpResponse = httpClient.execute(httpGet);

    System.out.println("GET Response Status:: "
            + httpResponse.getStatusLine().getStatusCode());

    showEntity(httpResponse,httpResponse.getEntity());

}


public static HttpEntity setParam(int count, String[] params, String[] values)
{
    List<NameValuePair> formparams = new ArrayList<NameValuePair>();

    for (int i = 0; i < count; i++)
    {
        formparams.add(new BasicNameValuePair(params[i],values[i]));
        System.out.println("Paramater------------------> "+params[i]+"   Values-------------> "+values[i]);
    }   
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
    return entity;

}

public static void setPost(HttpClient httpC) throws ClientProtocolException, IOException
{
    HttpPost httppost = new HttpPost("http://demo.studip.de/dispatch.php/admin/courses");

    //String[] params = {"loginname", "password"};
    //String[] values = {"test_admin", "testing"};
    //HttpEntity entity = setParam(2, params, values );

    HttpResponse response = httpC.execute(httppost);
    System.out.println("POST Response Status:: "
            + response.getStatusLine().getStatusCode());
    showEntity(response, response.getEntity());

}


public static void showEntity(HttpResponse httpResp, HttpEntity httpClient) throws IOException
{

        httpClient = httpResp.getEntity();
        if (httpClient != null) 
            httpClient = new BufferedHttpEntity(httpClient);

         System.out.print(EntityUtils.toString(httpClient));
}




 public static void main(String[] args) throws InterruptedException, IOException {

     CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
     credentialsProvider.setCredentials(AuthScope.ANY, 
             new UsernamePasswordCredentials("test_admin", "testing"));
     CloseableHttpClient hc = 
     HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();

     setGet(hc);
    // HttpClient httpclient = HttpClients.createDefault();
     setPost(hc);
     setGet(hc);

    }

The problem now ist that i get everytime the same answer from the server i only see the login page in the response, where the server asks me to login with username and password.

您从服务器401,403,301,302或200获得哪个代码?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM