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
Author

HttpURLConnection - Luanch Browser

Ron Khera
Greenhorn

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 "http://xml.cxml.org/schemas/cXML/1.1.009/cXML.dtd"> <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>https://www.eway.com/eway/celogin.p_ce_eway_b2b_punchout?userid=0000000BB689431ACCFD1BBADE1FFFD7D22A2AEE0005E83C0000000015CD&b2b_profile_id=5939734&sessionId=0000000A6273ED3D5DAE7D35BEC9A6A4738E40500005E83C0000000015CD&operation=edit&webmethods_cart_url=https%3A%2F%2Fb2bprod.cexp.com%2Finvoke%2Fprocess%2Freceive_punchOutCart&exit_url=X</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
conn.setRequestMethod("POST");
conn.setAllowUserInteraction(false);
conn.setDoOutput(true);

// 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");
out.write(postData);
out.flush();
out.close();

// Display Headers
if(dspHeaders) {
result.append(getHeaders(conn).toString());
}

// 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) {
result.append(line);
}
} 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");
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: 686
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://...'
Runtime.getRuntime().exec(cmd);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HttpURLConnection - Luanch Browser