Advertisement
Guest User

Untitled

a guest
May 13th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javax.net.ssl.HttpsURLConnection;
  4. import java.io.BufferedReader;
  5. import java.io.DataOutputStream;
  6. import java.io.InputStreamReader;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9.  
  10. public class Main {
  11.  
  12. private final String USER_AGENT = "Mozilla/5.0";
  13.  
  14. public static void main(String[] args) throws Exception {
  15.  
  16. Main http = new Main();
  17.  
  18. System.out.println("Testing 1 - Send Http GET request");
  19. http.sendGet();
  20.  
  21. System.out.println("nTesting 2 - Send Http POST request");
  22. http.sendPost();
  23.  
  24. }
  25.  
  26. // HTTP GET request
  27. private void sendGet() throws Exception {
  28.  
  29. String url = "http://sysadmins.ru/login.php";
  30.  
  31. URL obj = new URL(url);
  32. HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  33.  
  34. // optional default is GET
  35. con.setRequestMethod("GET");
  36.  
  37. //add request header
  38. con.setRequestProperty("User-Agent", USER_AGENT);
  39.  
  40. int responseCode = con.getResponseCode();
  41. System.out.println("nSending 'GET' request to URL : " + url);
  42. System.out.println("Response Code : " + responseCode);
  43.  
  44. BufferedReader in = new BufferedReader(
  45. new InputStreamReader(con.getInputStream()));
  46. String inputLine;
  47. StringBuffer response = new StringBuffer();
  48.  
  49. while ((inputLine = in.readLine()) != null) {
  50. response.append(inputLine);
  51. }
  52. in.close();
  53.  
  54. //print result
  55. System.out.println(response.toString());
  56.  
  57. }
  58.  
  59. // HTTP POST request
  60. private void sendPost() throws Exception {
  61.  
  62. String url = "http://sysadmins.ru/login.php";
  63. URL obj = new URL(url);
  64. HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
  65.  
  66. //add reuqest header
  67. con.setRequestMethod("POST");
  68. con.setRequestProperty("User-Agent", USER_AGENT);
  69. con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  70.  
  71. String urlParameters = "username=Megatron666&password=123456&redirect=&login=%C2%F5%EE%E4";
  72.  
  73. // Send post request
  74. con.setDoOutput(true);
  75. DataOutputStream wr = new DataOutputStream(con.getOutputStream());
  76. wr.writeBytes(urlParameters);
  77. wr.flush();
  78. wr.close();
  79.  
  80. int responseCode = con.getResponseCode();
  81. System.out.println("nSending 'POST' request to URL : " + url);
  82. System.out.println("Post parameters : " + urlParameters);
  83. System.out.println("Response Code : " + responseCode);
  84.  
  85. BufferedReader in = new BufferedReader(
  86. new InputStreamReader(con.getInputStream()));
  87. String inputLine;
  88. StringBuffer response = new StringBuffer();
  89.  
  90. while ((inputLine = in.readLine()) != null) {
  91. response.append(inputLine);
  92. }
  93. in.close();
  94.  
  95. //print result
  96. System.out.println(response.toString());
  97.  
  98. }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement