W3C home > Mailing lists > Public > www-jigsaw@w3.org > May to June 1997

Re: HttpURLConnection [was Re: Handling of redirections (fwd)]

From: Joseph M. Futrelle <futrelle@ncsa.uiuc.edu>
Date: Tue, 27 May 1997 14:57:50 -0500 (CDT)
Message-Id: <199705271957.OAA29473@pecos.ncsa.uiuc.edu>
To: mark@intraspect.com
Cc: www-jigsaw@w3.org

Method 1: make a URLStreamHandlerFactory that returns
w3c.www.protocol.http.Handlers, and make it the global URLStreamHandlerFactory,
like so:

 class myURLStreamHandlerFactory implements URLStreamHandlerFactory {
   public URLStreamHandler createURLStreamHandler (String protocol) {
     if (protocol.equalsIgnoreCase("http")) {
       return new w3c.www.protocol.http.Handler();
     } else return null;
   }
 }

 ...

 URL.setURLStreamHandlerFactory (new myURLStreamHandlerFactory());
 w3c.www.protocol.http.HttpURLConnection c =
  (w3c.www.protocol.http.HttpURLConnection) someURL.openConnection();

unfortunately you can't get the reply status using this method. But you
can get it from an ordinary java.net.HttpURLConnection with getResponseCode().

Method 2: Alternately, you can use the w3c client library like this:

    HttpManager     manager = HttpManager.getManager() ;
    Request request = manager.createRequest() ;
    request.setMethod("GET");
    request.setURL("http://foo.bar/baz");
    Reply    reply = manager.runRequest(request) ;
    System.out.println (reply.getStatus());

Hopefully this all makes sense ...

> I have a problem and a question. First the problem. I get a
> ClassCastException on the second line of the following fragment:
> 
>       URL url = new URL (args[0]);
>       w3c.www.protocol.http.HttpURLConnection c =
>          (w3c.www.protocol.http.HttpURLConnection) url.openConnection();
> 
> when run with the following command line:
> 
>   e:\jdk1.1.1\bin\java  -classpath
> 
> .;c:\jigsaw-alpha5\jigsaw\classes\jigsaw.zip;e:\jdk1.1.1\lib\classes.zip
> 
>       -Djava.handler.protol.pkgs=w3c.www.protocol httpURLTest
>       http://www.microsoft.com
> 
> where httpURLTest is shown at the end of this message.
> 
> My question is: How do you get the status code of an HttpURLConnection
> or a URLconnection?
> 
> Thanks in advance.
> 
> -Mark
> 
> ------------ httpURLTest.java -----------
> 
> import java.util.*;
> import java.net.*;
> import java.io.*; // FIXME - DEBUG
> 
> import w3c.www.protocol.http.*;
> import w3c.www.mime.*;
> import w3c.util.*;
> 
> public class httpURLTest {
> 
>   public static void main(String args[]) {
>     try {
>       URL url = new URL (args[0]);
>       w3c.www.protocol.http.HttpURLConnection c =
> (w3c.www.protocol.http.HttpURLConnection) url.openConnection();
>       // Display some infos:
>       System.out.println("last-modified: "+c.getLastModified());
>       System.out.println("length       : "+c.getContentLength());
> //       System.out.println("status       : "+c.getStatus());
>       System.out.println("location       :
> "+c.getHeaderField("location"));
>       // Display the returned body:
>       InputStream in = c.getInputStream();
>       byte buf[] = new byte[4096];
>       int  cnt   = 0;
>       while ((cnt = in.read(buf)) > 0)
>          System.out.print(new String(buf, 0, 0, cnt));
>       System.out.println("-");
>       in.close();
>  } catch (Exception ex) {
>       ex.printStackTrace();
>  }
>  System.exit(1);
>   }
> }
> 
> 


-- 
Joe Futrelle
Developer, Joule/Jigsaw Java/HTTP
National Center for Supercomputing Applications
futrelle@ncsa.uiuc.edu
(217) 265-0296
Received on Tuesday, 27 May 1997 15:57:58 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Monday, 9 April 2012 12:13:26 GMT