I am trying to log into this website with the latest version of commons 4.0.1
and can't seem to get it to work. I checked the forum and all the posts seem
to reference the older version. Here is my code please help me out .

First the form on the page I am trying to log into

<div class="loginPanel">
    <div class="container">

        <div class="title">Login</div>
        <form id="loginForm" class="loginForm" method="POST"
action="/path/to/main/page/login">
            
            <table>
                <tr>
                    <td class="label">USERNAME:</td>
                    <td><input
                            value=""
                            class="edit"
                            id="txtUsernameLogin"
                            name="txtUserName"
                            type="text"/></td>
                </tr>

                <tr>
                    <td class="label">PASSWORD:</td>
                    <td><input
                            value=""
                            class="edit"
                            name="txtPassword"
                            type="password"/></td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <input type="checkbox" name="txtRemember"
value="true" align="middle"/>
                        Remember me on this computer
                    </td>

                </tr>
                <tr>
                    <td></td>
                    <td class=""><input type="submit"
                                        class="submit"
                                        value="Login"
                                        alt="Login"/></td>
                </tr>
            </table>
        </form>
    </div>
</div>'

Now the code I am using to try and log in.

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

/**
 * WebUtils This class contains basic Web Utilities used for logging into
and manipulating data
 *                      on a requested website.
 * 
 * @author ME
 */
public class WebUtils {
        

        /**
         * getPageSource This Method will return the source code of the 
requested
page when given the
         *                       following parameters
         *  
         * @return pageSource The source code of the requested page
         */
        public String getPageSource(){
                
                  String pageSource = "";
                  
                  DefaultHttpClient httpclient = new DefaultHttpClient();
                  HttpContext localContext = new BasicHttpContext();
                  
                  
                  List<NameValuePair> qparams = new ArrayList<NameValuePair>();
                  qparams.add(new BasicNameValuePair("txtUserName", 
"myusername"));
                  qparams.add(new BasicNameValuePair("txtPassword", 
"mypassword"));
                  
                  URI uri;
                try {
                        uri = URIUtils.createURI("http", "the.url.to.the.site", 
8080,
"/path/to/login", 
                              URLEncodedUtils.format(qparams, "UTF-8"), null);
                        
                        HttpPost httppost = new HttpPost(uri);
                        HttpResponse response = httpclient.execute(httppost, 
localContext);
                HttpEntity entity = response.getEntity();
                
                List<Cookie> cookies = httpclient.getCookieStore().getCookies();
                
                if (cookies.isEmpty()) {
                    System.out.println("None");
                } else {
                    for (int i = 0; i < cookies.size(); i++) {
                        System.out.println(i +" - " + 
cookies.get(i).toString());
                    }
                }
                
                if (entity != null) {
                    long len = entity.getContentLength();
                    if (len != -1) {
                        pageSource = EntityUtils.toString(entity);
                        
                    } else {
                        pageSource = "";
                    }
                }
                                        
                } catch (URISyntaxException e) {
                        e.printStackTrace();
                } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return pageSource; 
          }
}

For some reason I can't seem to login. No matter what I do it takes me right
back to the login page. Any help will be greatly appreciated. Also please
note that I don't have control over the login form as it is on a third party
site that I am trying to pull some data off once I get logged in.

Thanks,



-- 
View this message in context: 
http://n4.nabble.com/Logging-into-a-website-issues-with-4-0-1-tp1490520p1490520.html
Sent from the Commons - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to