gpt4 book ai didi

java - 使用 Java org.apache.http.client 获取重定向 url

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:58:49 25 4
gpt4 key购买 nike

我需要帮助来弄清楚在我向服务器发帖后如何获得重定向。首先,我需要执行 get 从服务器获取一些 cookie。然后我使用 cookie 和其他参数执行一个帖子。然后服务器以 302 重定向响应。我如何获取该重定向的网址?

代码如下:

HttpGet get = new HttpGet(urlOne);

try {
//Creating a local instance of cookie store.
CookieStore cookieJar = new BasicCookieStore();

// Creating a local HTTP context
HttpContext localContext = new BasicHttpContext();

// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieJar);

HttpResponse response = httpClient.execute(get, localContext);
HttpEntity entity = response.getEntity();

System.out.println("------------------GET----------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}

// Print out cookies obtained from server
List<Cookie> cookies = cookieJar.getCookies();
for (int i = 0; i < cookies.size(); i++) {
System.out.println("Local cookie: " + cookies.get(i));
}

if (entity != null) {
entity.consumeContent();
}
System.out.println("------------------GET-END---------------------");

// Create a new post
HttpPost post = new HttpPost(urlTwo);
post.setHeader("Content-Type", "application/x-www-form-urlencoded");

// Add params
HttpParams params = new BasicHttpParams();
params.setParameter("action", "search");
params.setParameter("word", "hello");

post.setParams(params);

//Execute
HttpResponse response2 = httpClient.execute(post, localContext);

最佳答案

看我对这个问题的回答,

HttpClient 4 - how to capture last redirect URL

关于java - 使用 Java org.apache.http.client 获取重定向 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2033577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com