Java Code Examples for java.net.URLConnection#addRequestProperty()
The following examples show how to use
java.net.URLConnection#addRequestProperty() .
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: Headers.java From cxf with Apache License 2.0 | 6 votes |
/** * This procedure sets the URLConnection request properties * from the PROTOCOL_HEADERS in the message. */ private void transferProtocolHeadersToURLConnection(URLConnection connection) { boolean addHeaders = MessageUtils.getContextualBoolean(message, ADD_HEADERS_PROPERTY, false); for (Map.Entry<String, List<String>> entry : headers.entrySet()) { String header = entry.getKey(); if (HttpHeaderHelper.CONTENT_TYPE.equalsIgnoreCase(header)) { continue; } List<String> headerList = entry.getValue(); if (addHeaders || HttpHeaderHelper.COOKIE.equalsIgnoreCase(header)) { headerList.forEach(s -> connection.addRequestProperty(header, s)); } else { connection.setRequestProperty(header, String.join(",", headerList)); } } // make sure we don't add more than one User-Agent header if (connection.getRequestProperty("User-Agent") == null) { connection.addRequestProperty("User-Agent", USER_AGENT); } }
Example 2
Source File: IMFRemoteSearchCallable.java From jsr354-ri with Apache License 2.0 | 6 votes |
@Override public IMFRemoteSearchResult call() throws Exception { URLConnection connection = getConnection(); if(Objects.isNull(connection)) { return null; } connection.addRequestProperty("User-Agent", userAgent); try (InputStream inputStream = connection.getInputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream()) { byte[] data = new byte[4096]; int read = inputStream.read(data); while (read > 0) { stream.write(data, 0, read); read = inputStream.read(data); } return new IMFRemoteSearchResult(type, new ByteArrayInputStream(stream.toByteArray())); } catch (Exception e) { LOG.log(Level.INFO, "Failed to load resource from url " + type.getUrl(yearMonth), e); } return null; }
Example 3
Source File: URLFetchTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testURLConnection() throws Exception { URL fetch = getFetchUrl(); URLConnection conn = fetch.openConnection(); Assert.assertTrue(conn instanceof HttpURLConnection); HttpURLConnection huc = (HttpURLConnection) conn; conn.setDoOutput(true); conn.setDoInput(true); conn.addRequestProperty("Content-Type", "application/octet-stream"); huc.connect(); try { OutputStream out = conn.getOutputStream(); out.write("Juhuhu".getBytes()); out.flush(); String content = new String(FetchServlet.toBytes(conn.getInputStream())); Assert.assertEquals("Bruhuhu", content); Assert.assertEquals(200, huc.getResponseCode()); } finally { huc.disconnect(); } }
Example 4
Source File: JAXRSClientServerResourceCreatedSpringProviderTest.java From cxf with Apache License 2.0 | 6 votes |
@Test public void testGetBook123() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/webapp/resources/bookstore/books/123"; URL url = new URL(endpointAddress); URLConnection connect = url.openConnection(); connect.addRequestProperty("Accept", "application/json"); connect.addRequestProperty("Content-Language", "badgerFishLanguage"); InputStream in = connect.getInputStream(); assertNotNull(in); //Ensure BadgerFish output as this should have replaced the standard JSONProvider InputStream expected = getClass() .getResourceAsStream("resources/expected_get_book123badgerfish.txt"); assertEquals("BadgerFish output not correct", stripXmlInstructionIfNeeded(getStringFromInputStream(expected).trim()), stripXmlInstructionIfNeeded(getStringFromInputStream(in).trim())); }
Example 5
Source File: Jaziz.java From VileBot with MIT License | 6 votes |
private static String getContent( String word ) throws Exception { String content; URLConnection connection; connection = new URL( API_URL + word + API_FORMAT ).openConnection(); connection.addRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" ); try { Scanner scanner = new Scanner( connection.getInputStream() ); scanner.useDelimiter( "\\Z" ); content = scanner.next(); return content; } catch ( FileNotFoundException e ) { return "{}"; } }
Example 6
Source File: ArchiveReaderFactory.java From webarchive-commons with Apache License 2.0 | 6 votes |
protected ArchiveReader getArchiveReader(final URL f, final long offset) throws IOException { // Get URL connection. URLConnection connection = f.openConnection(); if (connection instanceof HttpURLConnection) { addUserAgent((HttpURLConnection)connection); } if (offset != 0) { // Use a Range request (Assumes HTTP 1.1 on other end). If // length >= 0, add open-ended range header to the request. Else, // because end-byte is inclusive, subtract 1. connection.addRequestProperty("Range", "bytes=" + offset + "-"); // TODO: should actually verify that server respected 'Range' request // (spec allows them to ignore; 206 response or Content-Range header // should be present if Range satisfied; multipart/byteranges could be // a problem). } return getArchiveReader(f.toString(), connection.getInputStream(), (offset == 0)); }
Example 7
Source File: JAXRSClientServerSpringBookTest.java From cxf with Apache License 2.0 | 6 votes |
private void getBook(String endpointAddress, String resource, String type, String mHeader) throws Exception { URL url = new URL(endpointAddress); URLConnection connect = url.openConnection(); connect.addRequestProperty("Content-Type", "*/*"); connect.addRequestProperty("Accept", type); connect.addRequestProperty("Accept-Encoding", "gzip;q=1.0, identity; q=0.5, *;q=0"); if (mHeader != null) { connect.addRequestProperty("X-HTTP-Method-Override", mHeader); } InputStream in = connect.getInputStream(); InputStream expected = getClass().getResourceAsStream(resource); assertEquals(stripXmlInstructionIfNeeded(getStringFromInputStream(expected)), stripXmlInstructionIfNeeded(getStringFromInputStream(in))); }
Example 8
Source File: TestStringsWithRestEasy.java From curator with Apache License 2.0 | 6 votes |
private String getJson(String urlStr, String body) throws IOException { URL url = new URL(urlStr); URLConnection urlConnection = url.openConnection(); urlConnection.addRequestProperty("Accept", "application/json"); if ( body != null ) { ((HttpURLConnection)urlConnection).setRequestMethod("PUT"); urlConnection.addRequestProperty("Content-Type", "application/json"); urlConnection.addRequestProperty("Content-Length", Integer.toString(body.length())); urlConnection.setDoOutput(true); OutputStream out = urlConnection.getOutputStream(); ByteSource.wrap(body.getBytes()).copyTo(out); } BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); try { return CharStreams.toString(in); } finally { in.close(); } }
Example 9
Source File: WorkflowBundleIO.java From incubator-taverna-language with Apache License 2.0 | 5 votes |
private void addAcceptHeaders(String mediaType, URLConnection connection) { connection.addRequestProperty("Accept", mediaType); if (mediaType.endsWith("+zip") || mediaType.equals("vnd.taverna.scufl2.workflow-bundle")) { connection.addRequestProperty("Accept", "application/zip;q=0.5"); connection.addRequestProperty("Accept", "application/x-zip-compressed;q=0.5"); } else if (mediaType.endsWith("+xml")) { connection.setRequestProperty("Accept", "application/xml;q=0.6"); connection.setRequestProperty("Accept", "text/xml;q=0.5"); } }
Example 10
Source File: GitHubConnector.java From Slimefun4 with GNU General Public License v3.0 | 5 votes |
public void pullFile() { file = new File("plugins/Slimefun/cache/github/" + getFileName() + ".json"); if (github.isLoggingEnabled()) { Slimefun.getLogger().log(Level.INFO, "Retrieving {0}.json from GitHub...", getFileName()); } try { URL website = new URL("https://api.github.com/repos/" + repository + getURLSuffix()); URLConnection connection = website.openConnection(); connection.setConnectTimeout(8000); connection.addRequestProperty("Accept-Charset", "UTF-8"); connection.addRequestProperty("User-Agent", "Slimefun 4 GitHub Agent (by TheBusyBiscuit)"); connection.setDoOutput(true); try (ReadableByteChannel channel = Channels.newChannel(connection.getInputStream())) { try (FileOutputStream stream = new FileOutputStream(file)) { stream.getChannel().transferFrom(channel, 0, Long.MAX_VALUE); parseData(); } } } catch (IOException e) { if (github.isLoggingEnabled()) { Slimefun.getLogger().log(Level.WARNING, "Could not connect to GitHub in time."); } if (hasData()) { parseData(); } else { onFailure(); } } }
Example 11
Source File: JwksKeyWithFractionConfigTest.java From thorntail with Apache License 2.0 | 5 votes |
@Test @RunAsClient public void testThatJwksKeyIsVerified() throws Exception { final KeyTool keyTool = KeyTool.newKeyTool(getClass().getResource("/keys/pkcs8_good_key.pem").toURI()); final String jwt = new JwtTool(keyTool, "http://testsuite-jwt-issuer.io").generateSignedJwt(); final URL url = new URL("http://localhost:8080/testsuite/mpjwt/token"); final URLConnection urlConnection = url.openConnection(); urlConnection.addRequestProperty("Authorization", "Bearer " + jwt); try (InputStreamReader isr = new InputStreamReader(urlConnection.getInputStream()); BufferedReader br = new BufferedReader(isr)) { assertEquals(jwt, br.readLine()); } }
Example 12
Source File: StaticKeyWithFractionConfigTest.java From thorntail with Apache License 2.0 | 5 votes |
@Test @RunAsClient public void testThatStaticKeyIsVerified() throws Exception { final KeyTool keyTool = KeyTool.newKeyTool(getClass().getResource("/keys/pkcs8_good_key.pem").toURI()); final String jwt = new JwtTool(keyTool, "http://testsuite-jwt-issuer.io").generateSignedJwt(); final URL url = new URL("http://localhost:8080/testsuite/mpjwt/token"); final URLConnection urlConnection = url.openConnection(); urlConnection.addRequestProperty("Authorization", "Bearer " + jwt); try (InputStreamReader isr = new InputStreamReader(urlConnection.getInputStream()); BufferedReader br = new BufferedReader(isr)) { assertEquals(jwt, br.readLine()); } }
Example 13
Source File: SimpleUpdater.java From ChestCommands with GNU General Public License v3.0 | 5 votes |
private Object readJson(String url) throws MalformedURLException, IOException { URLConnection conn = new URL(url).openConnection(); conn.setConnectTimeout(5000); conn.setReadTimeout(8000); conn.addRequestProperty("User-Agent", "Updater (by filoghost)"); conn.setDoOutput(true); return JSONValue.parse(new BufferedReader(new InputStreamReader(conn.getInputStream()))); }
Example 14
Source File: UrlModuleSourceProvider.java From openbd-core with GNU General Public License v3.0 | 5 votes |
void applyConditionals(URLConnection urlConnection) { if(lastModified != 0L) { urlConnection.setIfModifiedSince(lastModified); } if(entityTags != null && entityTags.length() > 0) { urlConnection.addRequestProperty("If-None-Match", entityTags); } }
Example 15
Source File: TestTapeArchiveChartLoader.java From microbean-helm with Apache License 2.0 | 5 votes |
@Before public void setUp() throws IOException { // Chart is arbitrary, but it does have subcharts in it, which exercise some tricky logic final URI uri = URI.create("https://kubernetes-charts.storage.googleapis.com/wordpress-0.6.6.tgz"); assertNotNull(uri); final URL url = uri.toURL(); assertNotNull(url); final URLConnection connection = url.openConnection(); assertNotNull(connection); connection.addRequestProperty("Accept-Encoding", "gzip"); connection.connect(); assertEquals("application/x-tar", connection.getContentType()); this.stream = new TarInputStream(new BufferedInputStream(new GZIPInputStream(connection.getInputStream()))); }
Example 16
Source File: Ttc.java From VileBot with MIT License | 5 votes |
private String getContent() throws Exception { String content; URLConnection connection; connection = new URL( TTC_URL ).openConnection(); connection.addRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" ); Scanner scanner = new Scanner( connection.getInputStream() ); scanner.useDelimiter( "\\Z" ); content = scanner.next(); return content; }
Example 17
Source File: ZybezQuery.java From osrsclient with GNU General Public License v2.0 | 5 votes |
private String getJsonString() throws MalformedURLException, IOException { URL url = new URL(apiURL + itemRequested); URLConnection urlConnection = url.openConnection(); urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0"); BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); //Put JSON data into String message String message = org.apache.commons.io.IOUtils.toString(in); return message; }
Example 18
Source File: PluginUpdater.java From BedwarsRel with GNU General Public License v3.0 | 4 votes |
/** * Make a connection to the BukkitDev API and request the newest file's details. * * @return true if successful. */ private boolean read() { try { final URLConnection conn = this.url.openConnection(); conn.setConnectTimeout(5000); if (this.apiKey != null) { conn.addRequestProperty("X-API-Key", this.apiKey); } conn.addRequestProperty("User-Agent", PluginUpdater.USER_AGENT); conn.setDoOutput(true); final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); final String response = reader.readLine(); final JSONArray array = (JSONArray) JSONValue.parse(response); if (array.isEmpty()) { this.plugin.getLogger() .warning("The updater could not find any files for the project id " + this.id); this.result = UpdateResult.FAIL_BADID; return false; } JSONObject latestUpdate = (JSONObject) array.get(array.size() - 1); this.versionName = (String) latestUpdate.get(PluginUpdater.TITLE_VALUE); this.versionLink = (String) latestUpdate.get(PluginUpdater.LINK_VALUE); this.versionType = (String) latestUpdate.get(PluginUpdater.TYPE_VALUE); this.versionGameVersion = (String) latestUpdate.get(PluginUpdater.VERSION_VALUE); this.versionCustom = this.getCustomVersion(); return true; } catch (final IOException e) { if (e.getMessage().contains("HTTP response code: 403")) { this.plugin.getLogger() .severe("dev.bukkit.org rejected the API key provided in plugins/updater/config.yml"); this.plugin.getLogger() .severe("Please double-check your configuration to ensure it is correct."); this.result = UpdateResult.FAIL_APIKEY; } else { this.plugin.getLogger() .severe("The updater could not contact dev.bukkit.org for updating."); this.plugin.getLogger().severe( "If you have not recently modified your configuration and this is the first time you are seeing this message, the site may be experiencing temporary downtime."); this.result = UpdateResult.FAIL_DBO; } this.plugin.getLogger().log(Level.SEVERE, null, e); return false; } }
Example 19
Source File: URLConnectionTest.java From j2objc with Apache License 2.0 | 4 votes |
/** * This test checks whether connections are gzipped by default. This * behavior in not required by the API, so a failure of this test does not * imply a bug in the implementation. */ // public void testGzipEncodingEnabledByDefault() throws IOException, InterruptedException { // server.enqueue(new MockResponse() // .setBody(gzip("ABCABCABC".getBytes("UTF-8"))) // .addHeader("Content-Encoding: gzip")); // server.play(); // // URLConnection connection = server.getUrl("/").openConnection(); // assertEquals("ABCABCABC", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); // assertNull(connection.getContentEncoding()); // assertEquals(-1, connection.getContentLength()); // // RecordedRequest request = server.takeRequest(); // assertContains(request.getHeaders(), "Accept-Encoding: gzip"); // } // TODO(tball): b/28067294 // public void testClientConfiguredGzipContentEncoding() throws Exception { // byte[] bodyBytes = gzip("ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes("UTF-8")); // server.enqueue(new MockResponse() // .setBody(bodyBytes) // .addHeader("Content-Encoding: gzip") // .addHeader("Content-Length: " + bodyBytes.length)); // server.play(); // // URLConnection connection = server.getUrl("/").openConnection(); // connection.addRequestProperty("Accept-Encoding", "gzip"); // InputStream gunzippedIn = new GZIPInputStream(connection.getInputStream()); // assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZ", readAscii(gunzippedIn, Integer.MAX_VALUE)); // assertEquals(bodyBytes.length, connection.getContentLength()); // // RecordedRequest request = server.takeRequest(); // assertContains(request.getHeaders(), "Accept-Encoding: gzip"); // assertEquals("gzip", connection.getContentEncoding()); // } // TODO(tball): b/28067294 // public void testGzipAndConnectionReuseWithFixedLength() throws Exception { // testClientConfiguredGzipContentEncodingAndConnectionReuse(TransferKind.FIXED_LENGTH); // } // TODO(tball): b/28067294 // public void testGzipAndConnectionReuseWithChunkedEncoding() throws Exception { // testClientConfiguredGzipContentEncodingAndConnectionReuse(TransferKind.CHUNKED); // } public void testClientConfiguredCustomContentEncoding() throws Exception { server.enqueue(new MockResponse() .setBody("ABCDE") .addHeader("Content-Encoding: custom")); server.play(); URLConnection connection = server.getUrl("/").openConnection(); connection.addRequestProperty("Accept-Encoding", "custom"); assertEquals("ABCDE", readAscii(connection.getInputStream(), Integer.MAX_VALUE)); RecordedRequest request = server.takeRequest(); assertContains(request.getHeaders(), "Accept-Encoding: custom"); }
Example 20
Source File: EcoTouchConnector.java From openhab1-addons with Eclipse Public License 2.0 | 4 votes |
/** * Request a value from the heat pump- * * @param tag * The register to query (e.g. "A1") * @return value This value is a 16-bit integer. */ public int getValue(String tag) throws Exception { // request values String url = "http://" + ip + "/cgi/readTags?n=1&t1=" + tag; StringBuilder body = null; int loginAttempt = 0; while (loginAttempt < 2) { try { URLConnection connection = new URL(url).openConnection(); if (cookies != null) { for (String cookie : cookies) { connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]); } } InputStream response = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(response)); body = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { body.append(line + "\n"); } if (body.toString().contains("#" + tag)) { // succeeded break; } // s.th. went wrong; try to log in throw new Exception(); } catch (Exception e) { login(); loginAttempt++; } } if (body == null || !body.toString().contains("#" + tag)) { // failed logger.debug("Cannot get value for tag '" + tag + "' from Waterkotte EcoTouch."); throw new Exception("invalid response from EcoTouch"); } // ok, the body now contains s.th. like // #A30 S_OK // 192 223 Matcher m = response_pattern.matcher(body.toString()); boolean b = m.find(); if (!b) { // ill formatted response logger.debug("ill formatted response: '" + body + "'"); throw new Exception("invalid response from EcoTouch"); } return Integer.parseInt(m.group(3)); }