Java HTTP Response Code getResponseCode(URLConnection connection)

Here you can find the source of getResponseCode(URLConnection connection)

Description

Returns the response code for the given URL connection (assuming this connection represents a HTTP(S) URL).

License

Apache License

Parameter

Parameter Description
connection the URL connection to get the response code for, can be <code>null</code>.

Return

the response code for the given connection, or -1 if it could not be determined.

Declaration

private static int getResponseCode(URLConnection connection) 

Method Source Code


//package com.java2s;
/*// w w w  .ja  v a  2  s.  c o m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;

import java.net.HttpURLConnection;
import java.net.URLConnection;

public class Main {
    /**
     * Returns the response code for the given URL connection (assuming this connection represents a HTTP(S) URL).
     * 
     * @param connection
     *            the URL connection to get the response code for, can be <code>null</code>.
     * @return the response code for the given connection, or <code>-1</code> if it could not be determined.
     */
    private static int getResponseCode(URLConnection connection) {
        try {
            if (connection instanceof HttpURLConnection) {
                return ((HttpURLConnection) connection).getResponseCode();
            }
            return -1;
        } catch (IOException exception) {
            return handleIOException(connection);
        }
    }

    /**
     * @see http://docs.oracle.com/javase/6/docs/technotes/guides/net/http-keepalive.html
     */
    public static int handleIOException(URLConnection conn) {
        int respCode = -1;
        if (!(conn instanceof HttpURLConnection)) {
            return respCode;
        }

        try {
            respCode = ((HttpURLConnection) conn).getResponseCode();
            flushStream(((HttpURLConnection) conn).getErrorStream());
        } catch (IOException ex) {
            // deal with the exception
        }
        return respCode;
    }

    static void flushStream(InputStream is) {
        byte[] buf = new byte[4096];
        try {
            while (is.read(buf) > 0) {
                // Ignore...
            }
        } catch (IOException ex) {
            // deal with the exception
        } finally {
            closeSilently(is);
        }
    }

    /**
     * @param closeable
     * @return always <code>null</code>, for easy chaining.
     */
    public static Closeable closeSilently(Closeable closeable) {
        try {
            if (closeable != null) {
                closeable.close();
            }
        } catch (IOException exception) {
            // Ignore...
        }
        return null;
    }
}

Related

  1. getResponseCode(String urlS)
  2. getResponseCode(String urlString)
  3. getResponseCode(String urlString)
  4. getResponseCode(String urlString)
  5. getResponseCode(URL url)