Package org.apache.http.client.entity

Examples of org.apache.http.client.entity.UrlEncodedFormEntity


        // Submit the form to /restricted/SecuredPostServlet
        HttpPost restrictedPost = new HttpPost(baseURLNoAuth + "restricted/SecuredPostServlet");

        List<NameValuePair> restrictedParams = new ArrayList<NameValuePair>();
        restrictedParams.add(new BasicNameValuePair("checkParam", "123456"));
        restrictedPost.setEntity(new UrlEncodedFormEntity(restrictedParams, "UTF-8"));

        log.info("Executing request " + restrictedPost.getRequestLine());
        HttpResponse restrictedResponse = httpclient.execute(restrictedPost);

        statusCode = restrictedResponse.getStatusLine().getStatusCode();
        errorHeaders = restrictedResponse.getHeaders("X-NoJException");
        assertTrue("Wrong response code: " + statusCode, statusCode == HttpURLConnection.HTTP_OK);
        assertTrue("X-NoJException(" + Arrays.toString(errorHeaders) + ") is null", errorHeaders.length == 0);

        HttpEntity entity = restrictedResponse.getEntity();
        if ((entity != null) && (entity.getContentLength() > 0)) {
            String body = EntityUtils.toString(entity);
            assertTrue("Redirected to login page", body.indexOf("j_security_check") > 0);
        } else {
            fail("Empty body in response");
        }

        String sessionID = null;
        for (Cookie k : httpclient.getCookieStore().getCookies()) {
            if (k.getName().equalsIgnoreCase("JSESSIONID"))
                sessionID = k.getValue();
        }
        log.info("Saw JSESSIONID=" + sessionID);

        // Submit the login form
        HttpPost formPost = new HttpPost(baseURLNoAuth + "j_security_check");
        formPost.addHeader("Referer", baseURLNoAuth + "restricted/login.html");

        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("j_username", "user1"));
        formparams.add(new BasicNameValuePair("j_password", "password1"));
        formPost.setEntity(new UrlEncodedFormEntity(formparams, "UTF-8"));

        log.info("Executing request " + formPost.getRequestLine());
        HttpResponse postResponse = httpclient.execute(formPost);

        statusCode = postResponse.getStatusLine().getStatusCode();
View Full Code Here


        formPost.addHeader("Referer", baseURLNoAuth + "restricted/login.html");

        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("j_username", username));
        formparams.add(new BasicNameValuePair("j_password", password));
        formPost.setEntity(new UrlEncodedFormEntity(formparams, "UTF-8"));

        log.info("Executing request " + formPost.getRequestLine());
        HttpResponse postResponse = httpclient.execute(formPost);

        statusCode = postResponse.getStatusLine().getStatusCode();
View Full Code Here

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("j_username", user));
            nvps.add(new BasicNameValuePair("j_password", pass));

            httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));

            response = httpclient.execute(httpost);
            entity = response.getEntity();
            if (entity != null)
                EntityUtils.consume(entity);
View Full Code Here

            HttpPost httpPost = new HttpPost(strippedContextUrl + "/j_security_check");
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("j_username", user));
            nameValuePairs.add(new BasicNameValuePair("j_password", pass));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            final HttpResponse response = httpClient.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            assertEquals("Unexpected status code returned after the authentication.", expectedStatusCode, statusCode);
            return EntityUtils.toString(response.getEntity());
        } finally {
View Full Code Here

            HttpPost httpPost = new HttpPost(strippedContextUrl + "/j_security_check");
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("j_username", user));
            nameValuePairs.add(new BasicNameValuePair("j_password", pass));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            response = httpClient.execute(httpPost);
            statusCode = response.getStatusLine().getStatusCode();
            assertEquals("Unexpected status code returned after the authentication.", expectedStatusCode, statusCode);
            return EntityUtils.toString(response.getEntity());
        } finally {
View Full Code Here

        formPost.addHeader("Referer", warURL + "login.html");

        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("j_username", "user1"));
        formparams.add(new BasicNameValuePair("j_password", "password1"));
        formPost.setEntity(new UrlEncodedFormEntity(formparams, "UTF-8"));

        HttpResponse postResponse = httpConn.execute(formPost);
        try {
            int statusCode = postResponse.getStatusLine().getStatusCode();
            Header[] errorHeaders = postResponse.getHeaders("X-NoJException");
View Full Code Here

        String samlRequest = splitted[1].substring(0, splitted[1].indexOf("\""));
        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("SAMLRequest", samlRequest));

        HttpPost httpPost = new HttpPost(idpURL.toURI());
        httpPost.setEntity(new UrlEncodedFormEntity(pairs));
        HttpResponse httpResponse = httpClient.execute(httpPost);

        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            httpResponseBody = EntityUtils.toString(httpResponse.getEntity());
            EntityUtils.consume(entity);
        }

        // parse SAMLResponse and post it back to SP
        splitted = httpResponseBody.split("NAME=\"SAMLResponse\" VALUE=\"");
        String samlResponse = splitted[1].substring(0, splitted[1].indexOf("\""));
        pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("SAMLResponse", samlResponse));

        httpPost = new HttpPost(spURL.toURI());
        httpPost.setEntity(new UrlEncodedFormEntity(pairs));
        httpResponse = httpClient.execute(httpPost);

        entity = httpResponse.getEntity();
        if (entity != null) {
            httpResponseBody = EntityUtils.toString(httpResponse.getEntity());
View Full Code Here

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("j_username", user));
            nvps.add(new BasicNameValuePair("j_password", pass));

            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

            response = httpclient.execute(httpost);
            entity = response.getEntity();
            if (entity != null) { EntityUtils.consume(entity); }
View Full Code Here

    setHeaderValue(httpPost, referer);
    httpPost.addHeader(CONTENT_TYPE_NAME, CONTENT_TYPE_VALUE_FORM);

    if(log) {
      //for test
      UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvpList, HTTP.UTF_8);
      httpPost.setEntity(entity);
      org.vietspider.common.io.DataReader reader = new org.vietspider.common.io.DataReader();
      byte [] bytes = reader.loadInputStream(entity.getContent()).toByteArray();
      StringBuilder builder = new StringBuilder("=====================Post Value=====================\n");
      builder.append(new String(bytes, "utf-8")).append('\n');
      builder.append("==========================================").append('\n');
      LogService.getInstance().setMessage("WEB", null, builder.toString());
      //end test
    } else {
      httpPost.setEntity(new UrlEncodedFormEntity(nvpList, HTTP.UTF_8));
    }

    return httpPost;
  }
View Full Code Here

    if(nvpList == null) return httpPost;

    if(log) {
      //for test
      UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvpList, HTTP.UTF_8);
      httpPost.setEntity(entity);
      org.vietspider.common.io.DataReader reader = new org.vietspider.common.io.DataReader();
      byte [] bytes = reader.loadInputStream(entity.getContent()).toByteArray();
      StringBuilder builder = new StringBuilder("=====================Post Value=====================\n");
      builder.append(new String(bytes, "utf-8")).append('\n');
      builder.append("==========================================").append('\n');
      LogService.getInstance().setMessage("WEB", null, builder.toString());
      //end test
    } else {
      httpPost.setEntity(new UrlEncodedFormEntity(nvpList, HTTP.UTF_8));
    }

    return httpPost;
  }
View Full Code Here

TOP

Related Classes of org.apache.http.client.entity.UrlEncodedFormEntity

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.