pass parameter from a website?
I'm writing a java application which is highly coupled to a website. The program has functionality which launches a browser and displays the website... and I can pass information to the site via the query string in this way. I'm running into a problem, however, with passing information back to the java application from the website. Does anyone know how if/how this can be achieved?
[392 byte] By [
bjb39a] at [2007-11-27 5:23:32]

Well, maybe you could look for tools so that you can use an internal browser instead of launching one besides. This way you could probably have access to the html content which is being sent to your browser.
Unfortunately using an internal browser isn't really an option. The only realistic way I can think of doing this is to have the website write out information to a file, and have the java application poll this for new data... although this solution is quite crude. Any thoughts?
bjb39a at 2007-7-12 11:49:26 >

> I'm writing a java application which is highly
> coupled to a website. The program has functionality
> which launches a browser and displays the website...
> and I can pass information to the site via the query
> string in this way. I'm running into a problem,
> however, with passing information back to the java
> application from the website. Does anyone know how
> if/how this can be achieved?
You could use an [url=http://java.sun.com/j2se/1.4.2/docs/api/java/net/HttpURLConnection.html]HttpUrlConnection[/url] to get the page, if it's not important that you see the returned HTML in a browser. The HTML returned will come back to you as an InputStream or in a String, and you can do with that whatever you like.
The user is going to be entering all kinds of information through a form, and I need this to be passed back to the Java application when the user hits a "Submit" button. Can HttpUrlConnection look for these types of form submit requests?
bjb39a at 2007-7-12 11:49:26 >

> The user is going to be entering all kinds of
> information through a form, and I need this to be
> passed back to the Java application when the user
> hits a "Submit" button. Can HttpUrlConnection look
> for these types of form submit requests?
Yeah, for that, you set the [url=http://java.sun.com/j2se/1.4.2/docs/api/java/net/HttpURLConnection.html#setRequestMethod(java.lang.String)]setRequestMethod()[/url] method to POST the form, instead of using the default GET request.
You won't be able to know the information the user filled inside the browser's form unless you setup your java application as an HttpProxy so that you can filter everything sent to the browser, and everything sent to the web server.
> You won't be able to know the information the user
> filled inside the browser's form unless you setup
> your java application as an HttpProxy so that you can
> filter everything sent to the browser, and everything
> sent to the web server.
Never thought about using a proxy.
Here's an example from koders.com:
http://www.koders.com/java/fid5B4AE58FBE92FA1EB5C7A8BCA785AA6B42BA016D.aspx
So wait.. let me get this straight. The suggested approach is to write a local HTTP proxy through which the browser will pass information to the website? The application would spawn off this thread, and listen for specific get/post requests and generate an event trigger when designated information is sent through the proxy?
This seems like a very complicated approach, but realistically it seems as though it could work. My only question is whether the browser would have to have specific settings so that it can route connections through the proxy, or can I just point it at http://localhost/... and have it go through the proxy?
bjb39a at 2007-7-12 11:49:27 >

> So wait.. let me get this straight. The suggested
> approach is to write a local HTTP proxy through which
> the browser will pass information to the website? The
> application would spawn off this thread, and listen
> for specific get/post requests and generate an event
> trigger when designated information is sent through
> the proxy?
>
> This seems like a very complicated approach, but
> realistically it seems as though it could work. My
> only question is whether the browser would have to
> have specific settings so that it can route
> connections through the proxy, or can I just point it
> at http://localhost/... and have it go through the
> proxy?
It really depends on whether you need the user to use the browser. I'm uncertain of your requirements... are you trying to make a Java application that intercepts a user's web browsing activity in order to pluck out parameters? Rather, are you trying strictly to make a Java app that inputs parameters and makes a web request on the user's behalf?
In the case of the first:
1. Fire up IE to local address, pointing to HttpProxy.
2. IE makes request to HttpProxy.
3. HttpProxy plucks out your parameters that you were wanting to intercept.
4. HttpProxy makes request (using HttpUrlConnection) to the real web site.
5. Search response to determine whether or not request succeeded.
6. The real web site responds to the HttpProxy.
7. The HttpProxy serves the response directly back to IE.
In the case of the second:
1. Swing form prompts user for parameters.
2. Parameters from Swing form are used to craft an HttpUrlConnection.
3. Request is made to webserver using HttpUrlConnection.
4. Search the response to determine whether or not your request succeeded.
Basically I'm working on a Java application which needs to be heavily integrated with a PHP based web application. There will be certain buttons placed throughout the PHP code which need to call methods within the Java application. Similarly, information needs to be passed from the Java application to the PHP websites as well.. although I've handled this by passing parameters through the query string using browser launching utilities of Java 6's Desktop object.
bjb39a at 2007-7-12 11:49:27 >

> Basically I'm working on a Java application which
> needs to be heavily integrated with a PHP based web
> application. There will be certain buttons placed
> throughout the PHP code which need to call methods
> within the Java application. Similarly, information
> needs to be passed from the Java application to the
> PHP websites as well.. although I've handled this by
> passing parameters through the query string using
> browser launching utilities of Java 6's Desktop
> object.
You might want to look into XML-RPC.
http://www.faqs.org/docs/Linux-HOWTO/XML-RPC-HOWTO.html
I've used it in the past, and it's a little intimidating to look at, but isn't really as difficult as it first looks. Plus, it's quite a bit more fault tolerant than scraping HTML responses.
I've done very similar things to what it appears you're talking about, although I'm not sure about what you mean by the fact that the PHP site has to call the Java application.
> So wait.. let me get this straight. The suggested
> approach is to write a local HTTP proxy through which
> the browser will pass information to the website? The
> application would spawn off this thread, and listen
> for specific get/post requests and generate an event
> trigger when designated information is sent through
> the proxy?
>
> This seems like a very complicated approach, but
> realistically it seems as though it could work. My
> only question is whether the browser would have to
> have specific settings so that it can route
> connections through the proxy, or can I just point it
> at http://localhost/... and have it go through the
> proxy?
This idea is very clever. Indeed, you could have your Java Application look like the actual web server by listening on a specific port (maybe even 80) on localhost and then perform any necessary operations on the data being sent before sending requests to the real web server. The same thing works on the other side, you will receive data from the web server and you must forward it to the web browser after you performed any actions on the content.
This approach will prevent the necessity of using browser configurations to use a proxy which would have been required with my original suggestion. And this way, your Java application can easily launch the browser with the address pointing to the port it is listening on!
> This idea is very clever. Indeed, you could have your Java Application
> look like the actual web server by listening on a specific port (maybe
> even 80) on localhost and then perform any necessary operations on
> the data being sent before sending requests to the real web server.
Wouldn't this generate problems with IIS? Or does it not matter so long as it's only really talking to itself.
bjb39a at 2007-7-12 11:49:27 >

> You might want to look into XML-RPC.This looks very complicated, but I'll take a look... thanks a lot!
bjb39a at 2007-7-12 11:49:27 >

> Wouldn't this generate problems with IIS? Or does it
> not matter so long as it's only really talking to
> itself.
It would generate problems with IIS if they are hosted on the same machine. If you want to make sure it can be used anywhere, just use one of the non-standard ports (>7000 is pretty safe, I think the real value is close to 6000+).
The proxy on localhost is a nifty idea.
The only other way (I can think of) would be that your application maintains a connection to the server which can notify it when the user is done submitting the web page.
Not sure either would be simpler then the other, all things considered.
Message was edited by:
bsampieri