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.*");
}
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());
}
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;
}
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;
}
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;
}
}
Aggregations