Http Login Post naar een website

Status
Niet open voor verdere reacties.

remcovg

Gebruiker
Lid geworden
25 jun 2006
Berichten
205
Mensjes,

Ik had laatst het plan om een applicatie te schrijven waarmee ik mijn (school)rooster op mijn telefoon kan ophalen. Mijn telefoon ondersteund java ( lg kp500 ). Maar omdat ik de SDK van LG nogal een beetje ingewikkeld vond wou ik het eerst in een windows applicatie maken.

Op de schoolsite moet eerst worden ingelogd voor je je rooster kan bekijken vanwege een of andere reden :P. dus moest ik een nieuw script schrijven. Maar ik krijg mijn post niet goed voor elkaar om in te loggen. wat doe ik hier fout? onderstaand staat me code ;).

Bij voorbaat dank :thumb:!

Gr,

Remco

Code:
package roosterapp;

/**
 *
 * @author Gri-Com
 */

import java.net.*;
import java.io.*;

public class LoginByHttpPost
{

    private static final String POST_CONTENT_TYPE = "application/x-www-form-urlencoded";
    private static final String LOGIN_ACTION_NAME = "cmdLogin";//login
    private static final String LOGIN_USER_NAME_PARAMETER_NAME = "txtUserName"; // user_name
    private static final String LOGIN_PASSWORD_PARAMETER_NAME = "txtPassword"; //user_pass

    private static final String LOGIN_USER_NAME = "";
    private static final String LOGIN_PASSWORD = "";

    private static final String TARGET_URL = "https://orkp.avans.nl/login.asp?comebackto=/index.asp?";
    public static void main (String args[])
    {
        LoginByHttpPost httpUrlBasicAuthentication = new LoginByHttpPost();
        httpUrlBasicAuthentication.httpPostLogin();
    }

    /**
     * The single public method of this class that
     * 1. Prepares a login message
     * 2. Makes the HTTP POST to the target URL
     * 3. Reads and returns the response
     *
     * @throws IOException
     * Any problems while doing the above.
     *
     */
    public void httpPostLogin ()
    {
        try
        {
            // Prepare the content to be written
            // throws UnsupportedEncodingException
            String urlEncodedContent = preparePostContent(LOGIN_USER_NAME, LOGIN_PASSWORD);

            HttpURLConnection urlConnection = doHttpPost(TARGET_URL, urlEncodedContent);

            String response = readResponse(urlConnection);

            System.out.println("Successfully made the HTTP POST.");
            System.out.println("Recevied response is: '/n" + response + "'");
        }
        catch(IOException ioException)
        {
            System.out.println("Problems encounterd.");
        }
    }

    /**
     * Using the given username and password, and using the static string variables, prepare
     * the login message. Note that the username and password will encoded to the
     * UTF-8 standard.
     *
     * @param loginUserName
     * The user name for login
     *
     * @param loginPassword
     * The password for login
     *
     * @return
     * The complete login message that can be HTTP Posted
     *
     * @throws UnsupportedEncodingException
     * Any problems during URL encoding
     */
    private String preparePostContent(String loginUserName, String loginPassword) throws UnsupportedEncodingException
    {
        // Encode the user name and password to UTF-8 encoding standard
        // throws UnsupportedEncodingException
        String encodedLoginUserName = URLEncoder.encode(loginUserName, "UTF-8");
        String encodedLoginPassword = URLEncoder.encode(loginPassword, "UTF-8");

        String content = "login.asp?comebackto=/index.asp?=" + LOGIN_ACTION_NAME +" &" + LOGIN_USER_NAME_PARAMETER_NAME +"="
        + encodedLoginUserName + "&" + LOGIN_PASSWORD_PARAMETER_NAME + "=" + encodedLoginPassword;// cmdLogin == login == login.asp?comebackto=/index.asp?

        return content;

    }

    /**
     * Makes a HTTP POST to the target URL by using an HttpURLConnection.
     *
     * @param targetUrl
     * The URL to which the HTTP POST is made.
     *
     * @param content
     * The contents which will be POSTed to the target URL.
     *
     * @return
     * The open URLConnection which can be used to read any response.
     *
     * @throws IOException
     */
    public HttpURLConnection doHttpPost(String targetUrl, String content) throws IOException
    {
        HttpURLConnection urlConnection = null;
        DataOutputStream dataOutputStream = null;
        try
        {
            // Open a connection to the target URL
            // throws IOException
            urlConnection = (HttpURLConnection)(new URL(targetUrl).openConnection());

            // Specifying that we intend to use this connection for input
            urlConnection.setDoInput(true);

            // Specifying that we intend to use this connection for output
            urlConnection.setDoOutput(true);

            // Specifying the content type of our post
            urlConnection.setRequestProperty("Content-Type", POST_CONTENT_TYPE);

            // Specifying the method of HTTP request which is POST
            // throws ProtocolException
            urlConnection.setRequestMethod("POST");

            // Prepare an output stream for writing data to the HTTP connection
            // throws IOException
            dataOutputStream = new DataOutputStream(urlConnection.getOutputStream());

            // throws IOException
            dataOutputStream.writeBytes(content);
            dataOutputStream.flush();
            dataOutputStream.close();

            return urlConnection;
        }
        catch(IOException ioException)
        {
            System.out.println("I/O problems while trying to do a HTTP post.");
            ioException.printStackTrace();

            // Good practice: clean up the connections and streams
            // to free up any resources if possible
            if (dataOutputStream != null)
            {
                try
                {
                    dataOutputStream.close();
                }
                catch(Throwable ignore)
                {
                    // Cannot do anything about problems while
                    // trying to clean up. Just ignore
                }
            }
            if (urlConnection != null)
            {
                urlConnection.disconnect();
            }

            // throw the exception so that the caller is aware that
            // there was some problems
            throw ioException;
        }
    }

    /**
     * Read response from the URL connection
     *
     * @param urlConnection
     * The URLConncetion from which the response will be read
     *
     * @return
     * The response read from the URLConnection
     *
     * @throws IOException
     * When problems encountered during reading the response from the
     * URLConnection.
     */
    private String readResponse(HttpURLConnection urlConnection) throws IOException
    {

        BufferedReader bufferedReader = null;
        try
        {
            // Prepare a reader to read the response from the URLConnection
            // throws IOException
            bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String responeLine;

            // Good Practice: Use StringBuilder in this case
            StringBuilder response = new StringBuilder();

            // Read untill there is nothing left in the stream
            // throws IOException
            while ((responeLine = bufferedReader.readLine()) != null)
            {
                response.append(responeLine);
            }

            return response.toString();
        }
        catch(IOException ioException)
        {
            System.out.println("Problems while reading the response");
            ioException.printStackTrace();

            // throw the exception so that the caller is aware that
            // there was some problems
            throw ioException;

        }
        finally
        {
            // Good practice: clean up the connections and streams
            // to free up any resources if possible
            if (bufferedReader != null)
            {
                try
                {
                    // throws IOException
                    bufferedReader.close();
                }
                catch(Throwable ignore)
                {
                    // Cannot do much with exceptions doing clean up
                    // Ignoring all exceptions
                }
            }

        }
    }
}

edit: wachtwoord en gebruikersnaam heb ik natuurlijk even weggehaald :P
 
Laatst bewerkt:
Vrij omslachtig en niet veilig
String content = "login.asp?comebackto=/index.asp?=" + LOGIN_ACTION_NAME +" &" + LOGIN_USER_NAME_PARAMETER_NAME +"="
+ encodedLoginUserName + "&" + LOGIN_PASSWORD_PARAMETER_NAME + "=" + encodedLoginPassword;// cmdLogin == login == login.asp?comebackto=/index.asp?

Bekijk eens ejb framework eens en je gooit asp wel snel overboord Het lijkt me sterk dat je wel voldoende studenten zal hebben die kunne maken

google zoeken op amatheras kom je al snel uit op een lib en(s****s ->website) en verder kan je geen bean draaien op je server vb een tomcat of zo
dan ga client op java maar ook server op java waar je dan doorlinkt naar die asp en kan je die mogelijk zelfs weglaten onmiddelijk naar mysql of ect verbinden.
 
Dag Remco,

Ik ben nieuw op het forum, want ik zag dat je iets gemaakt had wat ik ook wel kan gebruiken.

Is nu je probleem hiervoor opgelost? en zou je dit evt. willen delen?

Ik hoor het graag van je.


Gr,
Hans
 
oh ik zal er meteen een slotje op doen. Ja het probleem is opgelost. Ik heb een eenvoudiger login systeem gebruikt op een van mijn eigen servers. en die server logt weer door in op het rooster systeem ( ben sowieso wat meer thuis in php e.d. dus daardoor was dit de makkelijkste oplossing die ik kon bedenken ). ik hoop dat je daarmee geholpen bent? en anders stuur maar even een pm.
 
Hoi Remco,

Oke, ik wilde je een pm sturen maar dit gaat niet om de volgende reden:
Alleen verenigingsleden en donateurs kunnen privé-berichten sturen.

Kan nergens vinden waar ik dit kan doen, ma goed.


Ik heb namelijk zelf ook wel intresse in zo iets, ben zelf een klein beetje bekend met php.

Hoe gaan we dat oplossen?:o
 
Ik wil mijn e-mail niet vermelden op een forum i.v.m. spam. maar er is een contact formulier op www.gri-com.nl en daar staat ook het e-mail
info ( apenstaart ) de rest ;).
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan