java HttpClient模拟登陆一直401
本帖最后由 猫南北爱上狗东西 于 2019-12-19 22:56 编辑java HttpClient模拟登陆一直401
用OKHttp
package com.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.car.util.DownloadImg;
import com.car.util.GetImgSrc;
import com.car.util.HTMLParseUtil;
import com.car.util.JsoupUtil;
import com.car.util.StringUtils;
import lombok.Data;
import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.json.XML;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;
public class HTTPUtils {
private static RequestConfig config;
public HTTPUtils(){
config = RequestConfig.custom()
.setConnectionRequestTimeout(3000)
.setConnectTimeout(3000)
.setSocketTimeout(3000)
.build();
}
private static final String request_url = "https://www.baidu.com/";
public static void main(String[] args) throws Exception{
Response response = new HTTPUtils().get(request_url,"");
System.out.println(response.responseBody);
}
public HTTPUtils(int connectionRequestTimeout, int connectTimeout, int socketTimeout){
config = RequestConfig.custom()
.setConnectionRequestTimeout(connectionRequestTimeout)
.setConnectTimeout(connectTimeout)
.setSocketTimeout(socketTimeout)
.build();
}
public Response post(String url, String header, String requestBody) throws IOException {
CloseableHttpClient httpclient = buildSSLCloseableHttpClient(url);
HttpPost httppost = new HttpPost(url);
httppost.setConfig(config);
if (header != null && !header.equals("")) {
for (Map.Entry<String, String> entry : getRequestHeader(header).entrySet()) {
httppost.setHeader(entry.getKey(), entry.getValue());
}
}
httppost.setEntity(new StringEntity(requestBody));
CloseableHttpResponse response = httpclient.execute(httppost);
return getResponse(response);
}
public Response get(String url, String header) throws IOException {
CloseableHttpClient httpclient = buildSSLCloseableHttpClient(url);
HttpGet httpget = new HttpGet(url);
httpget.setConfig(config);
if (header != null && !header.equals("")) {
for (Map.Entry<String, String> entry : getRequestHeader(header).entrySet()) {
httpget.setHeader(entry.getKey(), entry.getValue());
}
}
CloseableHttpResponse response = httpclient.execute(httpget);
return getResponse(response);
}
private Map<String, String> getRequestHeader(String header){
Map<String, String> headerMap = new HashMap<String, String>();
JSONArray headerArray = JSONArray.parseArray(header);
for (int i=0; i<headerArray.size(); i++){
JSONObject headerObject = headerArray.getJSONObject(i);
for (String key : headerObject.keySet()){
headerMap.put(key, headerObject.getString(key));
}
}
return headerMap;
}
private Map<String, String> getResponseHeader(Header[] headers){
Map<String, String> headerMap = new HashMap<String, String>();
for (Header header : headers) {
headerMap.put(header.getName(), header.getValue());
}
return headerMap;
}
private CloseableHttpClient buildSSLCloseableHttpClient(String url){
SSLContext sslContext = null;
try {
sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) {
return true;
}
}).build();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
}
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslContext, new String[] { "TLSv1" }, null,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
return url.startsWith("https:") ? HttpClients.custom().setSSLSocketFactory(sslsf).build() : HttpClients.createDefault();
}
private Response getResponse(CloseableHttpResponse response){
Response res = null;
try {
String result = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
res = new Response();
res.setResponseCode(response.getStatusLine().getStatusCode());
res.setResponseHeader(getResponseHeader(response.getAllHeaders()));
res.setResponseBody(result);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return res;
}
/**
* json to xml
* @Param json String
* @return
*/
public String json2xml(String json) {
org.json.JSONObject jsonObj = new org.json.JSONObject(json);
return "<xml>" + XML.toString(jsonObj) + "</xml>";
}
/**
* xml to json
* @param xml String
* @return
*/
public String xml2json(String xml) {
org.json.JSONObject xmlJSONObj = XML.toJSONObject(xml.replace("<xml>", "").replace("</xml>", ""));
return xmlJSONObj.toString();
}
@Data
public class Response{
public int responseCode;
public Map<String, String> responseHeader;
public Object responseBody;
}
}
试试这个 有点意思 反正我也看不懂,点赞就行了{:1_921:} 401是没权限,那你账号密码输入对了吗?能返回说明能通 可能因为是访问HTTPS,createSSLClientDefault的实例需要设置对于证书的信任,可以设置为全通过
homurachyan 发表于 2019-12-20 08:55
可能因为是访问HTTPS,createSSLClientDefault的实例需要设置对于证书的信任,可以设置为全通过
已经设置了 smdzj 发表于 2019-12-20 08:44
401是没权限,那你账号密码输入对了吗?能返回说明能通
账号密码是对的 wl18122311198 发表于 2019-12-20 00:04
package com.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba ...
这个参数怎么传啊
页:
[1]