Skip to content

Instantly share code, notes, and snippets.

@zhong-j-yu
Last active October 9, 2023 02:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhong-j-yu/22af353e2c5a5aed5857 to your computer and use it in GitHub Desktop.
Save zhong-j-yu/22af353e2c5a5aed5857 to your computer and use it in GitHub Desktop.
package bayou;
import bayou.http.HttpClient;
import bayou.http.HttpClientConf;
import bayou.http.HttpResponse;
import bayou.ssl.SslConf;
import javax.imageio.ImageIO;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLParameters;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
public class ClientHttpsExample
{
public static void main(String[] args) throws Exception
{
HttpClient client = new HttpClientConf()
.sslContext(new SslConf().trustAll().createContext()) // trust self-signed certs
.sslEngineConf(engine -> disableHostNameVerification(engine))
.trafficDump(System.out::print)
.newClient();
// typically, app creates one client and use it for all requests
String url = "https://ns6.host.md:8443/sitepreview/http/zugo.md/media/images/thumb/23812__yu400x250.jpg";
HttpResponse response = client.doGet(url).sync();
ByteBuffer bb = response.bodyBytes(Integer.MAX_VALUE).sync();
InputStream is = new ByteArrayInputStream(bb.array(), bb.arrayOffset()+bb.position(), bb.remaining());
BufferedImage image = ImageIO.read(is);
}
static void disableHostNameVerification(SSLEngine engine)
{
SSLParameters sslParameters = engine.getSSLParameters();
{
// by default, it's set to "HTTPS", and the server certificate must match the request host.
// disable it for this example, since the server certificate is ill constructed.
sslParameters.setEndpointIdentificationAlgorithm(null);
}
engine.setSSLParameters(sslParameters);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment