Example usage for java.net HttpURLConnection getHeaderFieldKey

List of usage examples for java.net HttpURLConnection getHeaderFieldKey

Introduction

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

Prototype

public String getHeaderFieldKey(int n) 

Source Link

Document

Returns the key for the n th header field.

Usage

From source file:com.dv.Utils.DvFileUtil.java

/**
 * ???xx.??.//from  w  w w  . j a v  a 2s.  c  om
 * @param connection 
 * @return ??
 */
public static String getRealFileName(HttpURLConnection connection) {
    String name = null;
    try {
        if (connection == null) {
            return name;
        }
        if (connection.getResponseCode() == 200) {
            for (int i = 0;; i++) {
                String mime = connection.getHeaderField(i);
                if (mime == null) {
                    break;
                }
                // "Content-Disposition","attachment; filename=1.txt"
                // Content-Length
                if ("content-disposition".equals(connection.getHeaderFieldKey(i).toLowerCase())) {
                    Matcher m = Pattern.compile(".*filename=(.*)").matcher(mime.toLowerCase());
                    if (m.find()) {
                        return m.group(1).replace("\"", "");
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        DvLogUtil.e(DvFileUtil.class, "???");
    }
    return name;
}

From source file:org.atricore.idbus.capabilities.josso.test.JOSSO11WebSSORouteTest.java

public void testJOSSO11AuthNRequestToSAMLR2() throws Exception {
    String location = null;//from  ww  w .j a  v  a  2 s  .c  o  m
    URL spUrl = new URL("http://localhost:8181/JOSSO11/BIND/CH1?cmd=login");
    HttpURLConnection urlConn = (HttpURLConnection) spUrl.openConnection();
    urlConn.setInstanceFollowRedirects(false);
    urlConn.connect();

    String headerName = null;
    String jossoSession = null;
    for (int i = 1; (headerName = urlConn.getHeaderFieldKey(i)) != null; i++) {
        if (headerName.equals("Location")) {
            location = urlConn.getHeaderField(i);
        }
    }

    assert location != null;

}

From source file:org.atricore.idbus.capabilities.josso.test.JOSSO11WebSSORouteTest.java

public void testSAMLR2AuthnToJOSSO11() throws Exception {
    String location = null;/*  ww  w.j a  v  a 2s.c om*/
    URL spUrl = new URL("http://localhost:8181/JOSSO11/BIND/CH2?SAMLRequest=" + BASE64_SAMLR2_AUTHNREQUEST);
    HttpURLConnection urlConn = (HttpURLConnection) spUrl.openConnection();
    urlConn.setInstanceFollowRedirects(false);
    urlConn.connect();

    String headerName = null;
    String jossoSession = null;
    for (int i = 1; (headerName = urlConn.getHeaderFieldKey(i)) != null; i++) {
        if (headerName.equals("Location")) {
            location = urlConn.getHeaderField(i);
        }
    }

    assert location != null;

}

From source file:cn.org.eshow.framwork.util.AbFileUtil.java

/**
 * ???xx.??./*  w  w  w  .  jav  a 2  s .c o  m*/
 * @param connection 
 * @return ??
 */
public static String getRealFileName(HttpURLConnection connection) {
    String name = null;
    try {
        if (connection == null) {
            return name;
        }
        if (connection.getResponseCode() == 200) {
            for (int i = 0;; i++) {
                String mime = connection.getHeaderField(i);
                if (mime == null) {
                    break;
                }
                // "Content-Disposition","attachment; filename=1.txt"
                // Content-Length
                if ("content-disposition".equals(connection.getHeaderFieldKey(i).toLowerCase())) {
                    Matcher m = Pattern.compile(".*filename=(.*)").matcher(mime.toLowerCase());
                    if (m.find()) {
                        return m.group(1).replace("\"", "");
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        AbLogUtil.e(AbFileUtil.class, "???");
    }
    return name;
}

From source file:com.micro.utils.F.java

/**
 * ???xx.??.//from  w w w  .  ja va  2 s  .  c o  m
 * @param connection 
 * @return ??
 */
public static String getRealFileName(HttpURLConnection connection) {
    String name = null;
    try {
        if (connection == null) {
            return name;
        }
        if (connection.getResponseCode() == 200) {
            for (int i = 0;; i++) {
                String mime = connection.getHeaderField(i);
                if (mime == null) {
                    break;
                }
                // "Content-Disposition","attachment; filename=1.txt"
                // Content-Length
                if ("content-disposition".equals(connection.getHeaderFieldKey(i).toLowerCase())) {
                    Matcher m = Pattern.compile(".*filename=(.*)").matcher(mime.toLowerCase());
                    if (m.find()) {
                        return m.group(1).replace("\"", "");
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        L.E("???");
    }
    return name;
}

From source file:org.apache.jmeter.protocol.http.sampler.HTTPJavaImpl.java

/**
 * From the <code>HttpURLConnection</code>, store all the "set-cookie"
 * key-pair values in the cookieManager of the <code>UrlConfig</code>.
 *
 * @param conn//w w  w .j  a  v a  2s .  co  m
 *            <code>HttpUrlConnection</code> which represents the URL
 *            request
 * @param u
 *            <code>URL</code> of the URL request
 * @param cookieManager
 *            the <code>CookieManager</code> containing all the cookies
 *            for this <code>UrlConfig</code>
 */
private void saveConnectionCookies(HttpURLConnection conn, URL u, CookieManager cookieManager) {
    if (cookieManager != null) {
        for (int i = 1; conn.getHeaderFieldKey(i) != null; i++) {
            if (conn.getHeaderFieldKey(i).equalsIgnoreCase(HTTPConstants.HEADER_SET_COOKIE)) {
                cookieManager.addCookieFromHeader(conn.getHeaderField(i), u);
            }
        }
    }
}

From source file:at.spardat.xma.boot.transport.HTTPTransport.java

/**
 *  Parses the cookies from the given connection and stores them in httpState.
 *  Invalid cookies are ignored and logged.
 *///  w ww . j a va2 s  .  c o m
private void readCookies(IRtXMASessionClient session, URL url, HttpURLConnection conn) {
    String headerName = "";
    for (int i = 1; headerName != null; i++) {
        headerName = conn.getHeaderFieldKey(i);
        if (Statics.HTTP_SET_COOKIE.equals(headerName)) {
            try {
                Cookie[] cookies = cookieSpec.parse(url.getHost(), getPort(url), url.getPath(),
                        "https".equals(url.getProtocol()), conn.getHeaderField(i));
                if (cookies != null) {
                    for (int j = 0; j < cookies.length; j++) {
                        try {
                            cookieSpec.validate(url.getHost(), getPort(url), url.getPath(),
                                    "https".equals(url.getProtocol()), cookies[j]);
                            getHttpState(session).addCookie(cookies[j]);
                            if (session != null && "JSESSIONID".equals(cookies[j].getName())) {
                                session.setId(cookies[j].getName() + "=" + cookies[j].getValue());
                            }
                        } catch (MalformedCookieException e) {
                            log_.log(LogLevel.WARNING, "cookie rejected: \""
                                    + cookieSpec.formatCookie(cookies[j]) + "\". " + e.getMessage());
                        }
                    }
                }
            } catch (MalformedCookieException e) {
                log_.log(LogLevel.WARNING,
                        "Invalid cookie header: \"" + conn.getHeaderField(i) + "\". " + e.getMessage());
            }
        }
    }
}

From source file:com.sogrey.sinaweibo.utils.FileUtil.java

/**
 * ???xx.??./*from  w  ww.  j a  v  a  2 s  . c  om*/
 * 
 * @param connection
 *            
 * @return ??
 */
public static String getRealFileName(HttpURLConnection connection) {
    String name = null;
    try {
        if (connection == null) {
            return name;
        }
        if (connection.getResponseCode() == 200) {
            for (int i = 0;; i++) {
                String mime = connection.getHeaderField(i);
                if (mime == null) {
                    break;
                }
                // "Content-Disposition","attachment; filename=1.txt"
                // Content-Length
                if ("content-disposition".equals(connection.getHeaderFieldKey(i).toLowerCase())) {
                    Matcher m = Pattern.compile(".*filename=(.*)").matcher(mime.toLowerCase());
                    if (m.find()) {
                        return m.group(1).replace("\"", "");
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        LogUtil.e(FileUtil.class, "???");
    }
    return name;
}

From source file:org.zend.sdklib.internal.target.ZendDevCloud.java

private String authenticate(String username, String password) throws IOException {
    URL url = new URL(baseUrl + USER_LOGIN);
    final String content = MessageFormat.format("username={0}&password={1}&Submit={2}",
            URLEncoder.encode(username, "UTF-8"), URLEncoder.encode(password, "UTF-8"));

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

    urlConn.setDoOutput(true);// w  ww .j av a2 s . c o  m
    urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    DataOutputStream printout = new DataOutputStream(urlConn.getOutputStream());
    printout.writeBytes(content);
    printout.flush();
    printout.close();

    final int responseCode = urlConn.getResponseCode();
    if (responseCode != 302) {
        throw new IOException("Authentication error to: " + baseUrl);
    }

    String headerName;
    String cookie = null;
    for (int i = 1; (headerName = urlConn.getHeaderFieldKey(i)) != null; i++) {
        if ("Set-Cookie".equals(headerName)) {
            cookie = urlConn.getHeaderField(i);
        }
    }

    urlConn.disconnect();
    cookie = cookie.substring(0, cookie.indexOf(";"));
    return cookie;
}

From source file:org.apache.jmeter.protocol.http.sampler.HTTPJavaImpl.java

/**
 * Gets the ResponseHeaders from the URLConnection
 *
 * @param conn/*  ww  w  . jav  a 2  s .  c  om*/
 *            connection from which the headers are read
 * @return string containing the headers, one per line
 */
protected String getResponseHeaders(HttpURLConnection conn) {
    StringBuilder headerBuf = new StringBuilder();
    headerBuf.append(conn.getHeaderField(0));// Leave header as is
    // headerBuf.append(conn.getHeaderField(0).substring(0, 8));
    // headerBuf.append(" ");
    // headerBuf.append(conn.getResponseCode());
    // headerBuf.append(" ");
    // headerBuf.append(conn.getResponseMessage());
    headerBuf.append("\n"); //$NON-NLS-1$

    String hfk;
    for (int i = 1; (hfk = conn.getHeaderFieldKey(i)) != null; i++) {
        headerBuf.append(hfk);
        headerBuf.append(": "); // $NON-NLS-1$
        headerBuf.append(conn.getHeaderField(i));
        headerBuf.append("\n"); // $NON-NLS-1$
    }
    return headerBuf.toString();
}