Search in sources :

Example 56 with HttpsURLConnection

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

the class URLConnectionTest method testProxyAuthenticateOnConnect.

public void testProxyAuthenticateOnConnect() throws Exception {
    Authenticator.setDefault(new SimpleAuthenticator());
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
    server.enqueue(new MockResponse().setResponseCode(407).addHeader("Proxy-Authenticate: Basic realm=\"localhost\""));
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
    server.enqueue(new MockResponse().setBody("A"));
    server.play();
    URL url = new URL("https://android.com/foo");
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(server.toProxyAddress());
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    connection.setHostnameVerifier(new RecordingHostnameVerifier());
    assertContent("A", connection);
    RecordedRequest connect1 = server.takeRequest();
    assertEquals("CONNECT android.com:443 HTTP/1.1", connect1.getRequestLine());
    assertContainsNoneMatching(connect1.getHeaders(), "Proxy\\-Authorization.*");
    RecordedRequest connect2 = server.takeRequest();
    assertEquals("CONNECT android.com:443 HTTP/1.1", connect2.getRequestLine());
    assertContains(connect2.getHeaders(), "Proxy-Authorization: Basic " + SimpleAuthenticator.BASE_64_CREDENTIALS);
    RecordedRequest get = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
    assertContainsNoneMatching(get.getHeaders(), "Proxy\\-Authorization.*");
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 57 with HttpsURLConnection

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

the class URLConnectionTest method testConnectViaHttps.

public void testConnectViaHttps() throws IOException, InterruptedException {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
    server.play();
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    assertContent("this response comes via HTTPS", connection);
    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
    assertEquals("TLSv1", request.getSslProtocol());
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 58 with HttpsURLConnection

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

the class JdkHttpClient method post.

@Override
public String post(String url, int timeout, String userAgent, String content, String postContentType, boolean gzip) throws IOException {
    String result = null;
    canceled = false;
    final URL u = new URL(url);
    final HttpURLConnection conn = (HttpURLConnection) u.openConnection();
    conn.setDoOutput(true);
    conn.setConnectTimeout(timeout);
    conn.setReadTimeout(timeout);
    conn.setRequestProperty("User-Agent", userAgent);
    conn.setInstanceFollowRedirects(false);
    if (conn instanceof HttpsURLConnection) {
        setHostnameVerifier((HttpsURLConnection) conn);
    }
    byte[] data = content.getBytes("UTF-8");
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", postContentType);
    conn.setRequestProperty("charset", "utf-8");
    conn.setUseCaches(false);
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    try {
        OutputStream out;
        if (gzip) {
            out = new GZIPOutputStream(conn.getOutputStream());
        } else {
            out = conn.getOutputStream();
        }
        byte[] b = new byte[4096];
        int n;
        while (!canceled && (n = in.read(b, 0, b.length)) != -1) {
            if (!canceled) {
                out.write(b, 0, n);
                out.flush();
                onData(b, 0, n);
            }
        }
        closeQuietly(out);
        conn.connect();
        int httpResponseCode = getResponseCode(conn);
        if (httpResponseCode != HttpURLConnection.HTTP_OK && httpResponseCode != HttpURLConnection.HTTP_PARTIAL) {
            throw new ResponseCodeNotSupportedException(httpResponseCode);
        }
        if (canceled) {
            onCancel();
        } else {
            BufferedInputStream bis = new BufferedInputStream(conn.getInputStream(), 4096);
            ByteArrayBuffer baf = new ByteArrayBuffer(1024);
            byte[] buffer = new byte[64];
            int read;
            while (true) {
                read = bis.read(buffer);
                if (read == -1) {
                    break;
                }
                baf.append(buffer, 0, read);
            }
            result = new String(baf.toByteArray());
            onComplete();
        }
    } catch (Exception e) {
        onError(e);
    } finally {
        closeQuietly(in);
        closeQuietly(conn);
    }
    return result;
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) GZIPOutputStream(java.util.zip.GZIPOutputStream) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 59 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project cas by apereo.

the class CasCoreTicketsConfiguration method casClientTicketValidator.

@ConditionalOnMissingBean(name = "casClientTicketValidator")
@Bean
public AbstractUrlBasedTicketValidator casClientTicketValidator() {
    final String prefix = StringUtils.defaultString(casProperties.getClient().getPrefix(), casProperties.getServer().getPrefix());
    final Cas30ServiceTicketValidator validator = new Cas30ServiceTicketValidator(prefix);
    final HttpURLConnectionFactory factory = new HttpURLConnectionFactory() {

        private static final long serialVersionUID = 3692658214483917813L;

        @Override
        public HttpURLConnection buildHttpURLConnection(final URLConnection conn) {
            if (conn instanceof HttpsURLConnection) {
                final HttpsURLConnection httpsConnection = (HttpsURLConnection) conn;
                httpsConnection.setSSLSocketFactory(sslContext.getSocketFactory());
                httpsConnection.setHostnameVerifier(hostnameVerifier);
            }
            return (HttpURLConnection) conn;
        }
    };
    validator.setURLConnectionFactory(factory);
    return validator;
}
Also used : Cas30ServiceTicketValidator(org.jasig.cas.client.validation.Cas30ServiceTicketValidator) HttpURLConnection(java.net.HttpURLConnection) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HttpURLConnectionFactory(org.jasig.cas.client.ssl.HttpURLConnectionFactory) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 60 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project MyMaid2 by jaoafa.

the class MyMaid2Premise method postHttpJsonByJson.

@SuppressWarnings("unchecked")
private static boolean postHttpJsonByJson(String address, Map<String, String> headers, Map<String, String> contents) {
    StringBuilder builder = new StringBuilder();
    try {
        URL url = new URL(address);
        HttpsURLConnection connect = (HttpsURLConnection) url.openConnection();
        connect.setRequestMethod("POST");
        if (headers != null) {
            for (Map.Entry<String, String> header : headers.entrySet()) {
                connect.setRequestProperty(header.getKey(), header.getValue());
            }
        }
        connect.setDoOutput(true);
        OutputStreamWriter out = new OutputStreamWriter(connect.getOutputStream());
        JSONObject paramobj = new JSONObject();
        for (Map.Entry<String, String> content : contents.entrySet()) {
            paramobj.put(content.getKey(), content.getValue());
        }
        out.write(paramobj.toJSONString());
        out.close();
        connect.connect();
        if (connect.getResponseCode() != HttpURLConnection.HTTP_OK) {
            InputStream in = connect.getErrorStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            in.close();
            connect.disconnect();
            Bukkit.getLogger().warning("DiscordWARN: " + builder.toString());
            return false;
        }
        InputStream in = connect.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
        in.close();
        connect.disconnect();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
Also used : JSONObject(org.json.simple.JSONObject) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) HashMap(java.util.HashMap) Map(java.util.Map) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) IOException(java.io.IOException)

Aggregations

HttpsURLConnection (javax.net.ssl.HttpsURLConnection)522 URL (java.net.URL)310 IOException (java.io.IOException)177 HttpURLConnection (java.net.HttpURLConnection)128 InputStreamReader (java.io.InputStreamReader)93 InputStream (java.io.InputStream)89 Test (org.junit.Test)83 BufferedReader (java.io.BufferedReader)78 SSLContext (javax.net.ssl.SSLContext)70 OutputStream (java.io.OutputStream)54 HostnameVerifier (javax.net.ssl.HostnameVerifier)50 MalformedURLException (java.net.MalformedURLException)48 URLConnection (java.net.URLConnection)47 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)46 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)37 HashMap (java.util.HashMap)34 DataOutputStream (java.io.DataOutputStream)32 KeyManagementException (java.security.KeyManagementException)32 JSONObject (org.json.JSONObject)29