CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 1999
    Posts
    13

    How to get an instance of class HttpURLConnection


    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.


  2. #2
    Join Date
    May 1999
    Location
    Pune, MH, India.
    Posts
    453

    Re: How to get an instance of class HttpURLConnection

    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

  3. #3
    Join Date
    Jul 1999
    Posts
    13

    Re: How to get an instance of class HttpURLConnection

    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.


  4. #4
    Join Date
    Apr 1999
    Location
    Pakistan
    Posts
    207

    Re: How to get an instance of class HttpURLConnection

    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;



  5. #5
    Join Date
    May 1999
    Location
    Pune, MH, India.
    Posts
    453

    Re: How to get an instance of class HttpURLConnection

    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

  6. #6
    Join Date
    Jul 1999
    Posts
    13

    Re: How to get an instance of class HttpURLConnection

    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.


  7. #7
    Join Date
    May 1999
    Location
    Pune, MH, India.
    Posts
    453

    Re: How to get an instance of class HttpURLConnection

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured