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.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import sun.misc.BASE64Encoder;
import java.io.IOException;
/**
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)