Java And Url's

Discussion in 'Plugin Development' started by Kodfod, Jul 20, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Kodfod

    Java And Url's are easy to work with. and here are a few ways:

    Getting text from a url:

    Code:JAVA
    1. try {
    2. // Create a URL for the desired page
    3. URL url = new URL("[url]http://hostname:80/index.html[/url]");
    4.  
    5. // Read all the text returned by the server
    6. BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    7. String str;
    8. while ((str = in.readLine()) != null) {
    9. // str is one line of text; readLine() strips the newline character(s)
    10. }
    11. in.close();
    12. } catch (MalformedURLException e) {
    13. } catch (IOException e) {
    14. }


    Access a password protected url:

    Code:JAVA
    1. // Install the custom authenticator
    2. Authenticator.setDefault(new MyAuthenticator());
    3.  
    4. // Access the page
    5. try {
    6. // Create a URL for the desired page
    7. URL url = new URL("[url]http://hostname:80/index.html[/url]");
    8.  
    9. // Read all the text returned by the server
    10. BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    11. String str;
    12. while ((str = in.readLine()) != null) {
    13. // str is one line of text; readLine() strips the newline character(s)
    14. }
    15. in.close();
    16. } catch (MalformedURLException e) {
    17. } catch (IOException e) {
    18. }
    19.  
    20. public class MyAuthenticator extends Authenticator {
    21. // This method is called when a password-protected URL is accessed
    22. protected PasswordAuthentication getPasswordAuthentication() {
    23. // Get information about the request
    24. String promptString = getRequestingPrompt();
    25. String hostname = getRequestingHost();
    26. InetAddress ipaddr = getRequestingSite();
    27. int port = getRequestingPort();
    28.  
    29. // Get the username from the user...
    30. String username = "myusername";
    31.  
    32. // Get the password from the user...
    33. String password = "mypassword";
    34.  
    35. // Return the information
    36. return new PasswordAuthentication(username, password.toCharArray());
    37. }
    38. }


    Converting between a URI and a URL:

    Code:JAVA
    1. URI uri = null;
    2. URL url = null;
    3.  
    4. // Create a URI
    5. try {
    6. uri = new URI("file://D:/almanac1.4/Ex1.java");
    7. } catch (URISyntaxException e) {
    8. }
    9.  
    10. // Convert an absolute URI to a URL
    11. try {
    12. url = uri.toURL();
    13. // URI was not absolute
    14. } catch (MalformedURLException e) {
    15. }
    16.  
    17. // Convert a URL to a URI
    18. try {
    19. uri = new URI(url.toString());
    20. } catch (URISyntaxException e) {
    21. }


    Creating a URL:

    Code:JAVA
    1. try {
    2. // With components.
    3. URL url = new URL("http", "hostname", 80, "index.html");
    4.  
    5. // With a single string.
    6. url = new URL("[url]http://hostname:80/index.html[/url]");
    7. } catch (MalformedURLException e) {
    8. }


    Sending a POST Request Using a URL:

    Code:JAVA
    1. try {
    2. // Construct data
    3. String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
    4. data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
    5.  
    6. // Send data
    7. URL url = new URL("[url]http://hostname:80/cgi[/url]");
    8. URLConnection conn = url.openConnection();
    9. conn.setDoOutput(true);
    10. OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    11. wr.write(data);
    12. wr.flush();
    13.  
    14. // Get the response
    15. BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    16. String line;
    17. while ((line = rd.readLine()) != null) {
    18. // Process line...
    19. }
    20. wr.close();
    21. rd.close();
    22. } catch (Exception e) {
    23. }


    Parsing a URL:

    Code:JAVA
    1. try {
    2. URL url = new URL("[url]http://hostname:80/index.html#_top_[/url]");
    3.  
    4. String protocol = url.getProtocol(); // http
    5. String host = url.getHost(); // hostname
    6. int port = url.getPort(); // 80
    7. String file = url.getFile(); // index.html
    8. String ref = url.getRef(); // _top_
    9. } catch (MalformedURLException e) {
    10. }


    Please enjoy!!

    Source: http://www.exampledepot.com/
     
    SupaHam and hawkfalcon like this.
  2. Offline

    hawkfalcon

  3. Offline

    Kodfod

    Thanks =)
     
  4. Offline

    russjr08

    HTML scrapping (Aka reading text from a page) is a great way to check if a plugin is up to date, in case anyone needs an example on what to use this with! :D
     
    projectwoosh and Kodfod like this.
Thread Status:
Not open for further replies.

Share This Page