Results 1 to 20 of 31
Thread: [SOLVED] http request
- 03-13-2008, 09:55 AM #1
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
[SOLVED] 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
- 03-13-2008, 10:17 AM #2
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?
- 03-13-2008, 10:34 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 03-13-2008, 10:38 AM #4
- 03-13-2008, 11:27 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
- 03-13-2008, 11:29 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Just three lines to use.
What you think of it.Java Code:URL myRequest = new URL(my_url_goes_here); BufferedReader in = new BufferedReader(new InputStreamReader(myRequest.openStream())); String str = in.readLine(); in.close();
Eranga.
- 03-13-2008, 01:23 PM #7
- 03-14-2008, 03:46 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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. ;)
- 03-17-2008, 03:19 PM #9
Are you still going to put the full code as highlighted earlier, or you intend on sending a private msg?
:rolleyes:
- 03-19-2008, 09:37 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Aah,
Actually I'm saying about the exception handling. That's it. Ok, I'll put my code here.
- 04-08-2008, 10:39 AM #11
Hey aibtus - Here is a full code example:
Note the extra bit of code:Java 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(); } }
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! ;)Java Code:spoof.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)" );
If you wish to surf through a proxy server then you can also add this code to the top:
Java 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
me! :cool:
- 04-08-2008, 10:50 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Nice work, but no need to worry that much pal. Here is my code.
That's all. The replay caught here as a String.Java 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); }
- 04-08-2008, 10:53 AM #13
- 04-08-2008, 10:55 AM #14
Yeah I know... Just thought i'd add the extra code because you never know when you'll need it!Nice work, but no need to worry that much pal
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
me! :cool:
- 04-08-2008, 11:10 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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. :)
- 04-08-2008, 11:12 AM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Major thing I've forget to say is, default browser will be fired by the URL. Actually it bind with.
- 04-08-2008, 11:24 AM #17
Cool Cool :o
Did this post help you? Please
me! :cool:
- 04-08-2008, 11:31 AM #18
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ok, I got one scenario few days back. Say the html code comes with newline characters. Something like this,
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.Java Code:<html> <head></Head> </html>
Any idea you have?
- 04-08-2008, 11:36 AM #19
Yeah... Use this:
This will display all the code from the HTML page as it loops through it.Java Code:String strLine = ""; while ((strLine = in.readLine()) != null) { System.out.println(strLine); }Did this post help you? Please
me! :cool:
- 04-08-2008, 11:50 AM #20
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Hmm, I got the point. Say I want to return that String, I mean the whole String. Then what is the easiest way to do it.
Similar Threads
-
respose with out request
By karthikiniyan in forum Java ServletReplies: 1Last Post: 04-09-2008, 03:06 PM -
How to use request Scope in Spring
By JavaBean in forum Java TipReplies: 0Last Post: 09-28-2007, 12:46 PM -
Generate XML request from web form
By sabatier in forum XMLReplies: 1Last Post: 08-09-2007, 07:53 PM -
Help with request.getParameter()
By Albert in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 07-13-2007, 03:39 PM -
HttpServletRequest request size
By Ed in forum Java ServletReplies: 2Last Post: 07-02-2007, 02:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks