Java Code Examples for java.net.httpurlconnection#getHeaderField()
The following examples show how to use
java.net.httpurlconnection#getHeaderField() .
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: CordovaResourceApi.java From wildfly-samples with MIT License | 6 votes |
public String getMimeType(Uri uri) { switch (getUriType(uri)) { case URI_TYPE_FILE: case URI_TYPE_ASSET: return getMimeTypeFromPath(uri.getPath()); case URI_TYPE_CONTENT: case URI_TYPE_RESOURCE: return contentResolver.getType(uri); case URI_TYPE_DATA: { return getDataUriMimeType(uri); } case URI_TYPE_HTTP: case URI_TYPE_HTTPS: { try { HttpURLConnection conn = httpClient.open(new URL(uri.toString())); conn.setDoInput(false); conn.setRequestMethod("HEAD"); return conn.getHeaderField("Content-Type"); } catch (IOException e) { } } } return null; }
Example 3
Source File: DigestAuthentication.java From Komondor with GNU General Public License v3.0 | 6 votes |
/** * Get the digest challenge header by connecting to the resource * with no credentials. */ public static String getChallengeHeader(String url) throws IOException { HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setDoOutput(true); conn.getOutputStream().close(); try { conn.getInputStream().close(); } catch (IOException ex) { if (401 == conn.getResponseCode()) { // we expect a 401-unauthorized response with the // WWW-Authenticate header to create the request with the // necessary auth data String hdr = conn.getHeaderField("WWW-Authenticate"); if (hdr != null && !"".equals(hdr)) { return hdr; } } else if (400 == conn.getResponseCode()) { // 400 usually means that auth is disabled on the Fabric node throw new IOException("Fabric returns status 400. If authentication is disabled on the Fabric node, " + "omit the `fabricUsername' and `fabricPassword' properties from your connection."); } else { throw ex; } } return null; }
Example 4
Source File: LocalDocWriter.java From TranskribusCore with GNU General Public License v3.0 | 6 votes |
private static String getFilenameFromUrl(URL url) throws IOException { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.connect(); String raw = conn.getHeaderField("Content-Disposition"); // raw = "attachment; filename=abc.jpg" if(raw != null && raw.indexOf("=") != -1) { String[] splits = raw.split("="); if (splits.length > 1) { return raw.split("=")[1]; } } conn.disconnect(); return null; }
Example 5
Source File: HttpHelper.java From ZXing-Orient with Apache License 2.0 | 5 votes |
private static CharSequence downloadViaHttp(String uri, String contentTypes, int maxChars) throws IOException { int redirects = 0; while (redirects < 5) { URL url = new URL(uri); HttpURLConnection connection = safelyOpenConnection(url); connection.setInstanceFollowRedirects(true); // Won't work HTTP -> HTTPS or vice versa connection.setRequestProperty("Accept", contentTypes); connection.setRequestProperty("Accept-Charset", "utf-8,*"); connection.setRequestProperty("User-Agent", "ZXing (Android)"); try { int responseCode = safelyConnect(connection); switch (responseCode) { case HttpURLConnection.HTTP_OK: return consume(connection, maxChars); case HttpURLConnection.HTTP_MOVED_TEMP: String location = connection.getHeaderField("Location"); if (location != null) { uri = location; redirects++; continue; } throw new IOException("No Location"); default: throw new IOException("Bad HTTP response: " + responseCode); } } finally { connection.disconnect(); } } throw new IOException("Too many redirects"); }
Example 6
Source File: HttpUtils.java From azeroth with Apache License 2.0 | 5 votes |
public static void downloadFile(String fileURL, String saveDir) throws IOException { URL url = new URL(fileURL); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); int responseCode = httpConn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { String fileName = ""; String disposition = httpConn.getHeaderField("Content-Disposition"); if (disposition != null) { int index = disposition.indexOf("filename="); if (index > 0) { fileName = disposition.substring(index + 10, disposition.length() - 1); } } else { fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1, fileURL.length()); } InputStream inputStream = httpConn.getInputStream(); String saveFilePath = saveDir + File.separator + fileName; FileOutputStream outputStream = new FileOutputStream(saveFilePath); int bytesRead = -1; byte[] buffer = new byte[2048]; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.close(); inputStream.close(); } else { throw new BaseException(responseCode, "下载失败"); } httpConn.disconnect(); }
Example 7
Source File: ExporterServerTest.java From sofa-lookout with Apache License 2.0 | 5 votes |
public static String sendHttpRequest(URL url) throws IOException { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); //使用get请求 conn.connect(); String length = conn.getHeaderField("Content-Length"); byte[] bytes = new byte[Integer.valueOf(length)]; IOUtils.readFully(conn.getInputStream(), bytes); return new String(bytes, Charset.forName("utf-8")); }
Example 8
Source File: JnlpLauncher.java From weasis-pacs-connector with Eclipse Public License 2.0 | 5 votes |
private void addWeasisParameters(Map<String, Object> queryParameterMap, String appURL) { boolean enableVersion = false; String appVersion = ""; String felix = ""; String substance = ""; try { URL obj = new URL(appURL); HttpURLConnection conn = (HttpURLConnection) obj.openConnection(); int status = conn.getResponseCode(); if (status == HttpURLConnection.HTTP_OK) { String av = conn.getHeaderField("AppVersion"); String fv = conn.getHeaderField("FelixVersion"); String sv = conn.getHeaderField("SubstanceVersion"); if (av != null && fv != null && sv != null) { appVersion = av; felix = fv; substance = sv; enableVersion = true; } } } catch (Exception e) { LOGGER.error("Cannot get information of weasis package", e); } queryParameterMap.put("jnlp.jar.version", enableVersion); queryParameterMap.put("app.version", appVersion); queryParameterMap.put("felix.framework.version", felix); queryParameterMap.put("substance.version", substance); }
Example 9
Source File: TestHttpCookieFlag.java From big-c with Apache License 2.0 | 5 votes |
@Test public void testHttpCookie() throws IOException { URL base = new URL("http://" + NetUtils.getHostPortString(server .getConnectorAddress(0))); HttpURLConnection conn = (HttpURLConnection) new URL(base, "/echo").openConnection(); String header = conn.getHeaderField("Set-Cookie"); List<HttpCookie> cookies = HttpCookie.parse(header); Assert.assertTrue(!cookies.isEmpty()); Assert.assertTrue(header.contains("; HttpOnly")); Assert.assertTrue("token".equals(cookies.get(0).getValue())); }
Example 10
Source File: CordovaResourceApi.java From lona with GNU General Public License v3.0 | 5 votes |
public String getMimeType(Uri uri) { switch (getUriType(uri)) { case URI_TYPE_FILE: case URI_TYPE_ASSET: return getMimeTypeFromPath(uri.getPath()); case URI_TYPE_CONTENT: case URI_TYPE_RESOURCE: return contentResolver.getType(uri); case URI_TYPE_DATA: { return getDataUriMimeType(uri); } case URI_TYPE_HTTP: case URI_TYPE_HTTPS: { try { HttpURLConnection conn = (HttpURLConnection)new URL(uri.toString()).openConnection(); conn.setDoInput(false); conn.setRequestMethod("HEAD"); String mimeType = conn.getHeaderField("Content-Type"); if (mimeType != null) { mimeType = mimeType.split(";")[0]; } return mimeType; } catch (IOException e) { } } } return null; }
Example 11
Source File: BrotliServletFilterIntegrationTest.java From jbrotli with Apache License 2.0 | 5 votes |
@Test public void brotli_content_encoding_is_set() throws Exception { // given URL textFileUrl = new URL(root_url + "/canterbury-corpus/alice29.txt"); // when HttpURLConnection httpCon = (HttpURLConnection) textFileUrl.openConnection(); httpCon.addRequestProperty("Accept-Encoding", BROTLI_HTTP_CONTENT_CODING); httpCon.connect(); String contentEncoding = httpCon.getHeaderField("Content-Encoding"); httpCon.disconnect(); // then assertThat(contentEncoding).isEqualTo(BROTLI_HTTP_CONTENT_CODING); }
Example 12
Source File: HttpServerTest.java From msf4j with Apache License 2.0 | 5 votes |
@Test public void testContentTypeSetting0() throws Exception { HttpURLConnection urlConn = request("/test/v1/response/typehtml", HttpMethod.GET); assertEquals(Response.Status.OK.getStatusCode(), urlConn.getResponseCode()); String contentType = urlConn.getHeaderField(HttpHeaders.CONTENT_TYPE); assertTrue(contentType.equalsIgnoreCase(MediaType.TEXT_HTML)); String content = getContent(urlConn); assertEquals("Hello", content); urlConn.disconnect(); }
Example 13
Source File: JwtAuthenticationServiceImplTest.java From blueocean-plugin with MIT License | 5 votes |
private String getToken(JenkinsRule.WebClient webClient) throws IOException { URL tokenUrl = new URL(webClient.getContextPath() + "jwt-auth/token/"); HttpURLConnection connection = (HttpURLConnection) tokenUrl.openConnection(); Set<Cookie> cookies = webClient.getCookies(tokenUrl); for (Cookie cookie : cookies) { connection.addRequestProperty("Cookie", cookie.getName() + "=" + cookie.getValue()); } Assert.assertEquals(connection.getResponseCode(), 204); String token = connection.getHeaderField("X-BLUEOCEAN-JWT"); connection.disconnect(); return token; }
Example 14
Source File: DefaultHttpDataSource.java From Telegram with GNU General Public License v2.0 | 4 votes |
/** * Establishes a connection, following redirects to do so where permitted. */ private HttpURLConnection makeConnection(DataSpec dataSpec) throws IOException { URL url = new URL(dataSpec.uri.toString()); @HttpMethod int httpMethod = dataSpec.httpMethod; byte[] httpBody = dataSpec.httpBody; long position = dataSpec.position; long length = dataSpec.length; boolean allowGzip = dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_GZIP); boolean allowIcyMetadata = dataSpec.isFlagSet(DataSpec.FLAG_ALLOW_ICY_METADATA); if (!allowCrossProtocolRedirects) { // HttpURLConnection disallows cross-protocol redirects, but otherwise performs redirection // automatically. This is the behavior we want, so use it. return makeConnection( url, httpMethod, httpBody, position, length, allowGzip, allowIcyMetadata, /* followRedirects= */ true); } // We need to handle redirects ourselves to allow cross-protocol redirects. int redirectCount = 0; while (redirectCount++ <= MAX_REDIRECTS) { HttpURLConnection connection = makeConnection( url, httpMethod, httpBody, position, length, allowGzip, allowIcyMetadata, /* followRedirects= */ false); int responseCode = connection.getResponseCode(); String location = connection.getHeaderField("Location"); if ((httpMethod == DataSpec.HTTP_METHOD_GET || httpMethod == DataSpec.HTTP_METHOD_HEAD) && (responseCode == HttpURLConnection.HTTP_MULT_CHOICE || responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_SEE_OTHER || responseCode == HTTP_STATUS_TEMPORARY_REDIRECT || responseCode == HTTP_STATUS_PERMANENT_REDIRECT)) { connection.disconnect(); url = handleRedirect(url, location); } else if (httpMethod == DataSpec.HTTP_METHOD_POST && (responseCode == HttpURLConnection.HTTP_MULT_CHOICE || responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_SEE_OTHER)) { // POST request follows the redirect and is transformed into a GET request. connection.disconnect(); httpMethod = DataSpec.HTTP_METHOD_GET; httpBody = null; url = handleRedirect(url, location); } else { return connection; } } // If we get here we've been redirected more times than are permitted. throw new NoRouteToHostException("Too many redirects: " + redirectCount); }
Example 15
Source File: APITest.java From cosmic with Apache License 2.0 | 4 votes |
/** * Sending an api request through Http GET * * @param command command name * @param params command query parameters in a HashMap * @return http request response string */ protected String sendRequest(final String command, final HashMap<String, String> params) { try { // Construct query string final StringBuilder sBuilder = new StringBuilder(); sBuilder.append("command="); sBuilder.append(command); if (params != null && params.size() > 0) { final Iterator<String> keys = params.keySet().iterator(); while (keys.hasNext()) { final String key = keys.next(); sBuilder.append("&"); sBuilder.append(key); sBuilder.append("="); sBuilder.append(URLEncoder.encode(params.get(key), "UTF-8")); } } // Construct request url final String reqUrl = rootUrl + "?" + sBuilder.toString(); // Send Http GET request final URL url = new URL(reqUrl); final HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); if (!command.equals("login") && cookieToSent != null) { // add the cookie to a request conn.setRequestProperty("Cookie", cookieToSent); } conn.connect(); if (command.equals("login")) { // if it is login call, store cookie String headerName = null; for (int i = 1; (headerName = conn.getHeaderFieldKey(i)) != null; i++) { if (headerName.equals("Set-Cookie")) { String cookie = conn.getHeaderField(i); cookie = cookie.substring(0, cookie.indexOf(";")); final String cookieName = cookie.substring(0, cookie.indexOf("=")); final String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length()); cookieToSent = cookieName + "=" + cookieValue; } } } // Get the response final StringBuilder response = new StringBuilder(); final BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; try { while ((line = rd.readLine()) != null) { response.append(line); } } catch (final EOFException ex) { // ignore this exception System.out.println("EOF exception due to java bug"); } rd.close(); return response.toString(); } catch (final Exception e) { throw new CloudRuntimeException("Problem with sending api request", e); } }
Example 16
Source File: PseudoWebHDFSConnection.java From Transwarp-Sample-Code with MIT License | 4 votes |
/** * <b>CREATE</b> * * curl -i -X PUT "http://<HOST>:<PORT>/webhdfs/v1/<PATH>?op=CREATE * [&overwrite=<true|false>][&blocksize=<LONG>][&replication=<SHORT>] * [&permission=<OCTAL>][&buffersize=<INT>]" * * @param path * @param is * @return * @throws MalformedURLException * @throws IOException * @throws AuthenticationException */ public String create(String path, InputStream is) throws MalformedURLException, IOException, AuthenticationException { String resp = null; ensureValidToken(); String spec = MessageFormat.format( "/webhdfs/v1/{0}?op=CREATE&user.name={1}", URLUtil.encodePath(path), this.principal); String redirectUrl = null; HttpURLConnection conn = authenticatedURL.openConnection(new URL( new URL(httpfsUrl), spec), token); conn.setRequestMethod("PUT"); conn.setInstanceFollowRedirects(false); conn.connect(); logger.info("Location:" + conn.getHeaderField("Location")); resp = result(conn, true); if (conn.getResponseCode() == 307) redirectUrl = conn.getHeaderField("Location"); conn.disconnect(); if (redirectUrl != null) { conn = authenticatedURL.openConnection(new URL(redirectUrl), token); conn.setRequestMethod("PUT"); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestProperty("Content-Type", "application/octet-stream"); // conn.setRequestProperty("Transfer-Encoding", "chunked"); final int _SIZE = is.available(); conn.setRequestProperty("Content-Length", "" + _SIZE); conn.setFixedLengthStreamingMode(_SIZE); conn.connect(); OutputStream os = conn.getOutputStream(); copy(is, os); // Util.copyStream(is, os); is.close(); os.close(); resp = result(conn, false); conn.disconnect(); } return resp; }
Example 17
Source File: OcspMgmtClient.java From xipki with Apache License 2.0 | 4 votes |
private byte[] transmit(MgmtAction action, MgmtRequest req, boolean voidReturn) throws OcspMgmtException { initIfNotDone(); byte[] reqBytes = req == null ? null : JSON.toJSONBytes(req); int size = reqBytes == null ? 0 : reqBytes.length; URL url = actionUrlMap.get(action); try { HttpURLConnection httpUrlConnection = IoUtil.openHttpConn(url); if (httpUrlConnection instanceof HttpsURLConnection) { if (sslSocketFactory != null) { ((HttpsURLConnection) httpUrlConnection).setSSLSocketFactory(sslSocketFactory); } if (hostnameVerifier != null) { ((HttpsURLConnection) httpUrlConnection).setHostnameVerifier(hostnameVerifier); } } httpUrlConnection.setDoOutput(true); httpUrlConnection.setUseCaches(false); httpUrlConnection.setRequestMethod("POST"); httpUrlConnection.setRequestProperty("Content-Type", REQUEST_CT); httpUrlConnection.setRequestProperty("Content-Length", java.lang.Integer.toString(size)); OutputStream outputstream = httpUrlConnection.getOutputStream(); if (size != 0) { outputstream.write(reqBytes); } outputstream.flush(); if (httpUrlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { InputStream in = httpUrlConnection.getInputStream(); boolean inClosed = false; try { String responseContentType = httpUrlConnection.getContentType(); if (!RESPONSE_CT.equals(responseContentType)) { throw new OcspMgmtException( "bad response: mime type " + responseContentType + " not supported!"); } if (voidReturn) { return null; } else { inClosed = true; return IoUtil.read(httpUrlConnection.getInputStream()); } } finally { if (in != null & !inClosed) { in.close(); } } } else { String errorMessage = httpUrlConnection.getHeaderField(HttpConstants.HEADER_XIPKI_ERROR); if (errorMessage == null) { StringBuilder sb = new StringBuilder(100); sb.append("server returns ").append(httpUrlConnection.getResponseCode()); String respMsg = httpUrlConnection.getResponseMessage(); if (StringUtil.isNotBlank(respMsg)) { sb.append(" ").append(respMsg); } throw new OcspMgmtException(sb.toString()); } else { throw new OcspMgmtException(errorMessage); } } } catch (IOException ex) { throw new OcspMgmtException( "IOException while sending message to the server: " + ex.getMessage(), ex); } }
Example 18
Source File: CloudFile.java From azure-storage-android with Apache License 2.0 | 4 votes |
protected void updateLengthFromResponse(HttpURLConnection request) { final String xContentLengthHeader = request.getHeaderField(FileConstants.CONTENT_LENGTH_HEADER); if (!Utility.isNullOrEmpty(xContentLengthHeader)) { this.getProperties().setLength(Long.parseLong(xContentLengthHeader)); } }
Example 19
Source File: HttpUtil.java From aliyun-tsdb-java-sdk with Apache License 2.0 | 4 votes |
/** * Send a request * @param method HTTP method, for example "GET" or "POST" * @param url Url as string * @param body Request body as string * @param headers Optional map with headers * @return response Response as string * @throws IOException */ static public String fetch(String method, String url, String body, Map<String, String> headers) throws IOException { // connection URL u = new URL(url); HttpURLConnection conn = (HttpURLConnection)u.openConnection(); conn.setConnectTimeout(10000); conn.setReadTimeout(10000); // method if (method != null) { conn.setRequestMethod(method); } // headers if (headers != null) { for(String key : headers.keySet()) { conn.addRequestProperty(key, headers.get(key)); } } // body if (body != null) { conn.setDoOutput(true); OutputStream os = conn.getOutputStream(); os.write(body.getBytes()); os.flush(); os.close(); } // response InputStream is = conn.getInputStream(); String response = streamToString(is); is.close(); // handle redirects if (conn.getResponseCode() == 301) { String location = conn.getHeaderField("Location"); return fetch(method, location, body, headers); } return response; }
Example 20
Source File: DownloadUtils.java From Dota2Helper with Apache License 2.0 | votes |
/** * TODO 获得真实地址 * * @param segUrl * @return 302跳转后的地址 */ public static String getLocation(String segUrl) { try { URL url = new URL(segUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(20000); conn.setConnectTimeout(15000); conn.setInstanceFollowRedirects(false); conn.setRequestMethod("HEAD"); return conn.getHeaderField("Location"); } catch (IOException e) { Logger.e(TAG, "DownloadUtils#getLocation()", e); } return null; }