java获取重定向后的的url

package com.numberone.master.modules.test;
 
 
 
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
 
 
 
 
  
 
public class GetPostTest {
    public static String sendGet(String url) {
        String result = "";
        String urlName = url;
        try {
            URL realURL = new URL(urlName);
            URLConnection conn = realURL.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36");
            conn.connect();
            Map<String, List<String>> map = conn.getHeaderFields();
            for (String s : map.keySet()) {
                System.out.println(s + "-->" + map.get(s));
            }
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += "\n" + line;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
  
    public static String sendPost(String url,String param){
        String result = "";
        HttpURLConnection conn = null;  
        try {
            URL realUrl = new URL(url);
            conn =    (HttpURLConnection) realUrl.openConnection();
             
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36");
            conn.setInstanceFollowRedirects(true);
            conn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
            conn.setInstanceFollowRedirects(true);
             
            conn.setInstanceFollowRedirects(false);
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            //post设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);//这一行必须要写,因为要将参数放在outputStream中输出
            //PrintWriter out = new PrintWriter(conn.getOutputStream());
            //out.print(param);
            PrintWriter out =new PrintWriter(conn.getOutputStream());
            //out.write(param);
            out.print(param);  //写入缓存
            out.flush();
                    
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));// 发送请求参数
            String line;
            while((line = in.readLine()) != null){
                result +="\n" + line;
                    }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
 
    public static void main(String[] args) {
        String param = "Search%2FAirlineMode=false&Search%2FcalendarCacheSearchDays=60&Search%2FcalendarSearched=false&dropOffLocationRequired=false&Search%2FsearchType=F&searchTypeValidator=F&xSellMode=false&Search%2FflightType=oneway&destinationLocationSearchBoxType=L&Search%2FisUserPrice=1&Search%2FOriginDestinationInformation%2FOrigin%2Flocation=CITY_BJS_CN&Search%2FOriginDestinationInformation%2FOrigin%2Flocation_input=%E5%8C%97%E4%BA%AC&Search%2FOriginDestinationInformation%2FDestination%2Flocation=CITY_CTU_CN&Search%2FOriginDestinationInformation%2FDestination%2Flocation_input=%E6%88%90%E9%83%BD&Search%2FDateInformation%2FdepartDate_display=2018-09-06&Search%2FDateInformation%2FdepartDate=2018-09-06&Search%2FDateInformation%2FreturnDate=2018-09-05&Search%2FcalendarSearch=false&Search%2FPassengers%2Fadults=1&Search%2FPassengers%2Fchildren=0&Search%2FpromotionCode=";
        //String param = "";
        String sendRecvPost =GetPostTest.sendPost("http://flights.sichuanair.com/3uair/ibe/common/processSearchForm.do",param);
        System.out.println(sendRecvPost);
    }
     
}

转载来源:https://www.52pojie.cn/thread-791217-1-1.html

上一篇:Java 给python发送post请求


下一篇:2.centos7安装keepalived[使用keepalived搭建nginx主备]