2种方式
方式一:Http请求头上添加Basic Authentication认证
// httpPost
httpPost.addHeader(“Authorization”, "Basic " + Base64.encodeBase64String(“username:password”.getBytes()));
// httpConnection
httpConn.setRequestProperty(“Authorization”, "Basic " + Base64.encodeBase64String(“username:password”.getBytes()));
方式二:使用Authenticator重写getPasswordAuthentication()–无测试过
相关jar包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.Strin