Graph database Analysis of the Steam Network
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.0 KiB

  1. package net.jrtechs.www.utils;
  2. import net.jrtechs.www.SteamAPI.ConnectionErrors;
  3. import net.jrtechs.www.SteamAPI.SteamConnectionException;
  4. import java.io.*;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. /**
  8. * Class which retrieves contents of a website as html
  9. *
  10. * @author Jeffery Russell 5-25-18
  11. */
  12. public class WebScraper
  13. {
  14. /**
  15. * Grabs the contents of a website as a string
  16. *
  17. * @param link to open
  18. * @return source code of website as a single string
  19. */
  20. public static String getWebsite(String link) throws SteamConnectionException
  21. {
  22. try
  23. {
  24. URL url = new URL(link);
  25. HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  26. connection.setRequestMethod("GET");
  27. connection.connect();
  28. int code = connection.getResponseCode();
  29. switch (code)
  30. {
  31. case 401:
  32. throw new SteamConnectionException(ConnectionErrors.RESTRICTED);
  33. case 500:
  34. throw new SteamConnectionException(ConnectionErrors.CONNECTION);
  35. default:
  36. }
  37. BufferedReader br = new BufferedReader(
  38. new InputStreamReader(url.openStream())
  39. );
  40. return WebScraper.getBufferedReaderData(br);
  41. }
  42. catch (SteamConnectionException e)
  43. {
  44. throw e;
  45. }
  46. catch (Exception e)
  47. {
  48. throw new SteamConnectionException(ConnectionErrors.CONNECTION);
  49. }
  50. }
  51. /**
  52. * Gets contents of a post request sent to a web server
  53. *
  54. * @param link to open
  55. * @param postData to send
  56. * @return source code of website as a string
  57. */
  58. public static String getPostResponse(String link, String postData)
  59. {
  60. try
  61. {
  62. URL url = new URL(link);
  63. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  64. conn.setDoOutput(true);
  65. conn.setRequestMethod("POST");
  66. OutputStream os = conn.getOutputStream();
  67. os.write(postData.getBytes());
  68. os.flush();
  69. BufferedReader br = new BufferedReader(
  70. new InputStreamReader(conn.getInputStream())
  71. );
  72. return WebScraper.getBufferedReaderData(br);
  73. }
  74. catch (Exception ex)
  75. {
  76. ex.printStackTrace();
  77. }
  78. return "";
  79. }
  80. /**
  81. * Helper method for getPostResponse() and getWebsite()
  82. * Slams all contents of a buffered reader into a single string.
  83. *
  84. * @param br
  85. * @return contents of buffered reader
  86. * @throws IOException -- with br.readLine()
  87. */
  88. private static String getBufferedReaderData(BufferedReader br)
  89. throws IOException {
  90. String html = "";
  91. String line;
  92. while ((line = br.readLine()) != null)
  93. {
  94. html += line;
  95. }
  96. return html;
  97. }
  98. }