本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《
阿里云开发者社区用户服务协议
》和
《
阿里云开发者社区知识产权保护指引
》。如果您发现本社区中有涉嫌抄袭的内容,填写
侵权投诉表单
进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
HttpURLConnection获取重定向地址和cookie的代码如下:
public void first() throws Exception{
Map paramsHashMap = new HashMap();
paramsHashMap.
HttpURLConnection获取重定向地址和cookie的代码如下:
public void first() throws Exception{
Map<String, String> paramsHashMap = new HashMap<String, String>();
paramsHashMap.put("IF_NO", "IF0002");
paramsHashMap.put("UID", "12345");
firstRequest(API_HTTP, paramsHashMap, "UTF-8");
private void firstRequest(String path, Map<String, String> params, String encoding) throws Exception{
StringBuilder sb = new StringBuilder();
if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> entry : params.entrySet()){
sb.append(entry.getKey()).append('=');
sb.append(URLEncoder.encode(entry.getValue(), encoding));
sb.append('&');
sb.deleteCharAt(sb.length() - 1);
byte[] entity = sb.toString().getBytes();
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
conn.setInstanceFollowRedirects(true);
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(entity);
//获取重定向地址
redictURL= conn.getHeaderField( "Location" );
System.out.println("第一次请求重定向地址 location="+redictURL);
System.out.println("第一次请求 conn.getResponseCode()="+conn.getResponseCode());
//获取cookie
Map<String,List<String>> map=conn.getHeaderFields();
Set<String> set=map.keySet();
for (Iterator iterator = set.iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
if (key.equals("Set-Cookie")) {
System.out.println("key=" + key+",开始获取cookie");
List<String> list = map.get(key);
StringBuilder builder = new StringBuilder();
for (String str : list) {
builder.append(str).toString();
firstCookie=builder.toString();
System.out.println("第一次得到的cookie="+firstCookie);
HttpURLConnection在请求头中设置cookie的代码如下:
conn.setRequestMethod("POST");
conn.setRequestProperty("Cookie", firstCookie);
最近在做一个转发功能,zuul + ribbon + resttemplate 进行路由、负载、转发的功能
基本准备就绪,在微信自动登陆那遇到了一个坑,ribbon 系统用resttemplate 转发A系统的资源,在微信自动登陆的地方,A系统重定向到微信的地址,类似下面的代码
redirect:https://open.