Java 8 HttpURLConnection - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 HttpURLConnection class.

This code example shows how to use the following methods: setRequestMethod, setRequestProperty, setSSLSocketFactory
      SSLContext context = SSLContext.getInstance("TLS");
      context.init(null, trustManagerFactory.getTrustManagers(), null);
 
      URL url = new URL(String.format("https://gcm.textsecure.whispersystems.org%s", urlFragment));
      HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
      connection.setSSLSocketFactory(context.getSocketFactory());
      connection.setRequestMethod(method);
      connection.setRequestProperty("Content-Type", "application/json");
 
      if (password != null) {
        System.out.println("Adding authorization header: " + getAuthorizationHeader());
        connection.setRequestProperty("Authorization", getAuthorizationHeader());
      } 
 
      return connection;
    } catch (NoSuchAlgorithmException e) {
      throw new AssertionError(e);
    } catch (KeyManagementException e) {
      throw new AssertionError(e);
    } catch (MalformedURLException e) {
Full Snippet Info
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods: disconnect, getResponseCode, setChunkedStreamingMode, setRequestMethod, getOutputStream
This code example shows how to use the following methods: disconnect, getResponseCode, connect, setRequestProperty
    URLConnection conn = url.openConnection();
    if (!(conn instanceof HttpURLConnection)) {
      throw new IOException();
    } 
    HttpURLConnection connection = (HttpURLConnection) conn;
    connection.setRequestProperty("Accept", contentTypes);
    connection.setRequestProperty("Accept-Charset", "utf-8,*");
    connection.setRequestProperty("User-Agent", "ZXing (Android)");
    try { 
      try { 
        connection.connect();
      } catch (NullPointerException npe) {
        // this is an Android bug: http://code.google.com/p/android/issues/detail?id=16895 
        Log.w(TAG, "Bad URI? " + uri);
        throw new IOException(npe);
      } catch (IllegalArgumentException iae) {
        // Also seen this in the wild, not sure what to make of it. Probably a bad URL 
        Log.w(TAG, "Bad URI? " + uri);
        throw new IOException(iae);
      } 

10
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods: getResponseCode, connect
            getAuthenticationHandlerConfiguration(true)); 
    auth.start();
    try { 
      URL url = new URL(auth.getBaseURL());
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.connect();
      Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
    } finally { 
      auth.stop();
    } 
  } 
 
  @Test 
  public void testAnonymousDisallowed() throws Exception { 
    AuthenticatorTestCase auth = new AuthenticatorTestCase();
    auth.setAuthenticationHandlerConfig(
            getAuthenticationHandlerConfiguration(false)); 
    auth.start();
    try { 
      URL url = new URL(auth.getBaseURL());
Full Snippet Info
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods: getResponseCode
     */ 
    @Override 
    public String toString() {
        String responseCode;
        try { 
            responseCode = String.format("%d", (connection != null) ? connection.getResponseCode() : 200);
        } catch (IOException e) {
            responseCode = "unknown";
        } 
 
        return new StringBuilder().append("{Response: ").append(" responseCode: ").append(responseCode)
                .append(", graphObject: ").append(graphObject).append(", error: ").append(error)
                .append(", isFromCache:").append(isFromCache).append("}")
                .toString();
    } 
 
    /** 
     * Indicates whether the response was retrieved from a local cache or from the server. 
     * 
     * @return true if the response was cached locally, false if it was retrieved from the server 
Full Snippet Info
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods: setDoInput
        final URL url = new URL(spec); 
        Callable<String> task = new Callable<String>() { 
            @Override 
            public String call() throws Exception { 
                final HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
                conn.setDoInput(true); 
                return processResponse(conn); 
            } 
        }; 
        return execute(task, timeout, unit); 
    } 
 
    /** 
     * Returns the URL response as a string. 
     * 
     * @param spec  URL spec 
     * @param waitUntilAvailableMs  maximum timeout in milliseconds to wait for the URL to return non 404 response 
     * @param responseTimeout  the timeout to read the response 
     * @param responseTimeoutUnit  the time unit for responseTimeout 
     * @return  URL response 
Full Snippet Info
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods: setRequestMethod, setDoInput, setDoOutput, setRequestProperty, setUseCaches
		} 
		return new Request.Builder().source("com.apple.maps").unknown3(0).unknown4(0).wifis(wifis).build(); 
	} 
 
	private static void prepareConnection(HttpsURLConnection connection, int length) throws ProtocolException {
		connection.setRequestMethod("POST");
		connection.setDoInput(true);
		connection.setDoOutput(true);
		connection.setUseCaches(false);
		connection.setRequestProperty(HTTP_FIELD_CONTENT_TYPE, CONTENT_TYPE_URLENCODED);
		connection.setRequestProperty(HTTP_FIELD_CONTENT_LENGTH, String.valueOf(length));
	} 
 
	public Response retrieveLocations(String... macs) throws IOException {
		Request request = createRequest(macs);
		byte[] byteb = request.toByteArray();
		byte[] bytes = combineBytes(APPLE_MAGIC_BYTES, byteb, (byte) byteb.length);
		HttpsURLConnection connection = createConnection();
		prepareConnection(connection, bytes.length);
		OutputStream out = connection.getOutputStream();
Full Snippet Info
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods: getResponseCode, getInputStream
     * the request fails. 
     */ 
    public List entries;
 
    public ListAllMyBucketsResponse(HttpURLConnection connection) throws IOException {
        super(connection);
        if (connection.getResponseCode() < 400) {
            try { 
                XMLReader xr = Utils.createXMLReader();
                ListAllMyBucketsHandler handler = new ListAllMyBucketsHandler();
                xr.setContentHandler(handler);
                xr.setErrorHandler(handler);
 
                xr.parse(new InputSource(connection.getInputStream()));
                entries = handler.getEntries();
            } catch (SAXException e) {
                throw new RuntimeException("Unexpected error parsing ListAllMyBuckets xml", e);
            } 
        } 
    } 
Full Snippet Info
7
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods: getResponseCode, getResponseMessage, setAllowUserInteraction, setConnectTimeout, setReadTimeout
    boolean success = false;
    try { 
      Log.info("Job end notification trying " + urlToNotify);
      HttpURLConnection conn =
        (HttpURLConnection) urlToNotify.openConnection(proxyToUse);
      conn.setConnectTimeout(timeout);
      conn.setReadTimeout(timeout);
      conn.setAllowUserInteraction(false);
      if(conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
        Log.warn("Job end notification to " + urlToNotify +" failed with code: "
        + conn.getResponseCode() + " and message \"" + conn.getResponseMessage()
        +"\""); 
      } 
      else { 
        success = true;
        Log.info("Job end notification to " + urlToNotify + " succeeded");
      } 
    } catch(IOException ioe) {
      Log.warn("Job end notification to " + urlToNotify + " failed", ioe);
    } 
Full Snippet Info
8
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods: setRequestMethod, getOutputStream, setDoOutput, setRequestProperty
    } 
 
    public static URLConnection httpPost(WebServer webServer, String path, String body) throws IOException {
        URL url = new URL(webServer.getUri().toURL(), path);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        urlConnection.setDoOutput(true);
        urlConnection.getOutputStream().write(body.getBytes(Charset.forName("UTF8")));
        return urlConnection;
    } 
 
    public static URLConnection httpPostCompressed(WebServer webServer, String path, String body) throws IOException {
        URL url = new URL(webServer.getUri().toURL(), path);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.addRequestProperty("Content-Encoding", "gzip");
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        urlConnection.setDoOutput(true);
 
Full Snippet Info
8
CodeRank
Experience pair programming with AI  Get Codota for Java

Related Java 8 HttpURLConnection Questions & Answers:

Other Java 8 HttpURLConnection Examples

92:	            CommonUtilities.displayMessage(context, message);
93:	        } catch (IOException e) {
94:	            // At this point the device is unregistered from GCM, but still
95:	            // registered in the server.
96:	            // We could try to unregister again, but it is not necessary:
-
95:	            // registered in the server.
96:	            // We could try to unregister again, but it is not necessary:
97:	            // if the server tries to send a message to the device, it will get
98:	            // a "NotRegistered" error message and should unregister the device.
99:	            String message = context.getString(R.string.server_unregister_error,

Read More

66:	            Bitmap bitmap=null;
67:	            URL imageUrl = new URL(url);
68:	            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
69:	            conn.setConnectTimeout(30000);
70:	            conn.setReadTimeout(30000);
-
66:	            Bitmap bitmap=null;
67:	            URL imageUrl = new URL(url);
68:	            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
69:	            conn.setConnectTimeout(30000);
70:	            conn.setReadTimeout(30000);

Read More

65:	            URL imageUrl = new URL(url);
66:	            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
67:	            conn.setConnectTimeout(30000);
68:	            conn.setReadTimeout(30000);
69:	            conn.setInstanceFollowRedirects(true);
-
65:	            URL imageUrl = new URL(url);
66:	            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
67:	            conn.setConnectTimeout(30000);
68:	            conn.setReadTimeout(30000);
69:	            conn.setInstanceFollowRedirects(true);

Read More

See Code Examples for Java 8 HttpURLConnection Methods: