• Post Reply Bookmark Topic Watch Topic
  • New Topic

HttpURLConnection - Luanch Browser  RSS feed

Ron Khera
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

// 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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To lauch a Browser you could try to look at Runtime in java.lang. IE executable is iexplore.
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Norm Radder
Posts: 2240
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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://...'
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!