Java Code Examples for java.net.httpurlconnection#getHeaderFields()
The following examples show how to use
java.net.httpurlconnection#getHeaderFields() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GatewayHttpProxy.java From jboot with Apache License 2.0 | 6 votes |
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); } } } } }
Example 2
Source File: S3Util.java From wildfly-core with GNU Lesser General Public License v2.1 | 6 votes |
/** * 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; }
Example 3
Source File: HttpURLConnectionIT.java From pinpoint with Apache License 2.0 | 6 votes |
@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))); }
Example 4
Source File: WSUrlFetch.java From restcommander with Apache License 2.0 | 6 votes |
/** * 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(); } }
Example 5
Source File: WuploadUploaderPlugin.java From neembuu-uploader with GNU General Public License v3.0 | 6 votes |
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); } }
Example 6
Source File: Async.java From tns-core-modules-widgets with Apache License 2.0 | 6 votes |
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)); } } }
Example 7
Source File: WebHttpHelper.java From uavstack with Apache License 2.0 | 6 votes |
/** * 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(); }
Example 8
Source File: CookieUtil.java From wechatGroupRobot with GNU General Public License v3.0 | 6 votes |
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(); }
Example 9
Source File: HttpClientCookieHandler.java From dolphin-platform with Apache License 2.0 | 5 votes |
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); } } } }
Example 10
Source File: DropBoxUploaderPlugin.java From neembuu-uploader with GNU General Public License v3.0 | 5 votes |
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; }
Example 11
Source File: UploadBox.java From neembuu-uploader with GNU General Public License v3.0 | 5 votes |
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); }
Example 12
Source File: AuthenticatedURL.java From hadoop with Apache License 2.0 | 5 votes |
/** * 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()); } }
Example 13
Source File: HttpResponse.java From aliyun-tablestore-java-sdk with Apache License 2.0 | 5 votes |
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()); }
Example 14
Source File: TestWebAppProxyServlet.java From big-c with Apache License 2.0 | 5 votes |
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; }
Example 15
Source File: UGotFile.java From neembuu-uploader with GNU General Public License v3.0 | 5 votes |
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); }
Example 16
Source File: UGotFileUploaderPlugin.java From neembuu-uploader with GNU General Public License v3.0 | 5 votes |
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); }
Example 17
Source File: TestNonBlockingAPI.java From Tomcat8-Source-Read with MIT License | 4 votes |
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; }
Example 18
Source File: SlingFileUploaderPlugin.java From neembuu-uploader with GNU General Public License v3.0 | 4 votes |
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()); } }
Example 19
Source File: PortletIFrame.java From sakai with Educational Community License v2.0 | 4 votes |
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; }
Example 20
Source File: TestGetHtml.java From MySpider with MIT License | 3 votes |
@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); }