1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
import java.net.*;
import java.io.*;
public class ClientFormLogin
{
private static final String POST_CONTENT_TYPE = "application/x-www-form-urlencoded";
private static final String LOGIN_ACTION_NAME = "login";
private static final String LOGIN_USER_NAME_PARAMETER_NAME = "username";
private static final String LOGIN_PASSWORD_PARAMETER_NAME = "password";
private static final String LOGIN_USER_NAME = "user";
private static final String LOGIN_PASSWORD = "password";
private static final String TARGET_URL = "https://ferme.unibet.com/start";
public static void main (String args[])
{
// System.out.println("About to run the 'LogInByHttpPost' downloaded from <a href="http://www.1Your.com"" title="www.1Your.com"">www.1Your.com"</a>);
ClientFormLogin httpUrlBasicAuthentication = new ClientFormLogin();
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 HTPP POST.");
PrintWriter out2 = new PrintWriter(new FileWriter("coucou.html"));
out2.println(response);
out2.close();
System.out.println("Recevied response is: '/n" + response + "'");
//System.out.println("/n Hope you found the article and this sample program useful./nPlease leave your feed back at <a href="http://www.1your.com" title="www.1your.com">www.1your.com</a> and support us.");
}
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=" + LOGIN_ACTION_NAME +" &" + LOGIN_USER_NAME_PARAMETER_NAME +"="
+ encodedLoginUserName + "&" + LOGIN_PASSWORD_PARAMETER_NAME + "=" + encodedLoginPassword+"&"
+"PreviousURL=" + URLEncoder.encode("https://ferme.unibet.com/login");
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
}
}
}
}
} |
Partager