java 방향 을 바 꾼 url 가 져 오기

4802 단어
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> 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 += "
" + 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 +="
" + 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

좋은 웹페이지 즐겨찾기