1. 首先在浏览器访问登录相关网站,获取到登录后的cookie,例:
2. 通过postman验证cookie能否实现网站鉴权
正常不传cookie的话会出现401报错,提示需要登录才能访问接口
也获取不到cookie数据:
传cookie后,可得到接口数据:
可以在cookie记录中看到网站的cookie值:
注意:传参名称为Cookie,值格式为cookie名称=cookie值,来着这里
3. 代码实现
3.1 引入依赖包
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
</dependency>
3.2 解决访问https问题
SSLClient.java
package com.cnki.base.utils;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
public class SSLClient extends DefaultHttpClient {
public SSLClient() throws Exception{
super();
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
}
}
3.3 解决cookie传参问题
public static String doGet(String url,String charset) throws Exception{
HttpClient httpClient = null;
HttpGet httpGet = null;
String result = null;
//解决https访问
httpClient = new SSLClient();
httpGet = new HttpGet(url);
Map<String, String> headMap = new HashMap<>();
headMap.put("Content-Type", "application/json");
//解决cookie传参
String cookies = "symphony=b1d0d1ad98acdd5d7d846d4359048a923f932962125e7ad0c9db814d5942879467b648f693d6a11336bcaa8b1bee42090ae1061a025b6f16d8afdc6baac0c1129f62ce384f54ec3360273c1f53642ade6c02274317f2a0d3b8e8cc595d6c53c192ffd19a4884886efab2e7464b9ab71d3dc5d20ead8376e85f58003ad4970b25b6a66e42979b7d00fe0e025216f0aa80022eab08fca5700a7d78b0c891c3abbf8e2c8d1cef2fa81ae3991e267f9b3a8ab3c458077fe72cd7fc7403ef494f25dae5a7dab90ccae4ad4dad276fc0f0dd74c62516f9362c58fc1898491e39daca143340f8f66694422f58a3ab477aefb8ae";
httpGet.addHeader(new BasicHeader("Cookie", cookies));
//配置自定义UA
httpGet.setHeader("User-Agent","Mozilla/5.0(Windows NT 6.1;Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
setGetHead(httpGet, headMap);
HttpResponse response = httpClient.execute(httpGet);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,charset);
}
}
return result;
}
更多内容请访问:IT源点
注意:本文归作者所有,未经作者允许,不得转载