HttpURLConnection and URLConnection

hi,
what's the difference between HttpURLConnection and URLConnection ?
When should I use the first and when the second one ?
thanks

The Javadoc shows the relationship. Cast a URLConnection to an HttpURLConnection when the URL protocol is HTTP.

Similar Messages

  • Problem with HttpURLConnection and HttpSession.

    Hi,
    Problem with HttpURLConnection and HttpSession.
    I created a HttpSession object in my main class and then called a URL via HttpURLConnection.
    I tried to access the same session object from the servlet called by the URL . but the main session
    is not returned a new seperate session is created.let me know how can we continue the same session in
    the called URL also which is created in main class.
    Thanks
    Prasad

    You are not supported to create a HttpSession by java client. Only J2EE web container can create it.

  • HttpURLConnection and bandwidth

    I have written a program which downloads files from net using the HttpURLConnection class.
    I want to specify the percentage of bandwidth that should be used for downloading the files, so that it does not slow down by other net surfing work.
    Can anybody suggest me a way to do this.
    Thank u

    That's not something you can control via Java.
    The only way you could control that is thru some network hardware, or, if the server is yours, you could set a header in the request and the server could take that as a clue to throttle the data it's going to send out. But otherwise, URLConnection class itself will not be able to do anything.

  • HttpUrlConnection  and codification

    Hello, I am trying to open a connection to a webpage that is writen in greek (using utf8), but when I try to see the page in my JSP, I don't see well the greek characters. Is my code wrong? maybe is the server configuration?
    I have no idea. I try some encode, but nothing changes.
    Iam using java 5.0 and tomcat 5.5.
    My code:
    String url = "http://www.google.gr";
    URL u = new URL (url);
    HttpURLConnection urlc = (HttpURLConnection)u.openConnection ();
    response.setContentType("text/html;charset=UTF-8");
    urlc.connect ();
    URLConnection.getInputStream()
    String line;
    BufferedReader br = new BufferedReader (new InputStreamReader (urlc.getInputStream ()));
    while ( (line = br.readLine ()) != null ) {
    out.write(line);
    br.close ();
    urlc.disconnect ();
    Thanks a lot.
    isaac.

    Well, you aren't specifying the encoding on your InputStreamReader, so it would be using some other encoding instead. There may be other problems but fix that one first.

  • HttpURLConnection and redirect

    Hello,
    I need help with this. I have on server xml page with some data, but before I get this page, I must login to server. With my code, I reach only redirect page. How I can login and get response after redirect?
    Here is the function for communication:
    package qpconnector;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    public class ServerConnect {
        private String defaultServer =
                "http://10.0.5.55/login/login.action?username=InstallAdmin&password=&" +
                "page=/touchscreens/mainDisplay.qsphtml";
        public void connectToServer () {
            try {
                URL url = new URL(defaultServer);
                URLConnection connection = url.openConnection();
                HttpURLConnection httpConn = (HttpURLConnection) connection;
                httpConn.setDoInput(true);
                httpConn.setDoOutput(true);
                InputStream is = httpConn.getInputStream();
                StringBuffer sb = new StringBuffer();
                int read;
                while ((read = is.read()) != -1) {
                    sb.append((char) read);
                System.out.println (sb.toString());
            } catch (MalformedURLException ex1) {
                ex1.printStackTrace();
            } catch (Exception ex) {
                ex.printStackTrace();
        public static void main (String[] args) {
            ServerConnect sc = new ServerConnect();
            sc.connectToServer();
    }

    ProjectMoon wrote:
    malcolmmc wrote:
    By default java.net.URL connections manage cookies automatically.It appears that it will provide you with Cookie objects from the server when you make the connection. However, when you make a new connection, you will have to send your cookies with it. This is because of the way HTTP works. If that is incorrect, I apologize.It will return cookies received from a particular domain to that domain, which is generally all you need. Cookies always originate at the server end.
    >
    Edit: [Ah ha.|http://blogs.sun.com/CoreJavaTechTips/entry/cookie_handling_in_java_se] There is new stuff in Java 6 for cookies.
    There is, it makes the cookie business more manageable, but the basic cookie mechanism is already there, at least in 1.5, it's just a lot more tedious if you want to do something complicated, like maintain more than one cookie jar in the JVM.

  • HttpURLConnection and valid cookies

    Hi,
    I have implemented a web server with a login page to authenticate a user.
    When the browser has not send a valid cookie, the server redirects to the
    login page, where the user can fill a form to post the username and password.
    I am implementing a java stand-alone client to access this web server. I use
    the HttpURLConnection class for this.
    First, i authenticate the user sending a post request with the login and
    password. The server responds with a cookie. To access other resources in
    the server i send the cookie in each request.
    My problem is: i am using java class HttpURLConnection. When the cookie
    is not valid, the server redirects to the login page, but i have not
    found a way to know about it without parsing the response html. I get the http
    header fields but there is no info about this.
    Thanks in advance

    You should extraxt the sessionID from the cookie, when logging on to the system.
    From then on, pass the session ID to all further connections.
    You can obtain the session-ID like this:
    public void retrieveSessionID() {
                   String key = "";
                   String id = "";
                   try {
                     URL url = new URL ( "http://localhost/yourapplication/logon.jsp?username=Homer&password=donut);
                     URLConnection urlConnection = url.openConnection();
                     if (urlConnection != null)
                          for (int i = 1;(key = urlConnection.getHeaderFieldKey(i)) != null; i++)
                               if (key.equalsIgnoreCase("set-cookie"))
                                    id = urlConnection.getHeaderField(key);
                                    id = id.substring(0, id.indexOf(";"));
                               }     //if
                          }     //for
                     }     //if
                   } catch (IOException e) {
                                                                          e.printStackTrace();
                   }     //catch
                     this.jSessionId = id;
         }     //getSessionIDAnd then make every connection like this:
    URL url = new URL("http:......");
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
                    connection.setRequestProperty("Cookie", this.jSessionId);
                    connection.connect();                Hope I could help!
    Greetings
    Stef

  • HttpURLConnection and headers.

    I want to send an XML document to a HttpServlet and get a new one back. I plan to send it using the POST method however, in order to enclose the document in the request I need to set the Entity-Headers.. I can't really se how I would do this with HttpURLConnection. Can I modify the headers that HttpURLConnection produce at all?
    Of course, I could just use a regular URLConnection and pass all headers manually but it would be nice to avoid that.

    Well, thats not the problem...
    I need to send the whole document in the Request in order to get a response object with the other document. But if I do something like this:
    HttpURLConnection conn = (HttpURLConnection)url.openConnection("foo");
    conn.setContentType("text/plain");
    conn.setDoOutput(true);
    PrintWriter out = new PrintWriter( dataConn.getOutputStream() );
    out.write("blah");
    out.close();
    then I can't call setDoInput() and open an inputstream and read the response. I get and exception saying the connection is already open. This leads me to the conclusion that when I call the SetDoInput it opens a new Connection which is not possible since it is already open.
    When Is the request actually sent and how do I modify indivdual headers in the request?

  • Diffrence between socket connection and URLConnection

    hy friends,
    though, i know and often work on it but it might be more conceptual for me to know your perception.
    by the way, what are the main points which diffrentiate the connection between client and server using
    1) sokets which in tern using ClientSocket,ServerSocket... classes
    2) URL directly which in tern using HttpURLConnection,URLConnection...classes
    any help would greatly appreciated

    1) sokets which in tern using ClientSocket,ServerSocket... classesYou mean 'sockets', and 'Socket', not 'ClientSocket'. Sockets speak TCP.
    2) URL directly which in tern using HttpURLConnection,URLConnection...classesHttpURLConnection speaks HTTP which is an application protocol layered over TCP.

  • Firewall and URLConnection

    The product that am working currently has two web servers each hosting a web applicaiton. The first web server hosts the core applicaiton of the product lets call it as WEB1. And the second one is a light weight webserver developed here itself, its nothing more than a servlet/JSP container lets call this as WEB2.
    The relation between these web servers is one to many i.e. For every WEB1 you have many WEB2 servers.
    WEB1 calls few jsp pages from WEB2 in its jsp pages. Most of the times its frames. The upper part of the frame might serve WEB1 jsp and the lower part may serve from WEB2.
    Till now we were using URLConnection object to ping the WEB2 server and if its down then convey the user that WEB2 is down. This was done
    Now there are possibilities that WEB2 might reside on a machine that is not rechable by WEB2, probably behind firewall. In this case URLConnection is failing.
    Is there anyway, we can bypass the firwall for pinging the WEB2 server. Rather to know the status of WEB2 from WEB1 server.
    Note that the browser from which the user accesses the WEB1 server will reside on the machine where WEB2 is running.
    thanks,
    Vikram Patil.

    The crux of your topic seems to be How to open URLConnection from a host A to another host B, given that there is a firewall between them ?
    URLConnection uses socket(s) underneath itself.
    If the requisite port (usually 80 for HTTP traffic) is opened in firewall to let the trafiic in the requisite direction go through, you should not any problem.
    A commonly used configuration is to have a proxy server of sorts along with the firewall. The application opening the URLConnection would direct all HTTP requests to the proxy server. The proxy server would in turn send the HTTP requests to the target server, but only after consuming some infomation from the request which tells it about the client accessing it. And similarly for HTTP responses.
    There is in-built support in HttpURLConnection implementations to use proxies. The way to have your application actually send HTTP requests through a proxy is pretty simple. It basically involes setting a couple of system properties. http.proxyHost and http.proxyPort. The HttpURLConnection implementation will do the actual dirty work for you.
    For a wealth of information about this, do a search on these forums.

  • Sending email using URL and URLConnection

    I have been having trouble trying to send an email using URL.
    I have specified the mail host using: mailHost = Options.getProperty("mailHost", "mail.myisp.com");
    and tried to send the email using this:
    try
    try
    URL u = new URL("mailto:"+recipient);
    URLConnection c = u.openConnection();
    c.setDoInput(true);
    c.setDoOutput(true);
    c.connect();
    PrintWriter out = new PrintWriter(new OutputStreamWriter(c.getOutputStream()));
    out.print("From:\"ME\" \n");
    out.print("Subject: Stuff \n");
    out.print("blah blah blah");
    out.close();
    catch etc. . .
    I recieve the email with the subject line there, but the actual body of the email is empty. Anyone know what I have done wrong? This is part of a standalone application that was turned into an .exe with jbuilder9.

    That worked, thanks!
    Out of curious, why the surprise that it worked?
    Its not that bad is it?Yeah, I didn't know that mailto: support was there either, is all. The blank line thing is just common in HTTP and sendmail so, I figured that would be the solution.
    I don't know that it's bad... if it works. Generally, the JavaMail APIs are a more robust way of sending mail, but if mailto: URLs work, and it's simple mail, then I see no reason not to use it.

  • HttpURLConnection and Proxies

    Hello I try to connect form java application behind proxy :
    I got time out exception.
                    System.out.println("-START-");
                   System.getProperties().put( "proxySet", "true" );
                   System.getProperties().setProperty("http.proxyHost","genproxy.xxx.com");
                   System.getProperties().setProperty("http.proxyProt","8080");
                   Authenticator.setDefault(new Authenticator()
                      public PasswordAuthentication getPasswordAuthentication()
                          return new PasswordAuthentication("myNeUser", "myNePassword".toCharArray());
                   URL url = new URL("http://11.11.1.111/smg/servlet/XmlServlet");
                   URLConnection connection = url.openConnection();
                 BufferedReader reader = new BufferedReader(
                   new InputStreamReader(connection.getInputStream()));

    Ori-Gil wrote:
                   System.getProperties().put( "proxySet", "true" );There's no such system property. Get rid of this line.
                   System.getProperties().setProperty("http.proxyHost","genproxy.xxx.com");This looks okay, assuming you spelled the name of your proxy server correctly (and assuming you can actually connect to it from where you run your code).
                   System.getProperties().setProperty("http.proxyProt","8080");You'll want to spell the word "Port" correctly here.

  • HttpURLConnection and HtmlUnit

    Hello,
    I am using these commands to get a webpage from my in-house webserver:
    String urlStr = "http://localhost/welcome.jsp";
    URL url = new URL(urlStr);
    HttpURLConnection ucon = (HttpURLConnection)url.openConnection();
    ucon.connect();
    InputStreamReader isr = new InputStreamReader(ucon.getInputStream());
    BufferedReader in = new BufferedReader(isr);
    I then read the file line by line, searching the text for this input tag:
    <input type=hidden name=pagecode value=123>
    Is there any easier way to get the value of this hidden variable?
    Does the HttpURLConnection have some way of extracting hidden values directly?
    Thank you for helping.

    I put the htmlunit-2.11.jar file in the web-inf/lib folder.
    I added this line to my standard java file:
    import com.gargoylesoftware.htmlunit.WebClient;
    I tried to rebuild the java file and got this error:
    cannot access com.gargoylesoftware.htmlunit.WebClient;
    bad class file web-inf/lib/htmlunit.jar
    class file has wrong version 50.0, should 49.0
    please remove or make sure it appears in the correct subdirectory of the classpath
    Any suggestions are greatly appreciated.

  • HttpURLConnection and HEAD/GET methods

    I am attempting to validate whether an HTML page exists or not.
    I have found that, for about 7% of the pages checked, HEAD and GET methods return different response codes.
    I have structured my code such:
    1) make initial check using HEAD method
    2) for non valid (200) response codes, recheck the page using the GET method.
    In this case about 75% of the pages that failed using the HEAD method will work when using the GET method.
    So, I guess my questions are:
    1) Does anybody know why HEAD/GET return different response codes?
    2) Does anybody know a better way to check if a page exists?
    Here is the sample program I am using with a few URLs that exhibit this behaviour:
    import java.io.IOException;
    import java.net.HttpURLConnection;
    import java.net.InetAddress;
    import java.net.URL;
    import java.net.UnknownHostException;
    public class Internet
         private final static String DEFAULT_LOCAL_HOST = "127.0.0.1";
         private URL url;
         private HttpURLConnection http;
         private int responseCode;
         private String responseMessage;
         public Internet(URL url)
              this.url = url;
         public boolean isValid()
              try
                   //  Make first attempt using a HEAD request
                   http = (HttpURLConnection)url.openConnection();
                   http.setRequestMethod( "HEAD" );
                   http.connect();
                   System.out.println( "head: " + http.getResponseCode()
                   + " : " + http.getResponseMessage() );
                   //  GET seems to do a better job, try again
                   if ( http.getResponseCode() != HttpURLConnection.HTTP_OK)
                        http = (HttpURLConnection)url.openConnection();
                        http.setRequestMethod( "GET" );
                        http.connect();
                        System.out.println( "get:  " + http.getResponseCode() );
                   responseCode = http.getResponseCode();
                   responseMessage = http.getResponseMessage();
                   if (http.getResponseCode() == HttpURLConnection.HTTP_OK)
                        return true;
                   else
                        System.out.println( http.getResponseMessage() );
                        return false;
              catch (IOException e)
                   responseCode = -1;
                   responseMessage = e.getMessage();
                   System.out.println( e );
                   return false;
         public static void main(String[] args)
              throws Exception
              URL url = new URL( "http://www.trca.on.ca" );
              Internet internet = new Internet( url );
              System.out.println( internet.isValid() );
              url = new URL( "http://school.discovery.com/sciencefaircentral" );
              internet = new Internet( url );
              System.out.println( internet.isValid() );
              url = new URL( "http://www.amazon.com" );
              internet = new Internet( url );
              System.out.println( internet.isValid() );
    }

    Using my sample program:
    1) about 3K of data is transferred
    2) it runs in about 8 seconds
    Using InputStream in = http.getInputStream():
    1) about 73K of data is transferred
    2) it runs in about 15 seconds
    Using the getInputStream() method causes the entire file to be transmitted (even though I don't use any read() methods). I don't care about the data.
    I want the check to be as fast as possible which is why I use the HEAD method. It doesn't transfer the contents of the file. But why in some cases does it show a response code other than 200?

  • HttpURLConnection and the killer Pound (#)

    I'm trying to create a part of my application that posts data to an ASP page (on an affiliate's web site).
    Originally, I was using the following code to post the data:
    URL url = new URL(sPostURL);                    
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    // include fields to post
    conn.setRequestProperty("address", sAddress);
    conn.setRequestProperty("ste/apt#", sApt);
    DataOutputStream dos = new DataOutputStream(conn.getOutputStream() );
    dos.writeBytes("");
    dos.close();
    // Retrieve response
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String sNewLine = in.readLine();
    String sResp = "";
    while (sNewLine != null) {
      sResp += sNewLine;
      sNewLine = in.readLine();
    in.close();Which worked at first, but then when I went back to the code, I must have made a change because it no longer seems to post the data (sAddress and sApt) to the page. So after researching the topic in the forums, I switched to using a tokenized string to store my post data:
    URL url = new URL(sPostURL);                    
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    DataOutputStream dos = new DataOutputStream(conn.getOutputStream() );
    // include fields to post
    String sRequest = "&address=" + sAddress; // BUILD DATA STRING (NEW)
    sRequest += "&ste/apt#=" + sApt; // BUILD DATA STRING (NEW)
    dos.writeBytes("");
    dos.close();
    // Retrieve response
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String sNewLine = in.readLine();
    String sResp = "";
    while (sNewLine != null) {
      sResp += sNewLine;
      sNewLine = in.readLine();
    in.close();which posts the data fine, but the pound sign (#) causes the server to return a response code 500. I can't find an easy way to escape the pound sign. I've even tried building the string using the URL encoded value (%23) but it still causes problems. Does anyone have any suggestions? Was I correct in abandoning the first method? if anyone has any guidance I would be very grateful.

    I think the first method is more clean, but if you want to use the second one, you should put your request string through a URLEncoder to encode the string first.
    --lichu                                                                                                                                                                                                                                                                                                                                                       

  • HttpURLConnection and DataInputStream

    I posted this in the Conventional & Interruptable IO forum, but thought it might be better off here....
    I'm having a strange issue with this piece of code. The input stream coming in is quite predictable and I know that this loop will only iterate twice. Once for less than 1024 bytes, and the next returns -1. The problem I'm having has to do with the second iteration. The in.read(bary); line takes around 75 seconds just to get that -1 so we can break out of the loop. I don't understand how the first read is so fast, and the second one is so slow. Any thoughts?
    1 URL url = new URL("http://xyz");
    2 HttpURLConnection con = (HttpURLConnection)url.openConnection();
    3 DataInputStream in = new DataInputStream( con.getInputStream() );
    4 StringBuffer httpResponse = new StringBuffer();
    5 byte[] bary = new byte[1024];
    6 while (true) {
    7 int bytesRead = in.read(bary);
    8 if (bytesRead <= 0) {
    9 break;
    10 }
    11 httpResponse.append(new String(bary, 0, bytesRead));
    10 }//end while
    I also want to mention that this bit of code runs in J2EE apps that are deployed to 5 different environments, one of those being production which is heavly hit. In only one of these am I having the issue...not production :) . They're running on Solaris 8, sun's sdk 1.4.2_3, weblogic 8.1. Also, I've run the exact same code making this call and I know that the app on the other end is responding fast...we've also verified this using the webserver's access log.
    Thanks for reading,
    -Jon

    The -1 check is in the while loop, it's just not indented very well. The loop starts on line 6, and ends on 10. I mentioned the data is predicatable. On the first call to read I know that I'm reading all the data that the stream has to offer, the second time i read returns -1. I also know this because The actual code has debug statements that tell me how many times it loops, and how long it takes to perform the read in millies. 100% of the time it loops twice, and 100% of the time the read that results in -1 takes 75 seconds.
    Maybe i misunderstood what you were saying? Any thoughts would be greatly appreciated.
    Message was edited by:
    jTosca
    Message was edited by:
    jTosca

Maybe you are looking for