1.获取cookie
1.打开微博登录页面,浏览器F12进行抓包,登录。
2.在微博首页地址栏找到url
3.在浏览器调试页面找到Referer与地址栏相同的url
2.代码
1.加入依赖
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.cookie.CookieSpec;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.DefaultCookieSpec;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class cookieLogin {
private CloseableHttpClient client;
private HttpPost post;
private HttpGet get;
private BasicCookieStore cookieStore;
public cookieLogin() {
//cookie策略,不设置会拒绝cookie rejected,设置策略保存cookie信息
cookieStore = new BasicCookieStore();
CookieSpecProvider myCookie = new CookieSpecProvider() {
public CookieSpec create(HttpContext context) {
return new DefaultCookieSpec();
}
};
Registry<CookieSpecProvider> rg = RegistryBuilder.<CookieSpecProvider> create().register("myCookie", myCookie)
.build();
client = HttpClients.custom().setDefaultCookieStore(cookieStore).setDefaultCookieSpecRegistry(rg).build();
get = new HttpGet();
post = new HttpPost();
}
public void Login() throws ClientProtocolException, IOException, URISyntaxException {
String LoginUrl = "你的微博主页网址";
get.setURI(new URI(LoginUrl));
get.addHeader("Accept", "*/*");
get.addHeader("Accept-Encoding", "gzip, deflate, br");
get.addHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
get.addHeader("Cache-Control" , "no-cache");
get.addHeader("Connection" , "keep-alive");
get.addHeader("Content-Type" , "application/x-www-form-urlencoded");
get.addHeader(new BasicHeader("Cookie", "cookie"));
get.addHeader("Host", "weibo.com");
get.addHeader("Pragma", "no-cache");
get.addHeader("Referer", "你的微博主页网址");
get.addHeader("Sec-Fetch-Dest", "empty");
get.addHeader("Sec-Fetch-Mode", "cors");
get.addHeader("Sec-Fetch-Site", "same-origin");
get.addHeader("TE", "trailers");
get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0");
get.addHeader("X-Requested-With", "XMLHttpRequest");
HttpResponse resp = client.execute(get);
HttpEntity entity = resp.getEntity();
String cont = EntityUtils.toString(entity);
System.out.println("获取的微博内容:" + cont);
}
public CloseableHttpClient getClient() {
return client;
}
public void setClient(CloseableHttpClient client) {
this.client = client;
}
public HttpPost getPost() {
return post;
}
public void setPost(HttpPost post) {
this.post = post;
}
public HttpGet getGet() {
return get;
}
public void setGet(HttpGet get) {
this.get = get;
}
public BasicCookieStore getCookieStore() {
return cookieStore;
}
public void setCookieStore(BasicCookieStore cookieStore) {
this.cookieStore = cookieStore;
}
public static void main(String[] args) throws ClientProtocolException, IOException, URISyntaxException {
new cookieLogin().Login();
}
}
测试
参考;https://www.cnblogs.com/qianzf/p/7747899.html
标签:java,get,http,addHeader,微博,cookie,import,apache,org From: https://www.cnblogs.com/klearning/p/17024326.html