Search in sources :

Example 16 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project jfinal by jfinal.

the class HttpKit method getHttpConnection.

private static HttpURLConnection getHttpConnection(String url, String method, Map<String, String> headers) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
    URL _url = new URL(url);
    HttpURLConnection conn = (HttpURLConnection) _url.openConnection();
    if (conn instanceof HttpsURLConnection) {
        ((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory);
        ((HttpsURLConnection) conn).setHostnameVerifier(trustAnyHostnameVerifier);
    }
    conn.setRequestMethod(method);
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setConnectTimeout(19000);
    conn.setReadTimeout(19000);
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36");
    if (headers != null && !headers.isEmpty())
        for (Entry<String, String> entry : headers.entrySet()) conn.setRequestProperty(entry.getKey(), entry.getValue());
    return conn;
}
Also used : Entry(java.util.Map.Entry) HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 17 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project Honu by jboulon.

the class THttpClient method flush.

public void flush() throws TTransportException {
    // Extract request and reset buffer
    byte[] data = requestBuffer_.toByteArray();
    requestBuffer_.reset();
    try {
        // Create connection object
        HttpURLConnection connection = (HttpURLConnection) url_.openConnection();
        if (turnOffSSLHostnameVerifier && connection instanceof HttpsURLConnection) {
            ((HttpsURLConnection) connection).setHostnameVerifier(alwaysTrueHostnameVerifier);
        }
        // Timeouts, only if explicitly set
        if (connectTimeout_ > 0) {
            connection.setConnectTimeout(connectTimeout_);
        }
        if (readTimeout_ > 0) {
            connection.setReadTimeout(readTimeout_);
        }
        // Make the request
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-thrift");
        connection.setRequestProperty("Accept", "application/x-thrift");
        connection.setRequestProperty("User-Agent", "Java/THttpClient");
        if (customHeaders_ != null) {
            for (Map.Entry<String, String> header : customHeaders_.entrySet()) {
                connection.setRequestProperty(header.getKey(), header.getValue());
            }
        }
        connection.setDoOutput(true);
        connection.connect();
        connection.getOutputStream().write(data);
        int responseCode = connection.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK) {
            throw new TTransportException("HTTP Response code: " + responseCode);
        }
        // Read the responses
        inputStream_ = connection.getInputStream();
    } catch (IOException iox) {
        throw new TTransportException(iox);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 18 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project jersey by jersey.

the class SslHttpUrlConnectorTest method testSSLWithCustomSocketFactory.

/**
     * Test to see that the correct Http status is returned.
     *
     * @throws Exception in case of a test failure.
     */
@Test
public void testSSLWithCustomSocketFactory() throws Exception {
    final SSLContext sslContext = getSslContext();
    final CustomSSLSocketFactory socketFactory = new CustomSSLSocketFactory(sslContext);
    final ClientConfig cc = new ClientConfig().connectorProvider(new HttpUrlConnectorProvider().connectionFactory(new HttpUrlConnectorProvider.ConnectionFactory() {

        @Override
        public HttpURLConnection getConnection(final URL url) throws IOException {
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            connection.setSSLSocketFactory(socketFactory);
            return connection;
        }
    }));
    final Client client = ClientBuilder.newBuilder().withConfig(cc).sslContext(sslContext).register(HttpAuthenticationFeature.basic("user", "password")).register(LoggingFeature.class).build();
    final Response response = client.target(Server.BASE_URI).path("/").request().get();
    assertEquals(200, response.getStatus());
    assertTrue(socketFactory.isVisited());
}
Also used : Response(javax.ws.rs.core.Response) HttpUrlConnectorProvider(org.glassfish.jersey.client.HttpUrlConnectorProvider) LoggingFeature(org.glassfish.jersey.logging.LoggingFeature) SSLContext(javax.net.ssl.SSLContext) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 19 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project Smack by igniterealtime.

the class AbstractSmackIntTest method getHttpUrlConnectionFor.

protected HttpURLConnection getHttpUrlConnectionFor(URL url) throws IOException {
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    if (sinttestConfiguration.tlsContext != null && urlConnection instanceof HttpsURLConnection) {
        HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) urlConnection;
        httpsUrlConnection.setSSLSocketFactory(sinttestConfiguration.tlsContext.getSocketFactory());
    }
    return urlConnection;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 20 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project Talon-for-Twitter by klinker24.

the class FavoriterUtils method getJson.

public JSONObject getJson(long tweetId) {
    try {
        String url = "https://twitter.com/i/activity/favorited_popup?id=" + tweetId;
        URL obj = new URL(url);
        HttpsURLConnection connection = (HttpsURLConnection) obj.openConnection();
        connection.setRequestProperty("Content-Type", "text/html");
        connection.setRequestProperty("charset", "utf-8");
        connection.setRequestMethod("GET");
        connection.setRequestProperty("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36");
        connection.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        String docHtml = sb.toString();
        try {
            connection.disconnect();
        } catch (Exception e) {
        }
        return new JSONObject(docHtml);
    } catch (Exception e) {
        return null;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.JSONObject) BufferedReader(java.io.BufferedReader) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) TwitterException(twitter4j.TwitterException)

Aggregations

HttpsURLConnection (javax.net.ssl.HttpsURLConnection)209 URL (java.net.URL)113 HttpURLConnection (java.net.HttpURLConnection)51 IOException (java.io.IOException)50 Test (org.junit.Test)39 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)29 InputStream (java.io.InputStream)27 HostnameVerifier (javax.net.ssl.HostnameVerifier)23 SSLContext (javax.net.ssl.SSLContext)23 OutputStream (java.io.OutputStream)20 MockResponse (com.google.mockwebserver.MockResponse)19 InputStreamReader (java.io.InputStreamReader)19 TestSSLContext (libcore.javax.net.ssl.TestSSLContext)19 URLConnection (java.net.URLConnection)17 BufferedReader (java.io.BufferedReader)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 MalformedURLException (java.net.MalformedURLException)13 Proxy (java.net.Proxy)13 MockResponse (okhttp3.mockwebserver.MockResponse)13