Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-13-2008, 10:55 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
http request
Hi all,

For the first time I'm going to do this.

I have a http URL, and I want to make a request through that URL. Response is a String, which I want to use for further processing.

Any comments appreciate.

Eranga
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-13-2008, 11:17 AM
aibtus's Avatar
Member
 
Join Date: Mar 2008
Location: Randburg, South Africa
Posts: 9
aibtus is on a distinguished road
Send a message via Yahoo to aibtus Send a message via Skype™ to aibtus
WHAT DO YOU MEAN BY "Response is as String, which I want to use for further processing. "

D you want to make a URL request via a web browser and get the response for further processing before rendering onto the browser itself or what?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-13-2008, 11:34 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Ok, I have a URL where I want to make a request. On that request, the URL, actually the server gives a response. Simply a IP which my network is belongs to.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-13-2008, 11:38 AM
aibtus's Avatar
Member
 
Join Date: Mar 2008
Location: Randburg, South Africa
Posts: 9
aibtus is on a distinguished road
Send a message via Yahoo to aibtus Send a message via Skype™ to aibtus
Quote:
Originally Posted by Eranga View Post
Ok, I have a URL where I want to make a request. On that request, the URL, actually the server gives a response. Simply a IP which my network is belongs to.
Cool. Now I understand.

Ok, what technology are you using ie: JSP's, Servlets etc???
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-13-2008, 12:27 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Actually I just use simple Java technologies. And I have done it myself. It's not difficult at all. Simply call URL.openStream() to get the replay to a stream buffer. Then store it in a String variable for later user.

So, my problem is solved. Thanks for all who replay and view my post.

Eranga
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 03-13-2008, 12:29 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Just three lines to use.

Code:
URL myRequest = new URL(my_url_goes_here); BufferedReader in = new BufferedReader(new InputStreamReader(myRequest.openStream())); String str = in.readLine(); in.close();
What you think of it.

Eranga.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 03-13-2008, 02:23 PM
aibtus's Avatar
Member
 
Join Date: Mar 2008
Location: Randburg, South Africa
Posts: 9
aibtus is on a distinguished road
Send a message via Yahoo to aibtus Send a message via Skype™ to aibtus
Quote:
Originally Posted by Eranga View Post
Just three lines to use.

Code:
URL myRequest = new URL(my_url_goes_here); BufferedReader in = new BufferedReader(new InputStreamReader(myRequest.openStream())); String str = in.readLine(); in.close();
What you think of it.

Eranga.

It looks simple enough and you say It works... I love it!!
Is the class URL a custom class --- if not, can you give us the full package name. It may come in handy in some other projects. If it is, can you highlight the implementation..

Nice code though!
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 03-14-2008, 04:46 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Actually nothing much I used. Just import import java.net.URL;

Rather than, you have to handle all the exceptions. You want the full code which I have work on, if so I'll put it here after completing all.

Me too, love this code. It's so simple, really simply. Even you handle all the exceptions, only need to write around 10 to 15 lines.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 03-17-2008, 04:19 PM
aibtus's Avatar
Member
 
Join Date: Mar 2008
Location: Randburg, South Africa
Posts: 9
aibtus is on a distinguished road
Send a message via Yahoo to aibtus Send a message via Skype™ to aibtus
Are you still going to put the full code as highlighted earlier, or you intend on sending a private msg?
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 03-19-2008, 10:37 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Aah,

Actually I'm saying about the exception handling. That's it. Ok, I'll put my code here.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 04-08-2008, 11:39 AM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 237
DonCash will become famous soon enoughDonCash will become famous soon enough
Hey aibtus - Here is a full code example:

Code:
import java.net.*; import java.io.*; class httpconnect { public static void main(String[] args) throws Exception { URL url = new URL("http://www.google.co.uk/"); URLConnection spoof = url.openConnection(); spoof.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)" ); BufferedReader in = new BufferedReader(new InputStreamReader(spoof.getInputStream())); String strLine = ""; while ((strLine = in.readLine()) != null) { System.out.println(strLine); } in.close(); } }
Note the extra bit of code:

Code:
spoof.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)" );
Websites like Google block connections to their site from programs other than web browsers. With this bit of code you can trick sites like these into thinking the connection is coming from a web browser. Very handy!

If you wish to surf through a proxy server then you can also add this code to the top:

Code:
System.getProperties().put("proxySet", "true"); System.getProperties().put("proxyPort", "80"); System.getProperties().put("proxyHost", "my.proxyserver.co.uk");
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|| Don't forget to:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 04-08-2008, 11:50 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Nice work, but no need to worry that much pal. Here is my code.

Code:
try{ URL myRequest = new URL(buildRequestURL()); //URL myRequest = new URL(requestURL); BufferedReader inMessage = new BufferedReader(new InputStreamReader(myRequest.openStream())); String httpReplay = inMessage.readLine(); System.out.println(httpReplay); return httpReplay; } inMessage.close(); } catch(MalformedURLException ex){ System.out.println(ex); } catch(IOException ex){ System.out.println(ex); }
That's all. The replay caught here as a String.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 04-08-2008, 11:53 AM
aibtus's Avatar
Member
 
Join Date: Mar 2008
Location: Randburg, South Africa
Posts: 9
aibtus is on a distinguished road
Send a message via Yahoo to aibtus Send a message via Skype™ to aibtus
Way to go
Thanx a lot. I will try it out and best comment later.


Quote:
Originally Posted by DonCash View Post
Hey aibtus - Here is a full code example:

Code:
import java.net.*; import java.io.*; class httpconnect { public static void main(String[] args) throws Exception { URL url = new URL("http://www.google.co.uk/"); URLConnection spoof = url.openConnection(); spoof.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)" ); BufferedReader in = new BufferedReader(new InputStreamReader(spoof.getInputStream())); String strLine = ""; while ((strLine = in.readLine()) != null) { System.out.println(strLine); } in.close(); } }
Note the extra bit of code:

Code:
spoof.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)" );
Websites like Google block connections to their site from programs other than web browsers. With this bit of code you can trick sites like these into thinking the connection is coming from a web browser. Very handy!

If you wish to surf through a proxy server then you can also add this code to the top:

Code:
System.getProperties().put("proxySet", "true"); System.getProperties().put("proxyPort", "80"); System.getProperties().put("proxyHost", "my.proxyserver.co.uk");
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 04-08-2008, 11:55 AM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 237
DonCash will become famous soon enoughDonCash will become famous soon enough
Quote:
Nice work, but no need to worry that much pal
Yeah I know... Just thought i'd add the extra code because you never know when you'll need it!

The code to trick websites into thinking you are a web browser needs to be used more and more nowdays as sites actively block connections.
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|| Don't forget to:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 04-08-2008, 12:10 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Yes you are correct. At the time I need such a code, just to communicate with my other machine. Time to time I can develop it and I can use it as one of my libraries actually.

And also your code may really helpful to me develop my code segment.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 04-08-2008, 12:12 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Major thing I've forget to say is, default browser will be fired by the URL. Actually it bind with.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 04-08-2008, 12:24 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 237
DonCash will become famous soon enoughDonCash will become famous soon enough
Cool Cool
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|| Don't forget to:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 04-08-2008, 12:31 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Ok, I got one scenario few days back. Say the html code comes with newline characters. Something like this,
Code:
<html> <head></Head> </html>
Because of my readLine() only the <html> tag detected. How can I get whole the code. If I have that code as a single String it works.

Any idea you have?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 04-08-2008, 12:36 PM
DonCash's Avatar