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.
Printable View
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.
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
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.
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;
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.http://www.protocol.http.HttpURLConn...://unicman....
Check if u also get the same.
- UnicMan
http://members.tripod.com/unicman
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.
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