Get website content

Discussion in 'Plugin Development' started by mike2033, May 1, 2013.

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

    mike2033

    Hello forum!

    I would like to ask what's wrong with my code since I haven't found any solution yet. Currently it looks like this: (it's not my own code, I found it somewhere and changed it but don't remember the source anymore)

    Code:
    try {
             
                        String url = mywebsite here;
             
                        URL obj = new URL(url);
                        HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
                        conn.setReadTimeout(5000);
                        conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
                        conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0");
                        conn.addRequestProperty("Referer", "google.com");
             
                        BufferedReader in = new BufferedReader(
                                                  new InputStreamReader(conn.getInputStream()));
                        String inputLine;
                        StringBuffer html = new StringBuffer();
             
                        while ((inputLine = in.readLine()) != null) {
                            html.append(inputLine);
                        }
                        in.close();
             
                        } catch (Exception e)
                        {
                        e.printStackTrace();
                        }
    It actually works, but not 100%. Problems:

    1. It seems like the server hangs itself while downloading the page. Is there any way to fix that?
    2. It somehow looks wrong. ALTHOUGH in the serverlog.txt it says the right response without those weird "drawings" before the "hello" reponse.

    201305011859a1e8f19.png

    Thanks a lot in advance, I hope my problem is understandable and please keep in mind that I just started with Java a few days ago :)
     
  2. Offline

    CoderCloud

    Hi mike,
    I dont know what your second problem is or how to fix it, but i would use a new thread to load the website.
    Here is you can see how to use threads in bukkit.
     
  3. Offline

    mike2033

    Hello again! I wanted to ask if you can help me including my source under a thread. I don't understand how it works in Java.

    Thanks in advance
     
Thread Status:
Not open for further replies.

Share This Page