Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * ====================================================================
  3.  *
  4.  *  Licensed to the Apache Software Foundation (ASF) under one or more
  5.  *  contributor license agreements.  See the NOTICE file distributed with
  6.  *  this work for additional information regarding copyright ownership.
  7.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  8.  *  (the "License"); you may not use this file except in compliance with
  9.  *  the License.  You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  *  Unless required by applicable law or agreed to in writing, software
  14.  *  distributed under the License is distributed on an "AS IS" BASIS,
  15.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  *  See the License for the specific language governing permissions and
  17.  *  limitations under the License.
  18.  * ====================================================================
  19.  *
  20.  * This software consists of voluntary contributions made by many
  21.  * individuals on behalf of the Apache Software Foundation.  For more
  22.  * information on the Apache Software Foundation, please see
  23.  * <http://www.apache.org/>.
  24.  *
  25.  */
  26.  
  27. package org.apache.http.examples.client;
  28.  
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. import org.apache.http.HttpEntity;
  32. import org.apache.http.HttpResponse;
  33. import org.apache.http.NameValuePair;
  34. import org.apache.http.client.entity.UrlEncodedFormEntity;
  35. import org.apache.http.client.methods.HttpGet;
  36. import org.apache.http.client.methods.HttpPost;
  37. import org.apache.http.cookie.Cookie;
  38. import org.apache.http.impl.client.DefaultHttpClient;
  39. import org.apache.http.message.BasicNameValuePair;
  40. import org.apache.http.protocol.HTTP;
  41.  
  42. /**
  43.  * A example that demonstrates how HttpClient APIs can be used to perform
  44.  * form-based logon.
  45.  */
  46. public class ClientFormLogin {
  47.  
  48.     public static void main(String[] args) throws Exception {
  49.  
  50.         DefaultHttpClient httpclient = new DefaultHttpClient();
  51.  
  52.         HttpGet httpget = new HttpGet("https://portal.sun.com/portal/dt");
  53.  
  54.         HttpResponse response = httpclient.execute(httpget);
  55.         HttpEntity entity = response.getEntity();
  56.  
  57.         System.out.println("Login form get: " + response.getStatusLine());
  58.         if (entity != null) {
  59.             entity.consumeContent();
  60.         }
  61.         System.out.println("Initial set of cookies:");
  62.         List<Cookie> cookies = httpclient.getCookieStore().getCookies();
  63.         if (cookies.isEmpty()) {
  64.             System.out.println("None");
  65.         } else {
  66.             for (int i = 0; i < cookies.size(); i++) {
  67.                 System.out.println("- " + cookies.get(i).toString());
  68.             }
  69.         }
  70.  
  71.         HttpPost httpost = new HttpPost("https://portal.sun.com/amserver/UI/Login?" +
  72.                 "org=self_registered_users&" +
  73.                 "goto=/portal/dt&" +
  74.                 "gotoOnFail=/portal/dt?error=true");
  75.  
  76.         List <NameValuePair> nvps = new ArrayList <NameValuePair>();
  77.         nvps.add(new BasicNameValuePair("IDToken1", "username"));
  78.         nvps.add(new BasicNameValuePair("IDToken2", "password"));
  79.  
  80.         httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
  81.  
  82.         response = httpclient.execute(httpost);
  83.         entity = response.getEntity();
  84.  
  85.         System.out.println("Login form get: " + response.getStatusLine());
  86.         if (entity != null) {
  87.             entity.consumeContent();
  88.         }
  89.  
  90.         System.out.println("Post logon cookies:");
  91.         cookies = httpclient.getCookieStore().getCookies();
  92.         if (cookies.isEmpty()) {
  93.             System.out.println("None");
  94.         } else {
  95.             for (int i = 0; i < cookies.size(); i++) {
  96.                 System.out.println("- " + cookies.get(i).toString());
  97.             }
  98.         }
  99.  
  100.         // When HttpClient instance is no longer needed,
  101.         // shut down the connection manager to ensure
  102.         // immediate deallocation of all system resources
  103.         httpclient.getConnectionManager().shutdown();        
  104.     }
  105. }
  106.