Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Java HTTPURLConnection

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java HTTPURLConnection

    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)

    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 my application hangs itself while downloading the page. Is there any way to fix that?
    2. I attached a picture of how the response looks. ALTHOUGH in the log.txt it says the right response without those weird "drawings" before the "hello" reponse.

    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 :)
    Attached Images Attached Images


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java HTTPURLConnection

    Please make a small, complete program that compile, executes and shows the problem for testing.

    Copy the full text of the console from when the program is executed and paste that here also.


    my application hangs itself while downloading the page.
    Add some println statements to print out messages as the code executes to show where the statements are executed and to show the last statement that was executed. Include printing the Strings as they are read.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java HTTPURLConnection

    Hello! Thanks for your answer. So you mean add println("Some status") after every line?

    Greets

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java HTTPURLConnection

    Yes, something like that would show the where the execution is hanging.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java HTTPURLConnection

    Quote Originally Posted by Norm View Post
    Yes, something like that would show the where the execution is hanging.
    Alright. I tried it like that:

    			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");
    			Logger.getLogger("Minecraft").info("Task 0");
     
    			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    			Logger.getLogger("Minecraft").info("Task 1");
    			String inputLine;
    			Logger.getLogger("Minecraft").info("Task 2");
    			StringBuffer html = new StringBuffer();
    			Logger.getLogger("Minecraft").info("Task 4");
    			while ((inputLine = in.readLine()) != null) {
    				html.append(inputLine);
    				Logger.getLogger("Minecraft").info("Task 5");
    			}

    As you can see here it stucks at Task 0

    20:42:55 [INFO] Task 0
    20:42:57 [INFO] Task 1
    20:42:57 [INFO] Task 2
    20:42:57 [INFO] Task 4
    20:42:57 [INFO] Task 5

    In C# I would try it with threading, but I don't know if I'm good enough in Java to do so and I'm not even sure if it's possible

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java HTTPURLConnection

    The print out shows that it executed Tasks 0 through 4 and then Task 5 only one time. Task 5 being in a loop should print out one time for every time around the loop.
    Is that all the print out?

    Also need to print out what was read into the inputLine String.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java HTTPURLConnection

    Quote Originally Posted by Norm View Post
    The print out shows that it executed Tasks 0 through 4 and then Task 5 only one time. Task 5 being in a loop should print out one time for every time around the loop.
    Is that all the print out?

    Also need to print out what was read into the inputLine String.
    First, sorry that I didn't do it with prinl. I didn't know how to use it but I hope that my methode there worked too. And then I just see that I forget Task number 3, so I changed the code. Now the source looks like this:

    			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");
    			Logger.getLogger("Minecraft").info("Task 0");
     
    			BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    			Logger.getLogger("Minecraft").info("Task 1");
    			String inputLine;
    			Logger.getLogger("Minecraft").info("Task 2");
    			StringBuffer html = new StringBuffer();
    			Logger.getLogger("Minecraft").info("Task 3");
    			while ((inputLine = in.readLine()) != null) {
    				html.append(inputLine);
    				Logger.getLogger("Minecraft").info("Task 4"+inputLine);
    			}
    			in.close();

    It looks like the inputLine is the source code of the website too. Oh, and I just tried it on a local webserver, seems like the application hangs because of the server. On localhost it's much waster, but there was an 1 second delay anyway after Task 0

    EDIT: If you have Eclipse and Saros you can join me and take a look yourself!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java HTTPURLConnection

    I didn't know how to use it
    Add lines like this to the code:
    System.out.println("the msg");

    What are the last few lines that are printed out? At what statement is the code hanging?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    May 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java HTTPURLConnection

    Quote Originally Posted by Norm View Post
    Add lines like this to the code:
    System.out.println("the msg");

    What are the last few lines that are printed out? At what statement is the code hanging?
    That's the source code right now:

    				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");
    				System.out.println("Task 0");
     
    				BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    				System.out.println("Task 1");
    				String inputLine;
    				System.out.println("Task 2");
    				StringBuffer html = new StringBuffer();
    				System.out.println("Task 3");
    				while ((inputLine = in.readLine()) != null) {
    					html.append(inputLine);
    					System.out.println("Task 4"+inputLine);
    				}
    				in.close();

    Hi, as you can see it stucks right at the beginning

    22:07:53 [INFO] Task 0
    22:07:56 [INFO] Task 1
    22:07:56 [INFO] Task 2
    22:07:56 [INFO] Task 3
    22:07:56 [INFO] Task 4´╗┐Hello // What is ´╗┐?

    Maybe you know Minecraft, it's the Bukkit server and it says "Server overloaded" after some loading time while checking the source code of the website

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java HTTPURLConnection

    it stucks right at the beginning
    Do you mean it hangs after reading just one line, when trying to read the second line.

    Try the code with another site's URL
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    May 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java HTTPURLConnection

    Quote Originally Posted by Norm View Post
    Do you mean it hangs after reading just one line, when trying to read the second line.

    Try the code with another site's URL
    Like I said some posts before: Oh, and I just tried it on a local webserver, seems like the application hangs because of the server. On localhost it's much faster.

    And it writes Task 0 then waits some time and then writes Task 1,2,3,4 at a time... However, I think using another server is not a good solution. If the other server will have some heavier loads it will stuck and give errors there too. BUT it have problems getting the right source code there too. Always this weird symbols

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java HTTPURLConnection

    I think using another server is not a good solution.
    What happens when you try a server like google?
    The code works fine for me with the google site.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    May 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java HTTPURLConnection

    Quote Originally Posted by Norm View Post
    What happens when you try a server like google?
    The code works fine for me with the google site.
    Try it with my actual server: http://screens500.funpic.de/code.htm

    And yes, you are right. Somehow it works with Google. I'm not sure what's wrong my webpage. Please take a look

    EDIT: And is it possible to let the command only run like every few settings? So that the program will not be spammed.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java HTTPURLConnection

    This is what is returned by that site as bytes: [-17, -69, -65, 72, 101, 108, 108, 111]
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    mike2033 (May 2nd, 2013)

  16. #15
    Junior Member
    Join Date
    May 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java HTTPURLConnection

    Quote Originally Posted by Norm View Post
    This is what is returned by that site as bytes: [-17, -69, -65, 72, 101, 108, 108, 111]
    Hm, can you translate that into ASCII please?

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java HTTPURLConnection

    Find an ASCII table for the numeric char values.

    The negative values are not ASCII characters.

    Or use cast:
          System.out.println((char)101);
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Junior Member
    Join Date
    May 2013
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java HTTPURLConnection

    Quote Originally Posted by Norm View Post
    The negative values are not ASCII characters.
    The negative values are the problem, the rest is right. 71 101 108 108 111 means Hello. Damn, why does the webserver send that

    EDIT: I just tried it with my local server again, he sends the same

Similar Threads

  1. HttpURLConnection doesnt quite cut it
    By chopficaro in forum Java Theory & Questions
    Replies: 2
    Last Post: August 23rd, 2012, 08:48 AM
  2. Replies: 7
    Last Post: June 6th, 2012, 03:16 PM
  3. Passing session ID over HttpURLConnection
    By zuzacat in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 20th, 2011, 09:18 AM
  4. HttpURLConnection null pointer exception
    By chopficaro in forum Java Theory & Questions
    Replies: 0
    Last Post: May 10th, 2010, 10:19 AM
  5. java.net.HttpURLConnection:large file to upload
    By tommy_725 in forum Java Networking
    Replies: 1
    Last Post: October 28th, 2009, 11:53 AM