为什么HttpClient post请求返回的header中只有一个Set-Cookie,而用浏览器中请求返回的header中Set-Cookie有多个
时间: 2024-05-22 16:15:55 浏览: 95
这可能是因为浏览器在发送请求时会自动添加一些额外的头信息,例如User-Agent、Referer、Accept等,这些头信息可能会影响服务器返回的响应头信息。
另外,HttpClient发送的请求可以通过设置请求头信息来模拟浏览器发送的请求,例如设置User-Agent、Referer等头信息,这样可能会使服务器返回更多的Set-Cookie头信息。
相关问题
HttpClient发送Post请求并携带cookie
以下是一个使用HttpClient发送Post请求并携带cookie的示例代码:
```java
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.util.ArrayList;
import java.util.List;
public class HttpClientUtil {
public static void main(String[] args) throws Exception {
// 创建HttpClient实例
HttpClient httpClient = HttpClientBuilder.create().build();
// 创建CookieStore实例
CookieStore cookieStore = new BasicCookieStore();
// 创建HttpPost实例
HttpPost httpPost = new HttpPost("http://example.com/login");
// 设置请求参数
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000).setConnectionRequestTimeout(5000)
.setSocketTimeout(5000).build();
httpPost.setConfig(requestConfig);
// 设置请求头
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
// 设置请求体参数
List<BasicNameValuePair> parameters = new ArrayList<>();
parameters.add(new BasicNameValuePair("username", "example"));
parameters.add(new BasicNameValuePair("password", "password"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
httpPost.setEntity(formEntity);
// 执行HttpPost请求
HttpResponse httpResponse = httpClient.execute(httpPost);
// 获取响应实体
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
String response = EntityUtils.toString(httpEntity);
System.out.println(response);
}
// 获取Cookie
List<Cookie> cookies = cookieStore.getCookies();
// 创建HttpPost实例
httpPost = new HttpPost("http://example.com/data");
// 设置请求参数
requestConfig = RequestConfig.custom()
.setConnectTimeout(5000).setConnectionRequestTimeout(5000)
.setSocketTimeout(5000).build();
httpPost.setConfig(requestConfig);
// 设置请求头
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept", "application/json");
// 设置请求体参数
String requestBody = "{\"key\":\"value\"}";
httpPost.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
// 设置Cookie
httpClient = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
// 执行HttpPost请求
httpResponse = httpClient.execute(httpPost);
// 获取响应实体
httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
String response = EntityUtils.toString(httpEntity);
System.out.println(response);
}
}
}
```
httpclient获取cookie
HttpClient可以通过以下步骤获取cookie:
1. 创建HttpClient对象。
2. 创建HttpGet或HttpPost对象。
3. 执行HttpGet或HttpPost请求。
4. 获取HttpResponse对象。
5. 获取HttpResponse中的cookie。
以下是示例代码:
```java
// 创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建HttpGet对象
HttpGet httpGet = new HttpGet("http://www.example.com");
// 执行HttpGet请求
CloseableHttpResponse response = httpClient.execute(httpGet);
// 获取HttpResponse中的cookie
Header[] headers = response.getHeaders("Set-Cookie");
for (Header header : headers) {
System.out.println(header.getValue());
}
// 关闭HttpResponse和HttpClient
response.close();
httpClient.close();
```
阅读全文