CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Aug 2010
    Posts
    7

    Java - URL connection accessing Java Scrpit Function

    Hey Guys,

    I am trying to access a function at the following website - http://www.delisted.com.au/listed.aspx
    There is a button there (only one) that will enable me to view all the data i need, however i am having a lot of trouble calling that java script function from my java program.
    From What i can tell the function is called - javascript:__doPostBack('btnShowAll','')

    Here is what i have tried to do (trust me Ive tried lots of variations) -
    /////////////////////////////////////////////////

    URL servAddress = new URL("http://www.delisted.com.au?javascript:__doPostBack('btnShowAll','')");
    URLConnection add = servAddress.openConnection();

    URLConnection add = servAddress.openConnection();
    BufferedReader in = new BufferedReader( new InputStreamReader(add.getInputStream()));

    while ((companyData = in.readLine()) != null)
    {
    System.out.println("HTML: "+companyData);
    }

    ///////////////////////////////////////////////

    From what i know about javascript (not much) once i have the connection open (as i would in a standard internet browser) i should be able to remove the url and call the function directly - see http://www.youtube.com/watch?v=bNBWvaLKSYw. But so far that has not worked either.

    Thanks for any help in advance.

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Java - URL connection accessing Java Scrpit Function

    I don't see any code in what you've posted that would call a javascript function. I'm not sure that has any meaning outside of a browser.

    The posted code appears to read and print what it gets from the website.

    Can you explain how you expect to "call a javascript function" by reading the HTML source lines?
    Norm

  3. #3
    Join Date
    Aug 2010
    Posts
    7

    Re: Java - URL connection accessing Java Scrpit Function

    Hey,

    Ive tried many different types of URL connections such as -

    URL servAddress = new URL("http://www.delisted.com.au?javascript:__doPostBack('btnShowAll','')");

    URL servAddress = new URL("http://www.delisted.com.au#javascript:__doPostBack('btnShowAll','')");

    URL servAddress = new URL("http://www.delisted.com.au/javascript:__doPostBack('btnShowAll','')");

    URL servAddress = new URL("http://www.delisted.com.au/javasc#ipt:__doPostBack('btnShowAll','')");

    URL servAddress = new URL("http://www.delisted.com.au?javascript:__doPostBack('btnShowAll','0')");

    etc etc

    Sorry i didnt post, there were way to many and i wasnt even sure if i was heading in the right direction.

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Java - URL connection accessing Java Scrpit Function

    What do you expect the server to return with those URLs?
    I wouldn't expect the server to act like a browser: read the html, scan for a JS function in the html, execute the function and return the results.
    Norm

  5. #5
    Join Date
    Aug 2010
    Posts
    7

    Re: Java - URL connection accessing Java Scrpit Function

    I am trying to get the results of the button (java script). That is the browser has the same url (once the button has been selected) however 2500 companies are listed instead of a simple page with a button on it.
    I was trying to find a library that would allow my program to act like a browser (in the sence i could call java script functions - act like a user clicking the button) or in the examples i just posted accessing the java script function directly via a direct URL value.

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Java - URL connection accessing Java Scrpit Function

    Sorry, I have no idea if there are libraries that allow a java program to "execute javascript" functions from html source lines that are read by the java program.

    What is it you want to have the server return?
    Norm

  7. #7
    Join Date
    Aug 2010
    Posts
    7

    Re: Java - URL connection accessing Java Scrpit Function

    The full data from that page ( a list of 2500 companies listed with the ASX with their current trading status). I'm making a stock market competition program and this is the only source i can find for this data, just that button is not allowing me to view it via my java app.

  8. #8
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Java - URL connection accessing Java Scrpit Function

    What HTTP GET/POST request is generated by the button press?
    Can you capture the URL and GET/POST request sent by the browser when the button is pressed and use that in your program?
    Does the button press generate an https or http request?
    Can you use a proxy server that will capture the request sent from the browser using that proxy server when the button is pressed?
    Norm

  9. #9
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Java - URL connection accessing Java Scrpit Function

    Last edited by Norm; August 7th, 2010 at 04:56 PM.
    Norm

  10. #10
    Join Date
    Aug 2010
    Posts
    7

    Re: Java - URL connection accessing Java Scrpit Function

    I have no idea, if you haven't already, please have a look at the site - http://www.delisted.com.au/listed.aspx - its a very simple page with a single button, it wont take you more than a few minutes to see whats happening in source code view.
    Also why does it matter if I've "Cross Posted"? Its just asking more people the same question.

  11. #11
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Java - URL connection accessing Java Scrpit Function

    I have looked at that page and seen its HTML and javascript. The responses to your post on the sun/oracle site describes what you need to do.

    why does it matter if I've "Cross Posted"?
    Because: why should I waste my time researching and posting an answer that has already been posted on another forum???
    You should post links to the other sites so we can see the progress on this topic and not waste our time.
    Norm

  12. #12
    Join Date
    Aug 2010
    Posts
    7

    Re: Java - URL connection accessing Java Scrpit Function

    The Post from the oracle website shows me how i can get that running with other applications, and shows me how to run java scripts on my own local host, however if you research into each of those posts a bit further you will find they still don't solve my issue of executing a java script functions results from my java app from an external source.

    As for the cross posting thing, next time ill be sure to update, sorry about that.

  13. #13
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Java - URL connection accessing Java Scrpit Function

    my issue of executing a java script functions results from my java app from an external source.
    You can read a JS file and execute it in a java app. the post on javaprogrammingforums gave a link to a site that said how: http://java.sun.com/developer/techni...top/scripting/

    This has nothing to do with an HTML page containing some JS functions and a <FORM and simulating the response a browser would make if you press the button shown on the page.
    Norm

  14. #14
    Join Date
    Aug 2010
    Posts
    7

    Re: Java - URL connection accessing Java Scrpit Function

    Hey,

    Just for reference i thought i would post my solution - very little out there for this kind of thing (from what i saw).

    Anyway, i wanted to access a java script function within a HTML page and couldn't seem to using standard URL connections, so I found the htmlunit package - http://htmlunit.sourceforge.net/ - which allows your java application act like a web browser (that is click-able items, execute java script functions or full applications etc). Before you use it, just to let you know there is not alot of examples out there for this library yet but i found one good example that helped me - http://selenium.googlecode.com/svn-h...nitDriver.java.

    I could not get the function executeJavaScriptFunctionIfPossible working so i found a method which allows you to "click" on an object, essentially making your app act like a user.

    And here is the code i used to extract the java script results to a string -

    Code:
    String companyData;
    
    final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_8);
    webClient.setJavaScriptEnabled(true);
    
    HtmlPage page = webClient.getPage("http://www.delisted.com.au/listed.aspx");
    page = (page.getElementById("btnShowAll").click());
                
    companyData = page.asText();
    System.out.println("RESULTS: "+ companyData);
    where "btnShowAll" would be the id tag of the item you want to click on the page your trying to extract data from.

    Hope this helps anyone with similar issues.
    Last edited by virtual void; August 8th, 2010 at 07:53 AM.

  15. #15
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Java - URL connection accessing Java Scrpit Function

    thanks for the info.
    Norm

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured