cmd中发送http请求_「解决方案」发送HTTP请求返回301怎么办?

1、发送HTTP请求,如何从HttpResponse中获取Cookie?

2、发送HTTP请求返回301重定向响应吗,如何发起二次post请求,获得最终的响应?

7176c3faf52ae89616b3239f85cd10d8.png

解决方案如下:

private static String getCookieFromResponse(@NonNull HttpResponse response) { Header[] responseHeader = response.getHeaders("Set-Cookie"); int length = responseHeader.length; StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < length; i++) { if (responseHeader[i] != null) { if ("Set-Cookie".equals(responseHeader[i].getName())) { int index = responseHeader[i].getValue().indexOf(";"); if (i == length - 1) { stringBuilder.append(responseHeader[i].getValue().substring(0, index)); } else { stringBuilder.append(responseHeader[i].getValue().substring(0, index) + "; "); } } } } return stringBuilder.toString(); }  @SuppressWarnings("deprecation") private static HttpResponse doPost(String url, List list, String charset, boolean isSetCookie, String cookieValue) throws Exception { HttpClient httpClient = SSLClient.getSingletonHttpClient(); HttpPost httpPost = new HttpPost(url); if (isSetCookie) { httpPost.addHeader("Cookie
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值