Results 1 to 15 of 15

Thread: http request

  1. #1
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Question http request

    Hi All,

    I am developing a Java applet and can not get over this hurdle. In my app I have a text field the user can enter text then click a submit button. Once that user has clicked submit it will do a Google search on what that user has entered. Can some one point me in the direction of what I need to do. Providing code snippets or links to helpful sites would be much appreciated!

    I think I have to somehow enter what the user has entered within the URL in an http request with the use of a variable?

    Thanks!

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    I would have a look at the HttpComponents library. It used to be called commons-httpclient. This API has a lot of useful features for generating GET or POST requests using various kinds of parameter for the request and parsing the response.

    They have some examples here.

  3. #3
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    So it appears i got something back in the form of html code in the console?

    How do I use this now? what are my options of displaying the results to the user of the stuff they searched for?

    Seems like it would be alot of info to display on the applet if I was to show what the searched returned within the applet, plus it seems to be in html.

    Maybe, once the user clicks submit, I could have it open up in the browser displaying the results of what they searched?

  4. #4
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    Still looking for an answer...

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    What is it you're trying to accomplish? Is this an exercise or a real-world problem? Doing a Google search from an applet seems like a strange thing to want to do, unless it's an exercise to learn Java web interaction.

    -Gary-

  7. #7
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    its an exercise for a real world problem

    as of now though i get code back in my console in what seems to be html code, how can i use the data received that gets sent to the console?

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by nobleman_james View Post
    its an exercise for a real world problem

    as of now though i get code back in my console in what seems to be html code, how can i use the data received that gets sent to the console?
    How do you want to use it? HTML is text -- you need to parse out the parts you want, and throw away the parts you don't. Maybe give an example of what you're getting and what you want.

    -Gary-

  9. #9
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    i guess you can try to parse the HTML received using a HTML parser. Or have an embedded web browser inside the applet such as the lobo browser kind of thing to render the content for you. I'm not sure how if any is possible to use the applet to fetch something and then have it displayed in a regular web browser, without instructing the browser to just do the request in the first place.

  10. #10
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    in my applet i have a text-field and a button. when the user clicks submit, it takes whats in the text field and does a Google search with it. if the user entered apples in the text field it would do a Google search for apples. Then I would like a new window or tab to open up with the Google results listed that were returned.

  11. #11
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    When I run the request with a Google search URL I get this message:

    ---
    java.io.IOException: Server returned HTTP response code: 403 for URL:
    ---

    This is a snippet of my code:

    URL google = new URL("http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=apples");

    Where apples is I would probably pass a variable with the content from the text field. I feel my code is right, but its something to do with Google I have not set up with all their lovely terms and conditions stuff.

    From Google's site: (CS Custom Search)
    ---
    2.2 Interaction with CS Servers. The CS Implementation must not interfere with the normal functioning of the CS Servers, and Customer acknowledges that Google may monitor CS API activity in order to prevent such interference. Except as expressly permitted by the API Documentation, Customer will not use any automated means (such as the use of scraping, robots or load testing) to access, query or otherwise collect CS related information. Google may set usage limits on features of the CS API and may charge for usage that exceeds these limits.
    ---

    At this time i just want to get some search results from Google, then i can sort out how to display this content.

  12. #12
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    Quote Originally Posted by travishein View Post
    I would have a look at the HttpComponents library. It used to be called commons-httpclient. This API has a lot of useful features for generating GET or POST requests using various kinds of parameter for the request and parsing the response.

    They have some examples here.
    I just made a new post before I realized the similarity of this thread. So is this httpcomponent a standard library that my apache already has? I am running apach2.2 and tomcat6 side by side....

  13. #13
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    I need some help clarifying...so because I have apache installed my JVM will find the httpClient class when I compile? these examples are java code so are they on a JSP server side? or is this a J2ee class? I am reading this stuff about HttpClient and it is java code so where do I implement this code?
    Last edited by aaroncarpet; 12-17-2010 at 05:59 PM.

  14. #14
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    usually these http client api is not part of tomcat. you would need to have the jar file (downloaded and included in class path when compiling with ant, maven, etc) and then installed into tomcat, or shipped into your .war file when deploying.

    the java code can be ran from within a servlet, or a stand alone java object you might have as a singleton service, or wired up from spring framework, etc.

  15. #15
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    This is a little bit too layered for me at this time. what I would like to do is use the stuff in j2se java.net sockets and practice server client communication...I need examples of applets that use these methods to communicate back to the server....does anyone know where I can find examples like this....I tried downloading httpclient should I extract it in java folder? and put the full name of the jar in the classpath?......I think I am giving up on that one for now i need more building blocks to understand
    Last edited by aaroncarpet; 12-17-2010 at 07:33 PM.

Similar Threads

  1. Http request via applet
    By hoda in forum Java Applets
    Replies: 0
    Last Post: 11-20-2010, 10:07 AM
  2. server java HTTP request
    By kptN in forum New To Java
    Replies: 0
    Last Post: 10-22-2010, 03:42 PM
  3. HTTP Post Request from Java
    By Darrarski in forum New To Java
    Replies: 10
    Last Post: 06-05-2010, 05:22 PM
  4. Http request
    By arthik_babu in forum Advanced Java
    Replies: 2
    Last Post: 06-29-2009, 12:27 PM
  5. [SOLVED] http request
    By Eranga in forum Advanced Java
    Replies: 30
    Last Post: 11-27-2008, 04:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •