Click to See Complete Forum and Search --> : How to get an instance of class HttpURLConnection


xeonyaya
August 4th, 1999, 08:49 PM
I think it is an easy question to most guys.
But I am a freshman to Java.
So if anyone knows how to get an instance of HttpURLConnection,
please tell me.
Thanks a lot.

unicman
August 5th, 1999, 02:30 AM
U can get the instance using URL class. Specify the url to this class and call 'openConnection' method. This will return u instance of HttpURLConnection!

- UnicMan
http://members.tripod.com/unicman

xeonyaya
August 5th, 1999, 04:47 AM
Say thanks to UnicMan.I know URL.openConnection(),but it returns a URLConnection instance.
And HttpURLConnection is a sub-class of URLConnect.
If I do like this:
HttpURLConnection httpUC= (HttpURLConnection) url.openConnection();
//url is an instance of URL
I think many methods that HttpURLConnection has but URLConnection has not would not
work properply.
Any guy ,any new ideas?
Say thanks in advance.

Zulfi Khan
August 5th, 1999, 06:02 AM
Yes its simple, but not working on my computer but you can try:
URL hp = new URL("http://www.starwave.com/people/naughton/");
URLConnection hpCon = hp.openConnection ( );

Include the following files:
import java.net.*;
import java.io.*;
import java.util.Date;

unicman
August 5th, 1999, 08:59 AM
I had checked what 'openConnection' returns. And it returned me instance of 'HttpURLConnection' class. I had used JDK1.1.5. And if u try to cast 'URLConection' to 'Http...', it will raise exception. It wouldn't happen that the methods specific to 'Http...' will fail to work.

The code is as follows....


import java.net.*;

public class rnd
{
public static void main( String args[] )
{
try
{
URL url = new URL( "http://unicman.aftek.org/" );

HttpURLConnection hh = (HttpURLConnection)url.openConnection();
System.out.println( "hh" + hh );
}
catch( Exception e )
{ e.printStackTrace(); }
}
}




The output was...
sun.net.www.protocol.http.HttpURLConnection:http://unicman....

Check if u also get the same.

- UnicMan
http://members.tripod.com/unicman

xeonyaya
August 5th, 1999, 09:35 PM
Say thanks to UnicMan again.But if I want to invoke HttpURLConnection::getRequestMethod()?
I use URLConnection::setDoOutput(true),but it doesn't work.
How can I do? What I really to do is to post data to a CGI program or a servlet.

unicman
August 7th, 1999, 01:15 PM
I don't know. Actually HttpURLConnection is sub-class of URLConnection only. So if a method of URLConnection works then it should work for HttpURLConnection also.

I will have to test it. Can u give me ur code or any sample code that I can test. Otherwise I will have to write some sample from scratch.

- UnicMan
http://members.tripod.com/unicman