Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. package swag.botprotect;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8.  
  9. public class Licence {
  10.  
  11. private static String getCurrentIP() {
  12. String result = null;
  13. try {
  14. BufferedReader reader = null;
  15. try {
  16. URL url = new URL("http://myip.by/");
  17. InputStream inputStream = null;
  18. inputStream = url.openStream();
  19. reader = new BufferedReader(new InputStreamReader(inputStream));
  20. StringBuilder allText = new StringBuilder();
  21. char[] buff = new char[1024];
  22.  
  23. int count = 0;
  24. while ((count = reader.read(buff)) != -1) {
  25. allText.append(buff, 0, count);
  26. }
  27. // Строка содержащая IP имеет следующий вид
  28. // <a href="whois.php?127.0.0.1">whois 127.0.0.1</a>
  29. Integer indStart = allText.indexOf("\">whois ");
  30. Integer indEnd = allText.indexOf("</a>", indStart);
  31.  
  32. String ipAddress = new String(allText.substring(indStart + 8, indEnd));
  33. if (ipAddress.split("\\.").length == 4) { // минимальная (неполная)
  34. //проверка что выбранный текст является ip адресом.
  35. result = ipAddress;
  36. }
  37. } catch (MalformedURLException ex) {
  38. ex.printStackTrace();
  39. } catch (IOException ex) {
  40. ex.printStackTrace();
  41. } finally {
  42. if (reader != null) {
  43. try {
  44. reader.close();
  45. } catch (Exception ex) {
  46. ex.printStackTrace();
  47. }
  48. }
  49. }
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. }
  53.  
  54. return result;
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement