wood burning stoves 2.0
The moose likes Servlets and the fly likes forward to HttpUrlConnection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "forward to HttpUrlConnection" Watch "forward to HttpUrlConnection" New topic

forward to HttpUrlConnection

Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

I'm faced with constructing an HttpUrlConnection of method "POST" in code and using it to navigate, sending the user there as if they had submitted a form.

current steps:
1) User clicks a link on my web app
2)my webapp whips up a SAML document based on a number of parameters
3)my webapp adds the SAML document to the body of a programmatically created URLConnection of method "POST"
4) I use java.net.HTTPURLConnection to send the post to the remote server for authentication.

right now, the remote server happliy sends back HTML that scrolls by nicely in my server console window. my user, of course, needs to navigate to that remote site. If I follow up with a redirect or forward to that remote site, it generates another request, which isn't properly formed for authentication. it is a "GET", for starters.

I think I could also stick the document in as a request parameter, but I'm still faced with the central problem of how to navigate using a URLConnection. My server is making the request instead of the client, so I need another way around this.

I have thought of just making a form with hidden fields, but I don't think there is a way to alter the form's request parameters on the fly, or is there?

so perhaps I shouldn't use HttpUrlConnection. Is there a way to make a POST request on the fly and redirect to there? The purpose is to authenticate against a webapp firewall called Reactivity. Authentication is done through and encrypted, signed SOAP envelope
any ideas?
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
will this be of any help?

see the userguide and samples on the left hand side - that will give you some direction.

MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

Thank you, Lynnette,

I was considering using jakarta httpClient and still might, but I think it leaves me with the same problem. How do I make a request from the server and then redirect the client to that same resource without generating another request?
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

I think it's going to be hard to do this kind of spoofing in one request.
One workaround might be to return a page with a form and some javascript that submits the form from the user's browser from the page load event.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

Ben, that is exactly what I ended up doing. The initial server side post returned an HTML form that I pasted back into the browser with response.getWriter().write(theReturnedHTML) and it worked!
I agree. Here's the link: http://aspose.com/file-tools
subject: forward to HttpUrlConnection
jQuery in Action, 3rd edition