Guest User

Untitled

a guest
Mar 7th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. package practise;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.UnsupportedEncodingException;
  8. import java.net.HttpURLConnection;
  9. import java.net.URL;
  10. import java.net.URLEncoder;
  11.  
  12. public class WebsiteLogin {
  13.  
  14. private static final String POST_CONTENT_TYPE = "text/html; charset=UTF-8";
  15. private static final String LOGIN_USER_NAME_PARAMETER_NAME = "login";
  16. private static final String LOGIN_PASSWORD_PARAMETER_NAME = "password";
  17. private static final String LOGIN_USER_NAME = "myusername";
  18. private static final String LOGIN_PASSWORD = "mypassword";
  19. private static final String TARGET_URL = "https://mrpropop.com/login";
  20.  
  21. public static void main(String[] args) {
  22. WebsiteLogin httpUrlBasicAuthentication = new WebsiteLogin();
  23. httpUrlBasicAuthentication.httpPostLogin();
  24. }
  25.  
  26. public void httpPostLogin() {
  27.  
  28. try {
  29. String urlEncodedContent = preparePostContent(LOGIN_USER_NAME, LOGIN_PASSWORD);
  30. System.out.println(urlEncodedContent);
  31. HttpURLConnection urlConnection = doHttpPost(TARGET_URL, urlEncodedContent);
  32. String response = readResponse(urlConnection);
  33. System.out.println("Successfully made the HTPP POST.");
  34. System.out.println("Recevied response is: '/n" + response + "'");
  35.  
  36. } catch (IOException ioException) {
  37. System.out.println("Problems encounterd.");
  38. }
  39. }
  40.  
  41. private String preparePostContent(String loginUserName, String loginPassword) throws UnsupportedEncodingException {
  42.  
  43. String encodedLoginUserName = URLEncoder.encode(loginUserName, "UTF-8");
  44. String encodedLoginPassword = URLEncoder.encode(loginPassword, "UTF-8");
  45. String content = LOGIN_USER_NAME_PARAMETER_NAME + "=" + encodedLoginUserName + "&"
  46. + LOGIN_PASSWORD_PARAMETER_NAME + "=" + encodedLoginPassword;
  47. return content;
  48. }
  49.  
  50. public HttpURLConnection doHttpPost(String targetUrl, String content) throws IOException {
  51. HttpURLConnection urlConnection = null;
  52. DataOutputStream dataOutputStream = null;
  53.  
  54. try {
  55. urlConnection = (HttpURLConnection) (new URL(targetUrl).openConnection());
  56. urlConnection.setDoInput(true);
  57. urlConnection.setDoOutput(true);
  58. urlConnection.setRequestProperty("Content-Type", POST_CONTENT_TYPE);
  59. urlConnection.setRequestProperty("User-Agent",
  60. "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");
  61. HttpURLConnection.setFollowRedirects(true);
  62. urlConnection.setRequestMethod("POST");
  63. dataOutputStream = new DataOutputStream(urlConnection.getOutputStream());
  64. dataOutputStream.writeBytes(content);
  65. dataOutputStream.flush();
  66. dataOutputStream.close();
  67.  
  68. return urlConnection;
  69.  
  70. } catch (IOException ioException) {
  71. System.out.println("I/O problems while trying to do a HTTP post.");
  72. ioException.printStackTrace();
  73.  
  74. if (dataOutputStream != null) {
  75.  
  76. try {
  77. dataOutputStream.close();
  78. } catch (Throwable ignore) {
  79. }
  80. }
  81.  
  82. if (urlConnection != null) {
  83. urlConnection.disconnect();
  84. }
  85. throw ioException;
  86. }
  87. }
  88.  
  89. private String readResponse(HttpURLConnection urlConnection) throws IOException {
  90. BufferedReader bufferedReader = null;
  91. try {
  92. bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  93. String responeLine;
  94. StringBuilder response = new StringBuilder();
  95.  
  96. while ((responeLine = bufferedReader.readLine()) != null) {
  97. response.append(responeLine);
  98. }
  99. return response.toString();
  100.  
  101. } catch (IOException ioException) {
  102. System.out.println("Problems while reading the response");
  103. ioException.printStackTrace();
  104. throw ioException;
  105.  
  106. } finally {
  107.  
  108. if (bufferedReader != null) {
  109. try {
  110. bufferedReader.close();
  111. } catch (Throwable ignore) {
  112. }
  113. }
  114. }
  115. }
  116. }
Add Comment
Please, Sign In to add comment