Android App uses HttpURLConnection to send requests and upload files

http development tools commonly used in Android native app development The system’s built-in http request tool is HttpURLConnection httpClient is an open source tool for apache okHttp is simpler to use, and its syntax is much simpler than HttpURLConnection. You need to add dependencies in graddle. This article mainly explains how to use HttpURConnection to […]

HttpURLConnection download file, byte array POST multipart/form-data

View POST multipart/form-data protocol format The downloaded file is stored in a byte array and POST multipart/form-data interface package com.http; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.Closeable; import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class HttpDownload { /** * * […]

HttpURLConnection skips ssl verification

When using HttpURLConnection to link https resources, the verification fails and the error is reported as follows: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1964) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:328) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:322) at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1614) at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216) at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1052) at sun.security.ssl.Handshaker.process_record(Handshaker.java:987) at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1072) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413) […]

java basics – using HttpURLConnection to encapsulate http tool classes

package org.example; import java.io.*; import java.net.*; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; public class HttpUtil {<!– –> //MIME partial file type comparison table private static final Map<String, String> FILE_TYPE = new HashMap<>(); static {<!– –> FILE_TYPE.put(“.jpeg”, “image/jpeg”); FILE_TYPE.put(“.jpg”, “image/jpg”); FILE_TYPE.put(“.png”, “image/png”); FILE_TYPE.put(“.bmp”, “image/bmp”); FILE_TYPE.put(“.gif”, “image/gif”); FILE_TYPE.put(“.mp4”, “video/mp4”); FILE_TYPE.put(“.txt”, “text/plain”); […]

Use HttpsURLConnection to load the certificate and send the package

package com.bree.proxy.utils; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.Authenticator; import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.PasswordAuthentication; import java.net.Socket; import java.net.URL; import java.security.KeyStore; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.HashMap; import java.util.Map; import java.util.regex.Pattern; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLEngine; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import […]

Java calls third-party api using HttpURLConnection in jdk

1. Through the JDK network class Java.net.HttpURLConnection 1. The http request provided by the native java api under the java.net package Steps to use: 1. Obtain the connector (java.net.URLConnection) through the Uniform Resource Locator (java.net.URL). package com.ruoyi.web.controller.test; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; /** * jdk calls the third-party interface * @author yzd */ public […]

POST request of HttpURLConnection

Content outline 1. Background 2. HttpURLConnection 3. POST protocol 3.1 HTTP message 3.1.1 Start line 3.1.2 Request header (header) 3.1.3 Request Body 3.2 POST of HTTP 4. Examples 4.1 Simple POST request 4.2 Upload files 1. Background I believe that everyone is familiar with the use of the HttpURLConnection class. Usually, we use this class […]

URLConnection (2)

Article directory 1. Cache 2. Java web cache 1. Cache Web browsers cache pages and images, cache resources locally, and reload them from the cache each time they are needed, instead of requesting a remote server every time. Some HTTP headers (including Expires and Cache-Control) can control the header. By default, it is generally accepted […]

URLConnection (3)

Article directory 1. Configure connection 2. protected URL url 3. protected boolean connected 4. protected boolean allowUserInteraction 5. protected boolean doInput 5. protected boolean doOutput 6. protected boolean isModifiedSince 7. protected boolean useCaches 8. Timeout 1. Configure connection The URLConnection class has seven protected instance fields that define how the client makes a request to […]

URLConnection (4)

Article directory 1. Configure the client request HTTP header 2. Write data to the server 3. Security considerations of URLConnection 4. HttpURLConnection 1. Configure client request HTTP header An HTTP client (such as a browser) sends a request line and a header to the server, as follows: Based on this information, the web server can […]