Example usage for java.net HttpURLConnection getHeaderField

List of usage examples for java.net HttpURLConnection getHeaderField

Introduction

In this page you can find the example usage for java.net HttpURLConnection getHeaderField.

Prototype

public String getHeaderField(int n) 

Source Link

Document

Returns the value for the n th header field.

Usage

From source file:org.ut.biolab.medsavant.client.view.genetics.variantinfo.DownloadTask.java

public DownloadTask(String URLStr, String destPath, String notificationTitle) throws IOException {
    //do not want download tasks to be interrupted by changing subsection views, so 
    //use the class name for the task.        
    super(DownloadTask.class.getSimpleName(), notificationTitle);

    this.URLStr = URLStr;
    this.destPath = destPath;
    showResultsOnFinish(false);/*from  www.  j a  va 2s . c o m*/

    int filesize;
    String filename;

    URL url = new URL(URLStr);
    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
    int responseCode = httpConn.getResponseCode();

    if (responseCode != HttpURLConnection.HTTP_OK) {
        throw new IOException("File " + URLStr + "unavailable from server");
    }

    String disposition = httpConn.getHeaderField("Content-Disposition");
    filesize = httpConn.getContentLength();

    if (disposition != null) {
        // extracts file name from header field
        int index = disposition.indexOf("filename=");
        if (index > 0) {
            filename = disposition.substring(index + 10, disposition.length() - 1);
        } else {
            filename = extractFileFromURL();
        }
    } else {
        // extracts file name from URL
        filename = extractFileFromURL();
    }
    this.filename = filename;
    this.filesize = filesize;
    httpConn.disconnect();
}

From source file:org.apache.olingo.fit.tecsvc.http.ODataVersionConformanceITCase.java

@Test
public void validODataVersionAndMaxVersionHeader() throws Exception {
    URL url = new URL(SERVICE_URI + "ESAllPrim");

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod(HttpMethod.GET.name());
    connection.setRequestProperty(HttpHeader.ODATA_VERSION, "4.0");
    connection.setRequestProperty(HttpHeader.ODATA_MAX_VERSION, "5.0");
    connection.connect();//from   www . java2 s. c om

    assertEquals("4.0", connection.getHeaderField(HttpHeader.ODATA_VERSION));

    final String content = IOUtils.toString(connection.getErrorStream());
    assertNotNull(content);
    ;
}

From source file:it.polimi.chansonnier.agent.YoutubeGrabber.java

private String getRedirUrl(String url) {
    String hdr = "";
    try {/*from  w w  w .  j  a va  2  s.  co m*/
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setInstanceFollowRedirects(false);
        conn.addRequestProperty("User-Agent",
                "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.8) Gecko/20100215 Ubuntu/9.04 (jaunty) Shiretoko/3.5.8");
        hdr = conn.getHeaderField("location");
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return hdr;
}

From source file:com.docdoku.cli.helpers.FileHelper.java

private void manageHTTPCode(HttpURLConnection conn) throws IOException, LoginException {
    int code = conn.getResponseCode();
    switch (code) {
    case 401:/*w  w w  .j  a v a 2s .  c  o m*/
    case 403:
        throw new LoginException("Error trying to login");
    case 500:
        throw new IOException(conn.getHeaderField("Reason-Phrase"));
    }
}

From source file:com.tc.util.io.ServerURL.java

public String getServerVersion(PwProvider pwProvider) throws IOException {
    for (int i = 0; i < 3; i++) {
        HttpURLConnection urlConnection = createSecureConnection(pwProvider);
        try {//from w w w. jav  a 2s  . co  m
            urlConnection.connect();
            int responseCode = urlConnection.getResponseCode();
            if (responseCode == 200) {
                return urlConnection.getHeaderField(VERSION_HEADER);
            } else {
                logger.info("Failed to retrieve header field, response code : " + responseCode + ", headers :\n"
                        + readHeaderFields(urlConnection) + " body : \n[" + readLines(urlConnection) + "]");
            }
        } finally {
            urlConnection.disconnect();
        }

        logger.info("Retrying connection since response code != 200");
        ThreadUtil.reallySleep(50);
    }

    throw new IOException(
            "Cannot retrieve " + VERSION_HEADER + " header from server url after 3 tries : " + theURL);
}

From source file:org.ow2.proactive_grid_cloud_portal.cli.cmd.sched.PackageDownloader.java

/**
 * This method returns the real URL if the given URL forwards to another one. Otherwise it returns the given URL.
 *
 * @param url//from   ww w . j a v  a2 s  .co m
 * @return
 * @throws IOException
 */
private URL getRealURLIfForwarded(URL url) throws IOException {
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setInstanceFollowRedirects(false);
    con.connect();
    return con.getHeaderField("Location") != null ? new URL(con.getHeaderField("Location")) : url;
}

From source file:org.apache.olingo.fit.tecsvc.http.ODataVersionConformanceITCase.java

@Test
public void invalidODataVersionHeader1() throws Exception {
    URL url = new URL(SERVICE_URI + "ESAllPrim");

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod(HttpMethod.GET.name());
    connection.setRequestProperty(HttpHeader.ODATA_VERSION, "3.0");
    connection.connect();/* ww  w  .j  a  v a2  s.c o  m*/

    assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), connection.getResponseCode());
    assertEquals("4.0", connection.getHeaderField(HttpHeader.ODATA_VERSION));

    final String content = IOUtils.toString(connection.getErrorStream());
    assertTrue(content.contains("OData version '3.0' is not supported."));
}

From source file:org.apache.olingo.fit.tecsvc.http.ODataVersionConformanceITCase.java

@Test
public void invalidODataVersionHeader2() throws Exception {
    URL url = new URL(SERVICE_URI + "ESAllPrim");

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod(HttpMethod.GET.name());
    connection.setRequestProperty(HttpHeader.ODATA_VERSION, "5.0");
    connection.connect();/*from   w  w  w  .  j a v a2s  . com*/

    assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), connection.getResponseCode());
    assertEquals("4.0", connection.getHeaderField(HttpHeader.ODATA_VERSION));

    final String content = IOUtils.toString(connection.getErrorStream());
    assertTrue(content.contains("OData version '5.0' is not supported."));
}

From source file:org.jumpmind.symmetric.transport.http.HttpOutgoingTransport.java

public ChannelMap getSuspendIgnoreChannelLists(IConfigurationService configurationService, Node targetNode) {

    HttpURLConnection connection = requestReservation();

    // Connection contains remote suspend/ignore channels list if
    // reservation was successful.

    ChannelMap suspendIgnoreChannelsList = new ChannelMap();

    String suspends = connection.getHeaderField(WebConstants.SUSPENDED_CHANNELS);
    String ignores = connection.getHeaderField(WebConstants.IGNORED_CHANNELS);

    suspendIgnoreChannelsList.addSuspendChannels(suspends);
    suspendIgnoreChannelsList.addIgnoreChannels(ignores);

    ChannelMap localSuspendIgnoreChannelsList = configurationService
            .getSuspendIgnoreChannelLists(targetNode.getNodeId());
    suspendIgnoreChannelsList.addSuspendChannels(localSuspendIgnoreChannelsList.getSuspendChannels());
    suspendIgnoreChannelsList.addIgnoreChannels(localSuspendIgnoreChannelsList.getIgnoreChannels());

    return suspendIgnoreChannelsList;
}

From source file:org.apache.olingo.fit.tecsvc.http.BasicHttpITCase.java

@Test
public void testAcceptSimple() throws Exception {
    URL url = new URL(SERVICE_URI);

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    connection.setRequestMethod(HttpMethod.GET.name());
    connection.setRequestProperty(HttpHeader.ACCEPT, "application/json");

    connection.connect();/*from  w  ww  .ja v  a 2s .  co m*/

    assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode());
    assertEquals(ContentType.JSON, ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE)));
}