java.net.HttpURLConnection#getHeaderFields ( )源码实例Demo

下面列出了java.net.HttpURLConnection#getHeaderFields ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: pinpoint   文件: HttpURLConnectionIT.java
@Test
public void test() throws Exception {
    URL url = new URL(webServer.getCallHttpUrl());
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    connection.getHeaderFields();
    
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    
    Class<?> targetClass = Class.forName("sun.net.www.protocol.http.HttpURLConnection");
    Method getInputStream = targetClass.getMethod("getInputStream");
    
    String destinationId = webServer.getHostAndPort();
    String httpUrl = webServer.getCallHttpUrl();
    verifier.verifyTraceCount(1);
    verifier.verifyTrace(event("JDK_HTTPURLCONNECTOR", getInputStream, null, null, destinationId, annotation("http.url", httpUrl)));
}
 
源代码2 项目: restcommander   文件: WSUrlFetch.java
/**
 * you shouldnt have to create an HttpResponse yourself
 * @param method
 */
public HttpUrlfetchResponse(HttpURLConnection connection) {
    try {
        this.status = connection.getResponseCode();
        this.headersMap = connection.getHeaderFields();
        InputStream is = null;
        if (this.status >= HttpURLConnection.HTTP_BAD_REQUEST) {
            // 4xx/5xx may return a response via getErrorStream()
            is = connection.getErrorStream();
        } else {
            is = connection.getInputStream();
        }
        if (is != null) this.body = IO.readContentAsString(is, getEncoding());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        connection.disconnect();
    }
}
 
源代码3 项目: neembuu-uploader   文件: WuploadUploaderPlugin.java
public static void getFolderCookies() throws IOException {
    u = new URL(wuplodDomain);
    uc = (HttpURLConnection) u.openConnection();
    uc.setRequestProperty("Cookie", langcookie + ";" + sessioncookie + ";" + mailcookie + ";" + namecookie + ";" + rolecookie);
    Map<String, List<String>> headerFields = uc.getHeaderFields();
    if (headerFields.containsKey("Set-Cookie")) {
        List<String> header = headerFields.get("Set-Cookie");
        for (int i = 0; i < header.size(); i++) {
            String tmp = header.get(i);
            if (tmp.contains("fs_orderFoldersBy")) {
                orderbycookie = tmp;
                orderbycookie = orderbycookie.substring(0, orderbycookie.indexOf(";"));
            }
            if (tmp.contains("fs_orderFoldersDirection")) {
                directioncookie = tmp;
                directioncookie = directioncookie.substring(0, directioncookie.indexOf(";"));
            }
        }
        System.out.println("ordercookie : " + orderbycookie);
        System.out.println("directioncookie : " + directioncookie);
    }

}
 
源代码4 项目: wechatGroupRobot   文件: CookieUtil.java
public static String getCookie(HttpRequest request) {
    HttpURLConnection conn = request.getConnection();
    Map<String, List<String>> resHeaders = conn.getHeaderFields();
    StringBuffer sBuffer = new StringBuffer();
    for (Map.Entry<String, List<String>> entry : resHeaders.entrySet()) {
        String name = entry.getKey();
        if (name == null)
            continue; // http/1.1 line
        List<String> values = entry.getValue();
        if (name.equalsIgnoreCase("Set-Cookie")) {
            for (String value : values) {
                if (value == null) {
                    continue;
                }
                String cookie = value.substring(0, value.indexOf(";") + 1);
                sBuffer.append(cookie);
            }
        }
    }
    if(sBuffer.length() > 0){
        return sBuffer.toString();
    }
    return sBuffer.toString();
}
 
源代码5 项目: uavstack   文件: WebHttpHelper.java
/**
 * getCookie
 * 
 * @param conn
 * @return
 */
public static String getCookie(HttpURLConnection conn) {

    Map<String, List<String>> resHeaders = conn.getHeaderFields();
    StringBuffer sBuffer = new StringBuffer();
    for (Map.Entry<String, List<String>> entry : resHeaders.entrySet()) {
        String name = entry.getKey();
        if (name == null)
            continue; // http/1.1 line
        List<String> values = entry.getValue();
        if (name.equalsIgnoreCase("Set-Cookie")) {
            for (String value : values) {
                if (value == null) {
                    continue;
                }
                String cookie = value.substring(0, value.indexOf(";") + 1);
                sBuffer.append(cookie);
            }
        }
    }
    if (sBuffer.length() > 0) {
        return sBuffer.toString();
    }
    return sBuffer.toString();
}
 
源代码6 项目: tns-core-modules-widgets   文件: Async.java
public void getHeaders(HttpURLConnection connection) {
    Map<String, List<String>> headers = connection.getHeaderFields();
    if (headers == null) {
        // no headers, this may happen if there is no internet connection currently available
        return;
    }

    int size = headers.size();
    if (size == 0) {
        return;
    }

    for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
        String key = entry.getKey();
        for (String value : entry.getValue()) {
            this.headers.add(new KeyValuePair(key, value));
        }
    }
}
 
源代码7 项目: wildfly-core   文件: S3Util.java
/**
 * Examines the response's header fields and returns a Map from String to List of Strings representing the
 * object's metadata.
 */
private static Map extractMetadata(HttpURLConnection connection) {
    TreeMap metadata = new TreeMap();
    Map headers = connection.getHeaderFields();
    for (Iterator i = headers.keySet().iterator(); i.hasNext();) {
        String key = (String) i.next();
        if (key == null) {
            continue;
        }
        if (key.startsWith(Utils.METADATA_PREFIX)) {
            metadata.put(key.substring(Utils.METADATA_PREFIX.length()), headers.get(key));
        }
    }

    return metadata;
}
 
源代码8 项目: jboot   文件: GatewayHttpProxy.java
private void configResponse(HttpServletResponse resp, HttpURLConnection conn) throws IOException {
    resp.setContentType(contentType);
    resp.setStatus(conn.getResponseCode());

    Map<String, List<String>> headerFields = conn.getHeaderFields();
    if (headerFields != null && !headerFields.isEmpty()) {
        Set<String> headerNames = headerFields.keySet();
        for (String headerName : headerNames) {
            //需要排除 Content-Encoding,因为 Server 可能已经使用 gzip 压缩,但是此代理已经对 gzip 内容进行解压了
            //需要排除 Content-Type,因为会可能会进行多次设置
            if (StrUtil.isBlank(headerName)
                    || "Content-Encoding".equalsIgnoreCase(headerName)
                    || "Content-Type".equalsIgnoreCase(headerName)) {
                continue;
            } else {
                String headerFieldValue = conn.getHeaderField(headerName);
                if (StrUtil.isNotBlank(headerFieldValue)) {
                    resp.setHeader(headerName, headerFieldValue);
                }
            }
        }
    }
}
 
源代码9 项目: neembuu-uploader   文件: UGotFileUploaderPlugin.java
private static void initialize() throws Exception {
    System.out.println("Getting startup cookie from ugotfile.com");
    u = new URL("http://ugotfile.com/");
    uc = (HttpURLConnection) u.openConnection();
    Map<String, List<String>> headerFields = uc.getHeaderFields();

    if (headerFields.containsKey("Set-Cookie")) {
        List<String> header = headerFields.get("Set-Cookie");
        for (int i = 0; i < header.size(); i++) {
            tmp = header.get(i);
            if (tmp.contains("PHPSESSID")) {
                phpsessioncookie = tmp;
            }

        }
    }
    phpsessioncookie = phpsessioncookie.substring(0, phpsessioncookie.indexOf(";"));
    System.out.println("phpsessioncookie : " + phpsessioncookie);

    br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
    String k = "";
    while ((tmp = br.readLine()) != null) {
        k += tmp;
    }

    postURL = parseResponse(k, "action=\"", "\"");
    System.out.println("Post URL : " + postURL);
}
 
源代码10 项目: neembuu-uploader   文件: UGotFile.java
private void initialize() throws Exception {
    NULogger.getLogger().info("Getting startup cookie from ugotfile.com");
    u = new URL("http://ugotfile.com/");
    uc = (HttpURLConnection) u.openConnection();
    Map<String, List<String>> headerFields = uc.getHeaderFields();

    if (headerFields.containsKey("Set-Cookie")) {
        List<String> header = headerFields.get("Set-Cookie");
        for (int i = 0; i < header.size(); i++) {
            tmp = header.get(i);
            if (tmp.contains("PHPSESSID")) {
                phpsessioncookie = tmp;
            }

        }
    }
    phpsessioncookie = phpsessioncookie.substring(0, phpsessioncookie.indexOf(";"));
    NULogger.getLogger().log(Level.INFO, "phpsessioncookie : {0}", phpsessioncookie);

    br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
    String k = "";
    while ((tmp = br.readLine()) != null) {
        k += tmp;
    }

    postURL = StringUtils.stringBetweenTwoStrings(k, "action=\"", "\"");
    NULogger.getLogger().log(Level.INFO, "Post URL : {0}", postURL);
}
 
源代码11 项目: big-c   文件: TestWebAppProxyServlet.java
private boolean isResponseCookiePresent(HttpURLConnection proxyConn, 
    String expectedName, String expectedValue) {
  Map<String, List<String>> headerFields = proxyConn.getHeaderFields();
  List<String> cookiesHeader = headerFields.get("Set-Cookie");
  if (cookiesHeader != null) {
    for (String cookie : cookiesHeader) {
      HttpCookie c = HttpCookie.parse(cookie).get(0);
      if (c.getName().equals(expectedName) 
          && c.getValue().equals(expectedValue)) {
        return true;
      }
    }
  }
  return false;
}
 
源代码12 项目: aliyun-tablestore-java-sdk   文件: HttpResponse.java
private static void pasrseHttpConn(HttpResponse response, HttpURLConnection httpConn,
                                   InputStream content) throws IOException {
    byte[] buff = readContent(content);
    response.setStatus(httpConn.getResponseCode());
    Map<String, List<String>> headers = httpConn.getHeaderFields();
    for (Entry<String, List<String>> entry : headers.entrySet()) {
        String key = entry.getKey();
        if (null == key) { continue; }
        List<String> values = entry.getValue();
        StringBuilder builder = new StringBuilder(values.get(0));
        for (int i = 1; i < values.size(); i++) {
            builder.append(",");
            builder.append(values.get(i));
        }
        response.putHeaderParameter(key, builder.toString());
    }
    String type = response.getHeaderValue("Content-Type");
    if (null != buff && null != type) {
        response.setEncoding("UTF-8");
        String[] split = type.split(";");
        response.setHttpContentType(FormatType.mapAcceptToFormat(split[0].trim()));
        if (split.length > 1 && split[1].contains("=")) {
            String[] codings = split[1].split("=");
            response.setEncoding(codings[1].trim().toUpperCase());
        }
    }
    response.setStatus(httpConn.getResponseCode());
    response.setHttpContent(buff, response.getEncoding(),
            response.getHttpContentType());
}
 
源代码13 项目: dolphin-platform   文件: HttpClientCookieHandler.java
public void updateCookiesFromResponse(final HttpURLConnection connection) throws URISyntaxException {
    Assert.requireNonNull(connection, "connection");
    LOG.debug("adding cookies from response to cookie store");

    final Map<String, List<String>> headerFields = connection.getHeaderFields();
    final List<String> cookiesHeader = headerFields.get(SET_COOKIE_HEADER);
    if (cookiesHeader != null) {
        LOG.debug("found '{}' header field", SET_COOKIE_HEADER);
        for (String cookie : cookiesHeader) {
            if (cookie == null || cookie.isEmpty()) {
                continue;
            }
            LOG.debug("will parse '{}' header content '{}'", cookie);
            final List<HttpCookie> cookies = new ArrayList<>();
            try {
                cookies.addAll(HttpCookie.parse(cookie));
            } catch (final Exception e) {
                throw new DolphinRuntimeException("Can not convert '" + SET_COOKIE_HEADER + "' response header field to http cookies. Bad content: " + cookie, e);
            }
            LOG.debug("Found {} http cookies in header", cookies.size());
            for (final HttpCookie httpCookie : cookies) {
                LOG.trace("Found Cookie '{}' for Domain '{}' at Ports '{}' with Path '{}", httpCookie.getValue(), httpCookie.getDomain(), httpCookie.getPortlist(), httpCookie.getPath());
                cookieStore.add(connection.getURL().toURI(), httpCookie);
            }

        }
    }
}
 
源代码14 项目: hadoop   文件: AuthenticatedURL.java
/**
 * Helper method that extracts an authentication token received from a connection.
 * <p>
 * This method is used by {@link Authenticator} implementations.
 *
 * @param conn connection to extract the authentication token from.
 * @param token the authentication token.
 *
 * @throws IOException if an IO error occurred.
 * @throws AuthenticationException if an authentication exception occurred.
 */
public static void extractToken(HttpURLConnection conn, Token token) throws IOException, AuthenticationException {
  int respCode = conn.getResponseCode();
  if (respCode == HttpURLConnection.HTTP_OK
      || respCode == HttpURLConnection.HTTP_CREATED
      || respCode == HttpURLConnection.HTTP_ACCEPTED) {
    Map<String, List<String>> headers = conn.getHeaderFields();
    List<String> cookies = headers.get("Set-Cookie");
    if (cookies != null) {
      for (String cookie : cookies) {
        if (cookie.startsWith(AUTH_COOKIE_EQ)) {
          String value = cookie.substring(AUTH_COOKIE_EQ.length());
          int separator = value.indexOf(";");
          if (separator > -1) {
            value = value.substring(0, separator);
          }
          if (value.length() > 0) {
            token.set(value);
          }
        }
      }
    }
  } else {
    token.set(null);
    throw new AuthenticationException("Authentication failed, status: " + conn.getResponseCode() +
                                      ", message: " + conn.getResponseMessage());
  }
}
 
源代码15 项目: neembuu-uploader   文件: UploadBox.java
private void initialize() throws Exception {
    NULogger.getLogger().info("Getting startup cookie from uploadbox.com");
    u = new URL("http://uploadbox.com/");
    uc = (HttpURLConnection) u.openConnection();
    Map<String, List<String>> headerFields = uc.getHeaderFields();

    if (headerFields.containsKey("Set-Cookie")) {
        List<String> header = headerFields.get("Set-Cookie");
        for (int i = 0; i < header.size(); i++) {
            tmp = header.get(i);
            if (tmp.contains("sid")) {
                sidcookie = tmp;
            }

        }
    }
    sidcookie = sidcookie.substring(0, sidcookie.indexOf(";"));
    NULogger.getLogger().log(Level.INFO, "sidcookie : {0}", sidcookie);
    br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
    String k = "";
    while ((tmp = br.readLine()) != null) {
        k += tmp;
    }
    postURL = StringUtils.stringBetweenTwoStrings(k, "action = \"", "\"");
    generateUploadBoxID();
    postURL = postURL + uid;
    NULogger.getLogger().log(Level.INFO, "Post URL : {0}", postURL);
    server = StringUtils.stringBetweenTwoStrings(k, "name=\"server\" value=\"", "\"");
    NULogger.getLogger().info(server);
}
 
源代码16 项目: neembuu-uploader   文件: DropBoxUploaderPlugin.java
private static void initialize() throws Exception {


        System.out.println("Init....");

        u = new URL("https://www.dropbox.com");

        uc = (HttpURLConnection) u.openConnection();
        String k = "", tmp = "";
        Map<String, List<String>> headerFields = uc.getHeaderFields();
        if (headerFields.containsKey("set-cookie")) {
            List<String> header = headerFields.get("set-cookie");
            for (int i = 0; i < header.size(); i++) {
                tmp = header.get(i);
//                System.out.println(tmp);
                if (tmp.contains("gvc=")) {
                    gvcCookie = tmp;
                    gvcCookie = tmp.substring(0, tmp.indexOf(";"));
                }
                if (tmp.contains("t=")) {
                    tCookie = tmp;
                    tCookie = tmp.substring(0, tmp.indexOf(";"));
                }
            }
        }

        System.out.println("gvc cookie : " + gvcCookie);
        System.out.println("tcookie : " + tCookie);
        u = null;
        uc = null;
    }
 
源代码17 项目: Tomcat8-Source-Read   文件: TestNonBlockingAPI.java
public static int postUrlWithDisconnect(boolean stream, BytesStreamer streamer, String path,
        Map<String, List<String>> reqHead, Map<String, List<String>> resHead) throws IOException {

    URL url = new URL(path);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setReadTimeout(1000000);
    if (reqHead != null) {
        for (Map.Entry<String, List<String>> entry : reqHead.entrySet()) {
            StringBuilder valueList = new StringBuilder();
            for (String value : entry.getValue()) {
                if (valueList.length() > 0) {
                    valueList.append(',');
                }
                valueList.append(value);
            }
            connection.setRequestProperty(entry.getKey(), valueList.toString());
        }
    }
    if (streamer != null && stream) {
        if (streamer.getLength() > 0) {
            connection.setFixedLengthStreamingMode(streamer.getLength());
        } else {
            connection.setChunkedStreamingMode(1024);
        }
    }

    connection.connect();

    // Write the request body
    try (OutputStream os = connection.getOutputStream()) {
        while (streamer != null && streamer.available() > 0) {
            byte[] next = streamer.next();
            os.write(next);
            os.flush();
        }
    }

    int rc = connection.getResponseCode();
    if (resHead != null) {
        Map<String, List<String>> head = connection.getHeaderFields();
        resHead.putAll(head);
    }
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {

    }
    if (rc == HttpServletResponse.SC_OK) {
        connection.getInputStream().close();
        connection.disconnect();
    }
    return rc;
}
 
源代码18 项目: neembuu-uploader   文件: SlingFileUploaderPlugin.java
private static void initialize() throws IOException {

        if (login) {
            System.out.println("After login,geting the link again :)");
        } else {
            System.out.println("Getting startup cookies & link from slingfile.com");
        }
        u = new URL("http://www.slingfile.com/");
        uc = (HttpURLConnection) u.openConnection();
        if (login) {
            uc.setRequestProperty("Cookie", cookie.toString());
        }
        br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        String temp = "", k = "";
        while ((temp = br.readLine()) != null) {
//            System.out.println(temp);
            k += temp;
        }

        
        //http://sf002-01.slingfile.com/upload?X-Progress-ID=p173qvokvee3uk2h1kp776215pa4
//        System.exit(0);
//
//        sling_guest_url = parseResponse(k, "single-premium?uu=", "'");
//        System.out.println("sling guest url : " + sling_guest_url);
//        ssd = parseResponse(sling_guest_url, "&ssd=", "&rd");
//        System.out.println("SSD : " + ssd);
//        postURL = parseResponse(sling_guest_url, "http://", "&ssd");
//        postURL = "http://" + postURL;
//        System.out.println("Post URL : " + postURL);

//        postuploadpage = sling_guest_url.substring(sling_guest_url.indexOf("&rd=") + 4);
//        System.out.println("post upload page : " + postuploadpage);


        cookie = new StringBuilder();

        Map<String, List<String>> headerFields = uc.getHeaderFields();
        if (headerFields.containsKey("Set-Cookie")) {
            List<String> header = headerFields.get("Set-Cookie");
            for (int i = 0; i < header.size(); i++) {
                cookie.append(header.get(i)).append(";");

            }
            System.out.println(cookie.toString());

        }

    }
 
源代码19 项目: sakai   文件: PortletIFrame.java
public boolean popupXFrame(RenderRequest request, Placement placement, String url) 
{
    if ( xframeCache < 1 ) return false;

    // Only check http:// and https:// urls
    if ( ! (url.startsWith("http://") || url.startsWith("https://")) ) return false;

    // Check the "Always POPUP" and "Always INLINE" regular expressions
    String pattern = null;
    Pattern p = null;
    Matcher m = null;
    pattern = ServerConfigurationService.getString(IFRAME_XFRAME_POPUP, null);
    if ( pattern != null && pattern.length() > 1 ) {
        p = Pattern.compile(pattern);
        m = p.matcher(url.toLowerCase());
        if ( m.find() ) {
            return true;
        }
    }
    pattern = ServerConfigurationService.getString(IFRAME_XFRAME_INLINE, null);
    if ( pattern != null && pattern.length() > 1 ) {
        p = Pattern.compile(pattern);
        m = p.matcher(url.toLowerCase());
        if ( m.find() ) {
            return false;
        }
    }

    // Don't check Local URLs
    String serverUrl = ServerConfigurationService.getServerUrl();
    if ( url.startsWith(serverUrl) ) return false;
    if ( url.startsWith(ServerConfigurationService.getAccessUrl()) ) return false;

    // Force http:// to pop-up if we are https://
    if ( request.isSecure() || ( serverUrl != null && serverUrl.startsWith("https://") ) ) {
        if ( url.startsWith("http://") ) return true;
    }

    // Check to see if time has expired...
    Date date = new Date();
    long nowTime = date.getTime();
    
    String lastTimeS = placement.getPlacementConfig().getProperty(XFRAME_LAST_TIME);
    long lastTime = -1;
    try {
        lastTime = Long.parseLong(lastTimeS);
    } catch (NumberFormatException nfe) {
        lastTime = -1;
    }

    log.debug("lastTime="+lastTime+" nowTime="+nowTime);

    if ( lastTime > 0 && nowTime < lastTime + xframeCache ) {
        String lastXF = placement.getPlacementConfig().getProperty(XFRAME_LAST_STATUS);
        log.debug("Status from placement="+lastXF);
        return "true".equals(lastXF);
    }

    placement.getPlacementConfig().setProperty(XFRAME_LAST_TIME, String.valueOf(nowTime));
    boolean retval = false;
    try {
        // note : you may also need
        //        HttpURLConnection.setInstanceFollowRedirects(false)
        HttpURLConnection.setFollowRedirects(true);
        HttpURLConnection con =
            (HttpURLConnection) new URL(url).openConnection();
        con.setRequestMethod("HEAD");

        String sakaiVersion = ServerConfigurationService.getString("version.sakai", "?");
        con.setRequestProperty("User-Agent","Java Sakai/"+sakaiVersion);

        Map headerfields = con.getHeaderFields();
        Set headers = headerfields.entrySet(); 
        for(Iterator i = headers.iterator(); i.hasNext();) { 
            Map.Entry map = (Map.Entry)i.next();
            String key = (String) map.getKey();
            if ( key == null ) continue;
            key = key.toLowerCase();
            if ( ! "x-frame-options".equals(key) ) continue;

            // Since the valid entries are SAMEORIGIN, DENY, or ALLOW-URI
            // we can pretty much assume the answer is "not us" if the header
            // is present
            retval = true;
            break;
        }

    }
    catch (Exception e) {
        // Fail pretty silently because this could be pretty chatty with bad urls and all
        log.debug(e.getMessage());
        retval = false;
    }
    placement.getPlacementConfig().setProperty(XFRAME_LAST_STATUS, String.valueOf(retval));
    // Permanently set popup to true as we don't expect that a site will go back
    if ( retval == true ) placement.getPlacementConfig().setProperty(POPUP, "true");
    placement.save();
    log.debug("Retrieved="+url+" XFrame="+retval);
    return retval;
}
 
源代码20 项目: MySpider   文件: TestGetHtml.java
@Test
public void testHTTP() throws IOException {
    URL target = new URL("https://www.baidu.com/");

    HttpURLConnection urlConnection = (HttpURLConnection) target.openConnection();

    Map<String, List<String>> map = urlConnection.getHeaderFields();

    System.out.println(map);
}