aspose file tools
The moose likes Beginning Java and the fly likes HttpURLConnection - Luanch Browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HttpURLConnection - Luanch Browser" Watch "HttpURLConnection - Luanch Browser" New topic

HttpURLConnection - Luanch Browser

Ron Khera

Joined: Jun 27, 2005
Posts: 4
Hi folks

I am creating an application to send cXML documents using a HttpURLConnection. My problem is that a response document I receive contains a string url that I need to luanch in a browser. When I copy the url into my browser its get http 401 error. How can I luanch a browser from my java app. Is there any connection info in the response needed to luanch the browser ? Below is my code with the response.

*** Headers ***
Content-Type: text/xml
Set-Cookie: ssnid=; path=/;
Connection: Keep-Alive
Content-Length: 765

*** Connection Detail ***
Follow Redirects: true
Request Method: POST
getContentType: text/xml
getContentLength: 765
getContentEncoding: null
getDate: 0
getExpiration: 0
getLastModifed: 0
Response Code: 200
Response Message: OK

*** Content ***
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE cXML SYSTEM ""> <cXML version="1.1" payloadID="20050810.133143_627804_uscobrmfa-sw-04" timestamp="2005-08-10T13:31:43-06:00"> <Response> <Status code="200" text="OK"/> <PunchOutSetupResponse> <StartPage> <URL></URL> </StartPage> </PunchOutSetupResponse> </Response></cXML>

The URL tag should be launchable (new word)

This is my HTTP POST method.

public StringBuffer post() {
// StringBuffer will contain result of post or any exception thown
StringBuffer result = new StringBuffer();

try {
// URL must use the http protocol!
URL url = new URL(urlName);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

// HTTP Method and options

// Set Headers
conn.setRequestProperty("Content-Type", "text/xml; charset=\"utf-8\"");
conn.setRequestProperty("Content-length", Integer.toString(postData.length()));

// Get the output stream to POST our data
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(),"UTF8");

// Display Headers
if(dspHeaders) {

// Store the response in the result StringBuffer
InputStream rawInStream = conn.getInputStream();
BufferedReader rdr = new BufferedReader(new InputStreamReader(rawInStream, "UTF8"));
String line;
while ((line = rdr.readLine()) != null) {
} catch (Exception e) {
result.append("** Post Error: " + e + " **\n");
return result;
public StringBuffer getHeaders(HttpURLConnection cn) {
StringBuffer sb = new StringBuffer();
try {
int n = 1;
String key;
sb.append("*** Headers ***\n");
while ((key = cn.getHeaderFieldKey(n)) != null) {
String value = cn.getHeaderField(n);
sb.append(key + ": " + value + "\n");
sb.append("\n*** Connection Detail ***\n");
sb.append("Follow Redirects: "+cn.getFollowRedirects()+"\n");
sb.append("Request Method: "+cn.getRequestMethod()+"\n");
sb.append("getContentType: " + cn.getContentType()+"\n");
sb.append("getContentLength: " + cn.getContentLength()+"\n");
sb.append("getContentEncoding: " + cn.getContentEncoding()+"\n");
sb.append("getDate: " + cn.getDate()+"\n");
sb.append("getExpiration: " + cn.getExpiration()+"\n");
sb.append("getLastModifed: " + cn.getLastModified()+"\n");
sb.append("Response Code: "+cn.getResponseCode()+"\n");
sb.append("Response Message: "+cn.getResponseMessage()+"\n");
sb.append("\n*** Content ***\n");
catch (Exception e) {
sb.append("** getHeader Errors : "+e);
return sb;
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
To lauch a Browser you could try to look at Runtime in java.lang. IE executable is iexplore.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
That 401 means the server doesn't want to show you that page. Opening a new browser isn't likely to help. Are you able to navigate to the target page using a browser, maybe starting from a menu or logging in or something? The server may be looking for cookies or referrer in the header.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 691
On Windows the following command will launch the systems default browser. Not everyone uses IE.
Put this in the command string
cmd = "rundll32 url.dll,FileProtocolHandler " + url; //eg http://...'
I agree. Here's the link:
subject: HttpURLConnection - Luanch Browser