无所不能的四种请求方式(天下武功为怪不破)

阅读更多
package com.teamdev.jxbrowser.chromium.demo.lvmama_bjcp.店铺.住宿;

import java.io.BufferedInputStream;
import java.io.BufferedReader; 
import java.io.ByteArrayOutputStream;
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.HttpURLConnection;
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.GZIPInputStream;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class GetUrlGetHotel {

     
/**
  * web get请求
  * */
public static String getHtmlGet(String urlStr) { 
        try { 
            URL url = new URL(urlStr);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("GET");
            con.setDoOutput(true); 
            con.setDoInput(true);
con.setUseCaches(false);
            // 默认是 GET方式
con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            con.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
    con.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
    con.setRequestProperty("Cache-Control", "max-age=0");
    con.setRequestProperty("Connection", "keep-alive");
    con.setRequestProperty("Content-Type", "application/json; charset=utf-8");
    con.setRequestProperty("Host", "s.lvmama.com");
    con.setRequestProperty("Upgrade-Insecure-Requests", "1");
    con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
            OutputStreamWriter out = new OutputStreamWriter(con
                    .getOutputStream(),"UTF-8");
            //String parm = "type=all¤tPage="+pageIndex+"&totalCount="+ev_num+"&placeId=10034946" +
            // "&productId=&placeIdType=PLACE&isPicture=&isBest=&isPOI=Y&isELong=Y";
            //out.write(new String(parm.getBytes("utf-8")));
            out.flush();
            out.close();
BufferedInputStream bis = new BufferedInputStream(new GZIPInputStream(con.getInputStream()));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len;
byte[] arr = new byte[1024];
while((len=bis.read(arr))!= -1){
bos.write(arr,0,len);
bos.flush();
}
bos.close();
            return bos.toString("utf-8");
        } catch (MalformedURLException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        }
return null; 
    }


/**
  * 多个参数的web post请求
  * */
public static String getHtmlPost(String pageIndex) { 
        try { 
            URL url = new URL("https://hotel.jd.com/api/json/getHotelList");
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
            con.setDoOutput(true); 
            con.setDoInput(true);
con.setUseCaches(false);
            // 默认是 GET方式
            con.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
    con.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
    con.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
    con.setRequestProperty("Connection", "keep-alive");
    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    con.setRequestProperty("Cookie","cn=0; unpl=V2_ZzNtbUAFExN0DUFVfREPDWJTRggSA0sUfQ1CV3saXwUyUBFcclRCFXwUR1JnGloUZwIZXUZcRh1FCHZXchBYAWcCGllyBBNNIEwHDCRSBUE3XHxcFVUWF3RaTwEoSVoAYwtBDkZUFBYhW0IAKElVVTUFR21yVEMldQl2VHoYWQNnBxpfQmdzEkU4dlR5GV0AYDMTbUNnAUEpCkJWexpbSGcCE1hEV0cddwh2VUsa; __jdv=122270672|baidu-pinzhuan|t_288551095_baidupinzhuan|cpc|0f3d30c8dba7459bb52f2eb5eba8ac7d_0_3ca7046079b94aedae9094521221db20|1532657751699; PCSYCityID=1; shshshfpa=a5c42043-fe72-ad41-b1e8-9dda3b0c262c-1532657753; ipLoc-djd=1-72-2799-0; shshshfp=546db3f16653ecbba5cab2e0d17f01a7; shshshsID=e92bfb065abe77c491cf64bdfa51a46a_2_1532657770541; shshshfpb=014d1bcf8df5532c759221ebb513446b881b3534352157d7b5af2ad7bb; __guid=107159195.3197066150622231000.1532657788366.921; cityName=%u5317%u4EAC%7C36; monitor_count=3; __jda=122270672.1004715931.1524121297.1531460818.1532657752.28; __jdb=122270672.7.1004715931|28.1532657752; __jdc=122270672; __jdu=1004715931; 3AB9D23F7A4B3C9B=I33RQ6RM6LCSWTGHWCWKVHO32GQPWZAEHB4WK5YDNWTGB3FIHBJ7SGFNW2QEI2OCI4BEFJT5A6RX4MOU6OFPFSAR74");
    con.setRequestProperty("Host", "hotel.jd.com");
    con.setRequestProperty("Origin", "https://hotel.jd.com");
    con.setRequestProperty("Referer", "https://hotel.jd.com/list.html?cityId=36&cityName=%E5%8C%97%E4%BA%AC&checkInDate=2018-07-28&checkOutDate=2018-07-29&keyword=&poiType=&poiCode=");
    con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36");
    con.setRequestProperty("X-Requested-With", "XMLHttpRequest");
            OutputStreamWriter out = new OutputStreamWriter(con
                    .getOutputStream(),"UTF-8");
            String parm = "keyword=&cityId=36&checkInDate=2018-07-28&checkOutDate=2018-07-29&minPrice=" +
            "&maxPrice=&stars=&pageSize=30&pageNum="+pageIndex+"&bedType=&amenities=&promotions=" +
            "&themes=&breakfast=&order=&payMode=&poi=%5B%7B%22type%22%3A%2254%22%2C%22code%22%3A%222310%22%7D%5D";
            out.write(new String(parm.getBytes("utf-8")));
            out.flush();
            out.close();
            System.out.println(con.getResponseCode());
            if(200 == con.getResponseCode()){
            //得到输入流
                InputStream is =con.getInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while(-1 != (len = is.read(buffer))){
                    baos.write(buffer,0,len);
                    baos.flush();
                }
                return baos.toString("utf-8");
            }
            //压缩
// BufferedInputStream bis = new BufferedInputStream(new GZIPInputStream(con.getInputStream()));
// ByteArrayOutputStream bos = new ByteArrayOutputStream();
// int len;
// byte[] arr = new byte[1024];
// while((len=bis.read(arr))!= -1){
// bos.write(arr,0,len);
// bos.flush();
// }
// bos.close();
//             return bos.toString("utf-8");
        } catch (MalformedURLException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        }
return null; 
    } /**
  * json参数的app get请求
  * */
public static String GetAPPGetByJsonText(String urlStr,String pageIndex) { 
        try { 
            URL url = new URL(urlStr); 
            URLConnection con = url.openConnection(); 
            con.setDoOutput(true); 
            con.setRequestProperty("Cache-Control", "max-age=5");
    con.setRequestProperty("ApplicationContext","eyJFbXBsb3llZUlkIjoiMDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwIiwiTG9naW5DdXJyZW50Q3VsdHVyZSI6MCwiTG9naW5EZXBhcnRtZW50IjoiMDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwIiwiTG9naW5PcmciOiIwMDAwMDAwMC0wMDAwLTAwMDAtMDAwMC0wMDAwMDAwMDAwMDAiLCJMb2dpblRlbmFudElkIjoiMDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwIiwiTG9naW5UaW1lIjoiL0RhdGUoMTUzMjA2NjY5NjMyMyswODAwKS8iLCJMb2dpblVzZXJDb2RlIjoiODYyNzcwMDMxMjcwNjM1QGIwOjg5OjAwOjk5OjI3OmU4IiwiTG9naW5Vc2VySUQiOiJhNmI3NTI3My0wMTUzLTQ0ZTQtYmUwZC0yOWNmMDA2ZWJiMjIiLCJTZXNzaW9uSUQiOiI4MDNhYmM2NS1iZjlkLTRlOWMtYmY1Ni0yNjhmYWY2NWU0OWQifQ==");
    con.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
    con.setRequestProperty("Accept-Encoding", "");
    con.setRequestProperty("Connection", "Keep-Alive");
    con.setRequestProperty("Content-Type", "application/json; charset=utf-8");
    con.setRequestProperty("Host", "iustore.iuoooo.com");
    con.setRequestProperty("User-Agent", "okhttp/3.2.0");
            OutputStreamWriter out = new OutputStreamWriter(con 
                    .getOutputStream());     
            //String xmlInfo = getXmlInfo(); 
            //System.out.println("urlStr=" + urlStr); 
            //System.out.println("xmlInfo=" + xmlInfo); 
            out.write("{\"getStoreInfosSearchDTO\":{\"appId\":\"b3ad5aba-ae1e-4f4b-ba55-c5fb0375de3c\",\"areaCode\":\"3-110112\",\"filter\":{\"areaCode\":\"\",\"distance\":\"\",\"govGrade\":\"\",\"marketCode\":\"\",\"restaurantType\":\"\",\"tradeAreaCode\":\"\"},\"lat\":\"39.966983795166\",\"lng\":\"116.30883026123\",\"pageIndex\":"+pageIndex+",\"pageSize\":20,\"userId\":\"a6b75273-0153-44e4-be0d-29cf006ebb22\"}}");
            //out.write(new String(xmlInfo.getBytes("ISO-8859-1"))); 
            out.flush(); 
            out.close(); 
            BufferedReader br = new BufferedReader(new InputStreamReader(con 
                    .getInputStream())); 
            String line = ""; 
            for (line = br.readLine(); line != null; line = br.readLine()) { 
                //System.out.println(line);
            return line;
            } 
          
        } catch (MalformedURLException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        }
return null; 
    }

  public static String testPost(String urlStr,String pageIndex) { 
        try { 
            URL url = new URL(urlStr); 
            URLConnection con = url.openConnection(); 
            con.setDoOutput(true); 
            con.setRequestProperty("Cache-Control", "max-age=5");
    con.setRequestProperty("ApplicationContext","eyJFbXBsb3llZUlkIjoiMDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwIiwiTG9naW5DdXJyZW50Q3VsdHVyZSI6MCwiTG9naW5EZXBhcnRtZW50IjoiMDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwIiwiTG9naW5PcmciOiIwMDAwMDAwMC0wMDAwLTAwMDAtMDAwMC0wMDAwMDAwMDAwMDAiLCJMb2dpblRlbmFudElkIjoiMDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAwIiwiTG9naW5UaW1lIjoiL0RhdGUoMTUzMjA2NjY5NjMyMyswODAwKS8iLCJMb2dpblVzZXJDb2RlIjoiODYyNzcwMDMxMjcwNjM1QGIwOjg5OjAwOjk5OjI3OmU4IiwiTG9naW5Vc2VySUQiOiJhNmI3NTI3My0wMTUzLTQ0ZTQtYmUwZC0yOWNmMDA2ZWJiMjIiLCJTZXNzaW9uSUQiOiI4MDNhYmM2NS1iZjlkLTRlOWMtYmY1Ni0yNjhmYWY2NWU0OWQifQ==");
    con.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
    con.setRequestProperty("Accept-Encoding", "");
    con.setRequestProperty("Connection", "Keep-Alive");
    con.setRequestProperty("Content-Type", "application/json; charset=utf-8");
    con.setRequestProperty("Host", "iustore.iuoooo.com");
    con.setRequestProperty("User-Agent", "okhttp/3.2.0");
            OutputStreamWriter out = new OutputStreamWriter(con 
                    .getOutputStream());     
            //String xmlInfo = getXmlInfo(); 
            //System.out.println("urlStr=" + urlStr); 
            //System.out.println("xmlInfo=" + xmlInfo); 
            out.write("{\"getStoreInfosSearchDTO\":{\"appId\":\"b3ad5aba-ae1e-4f4b-ba55-c5fb0375de3c\",\"areaCode\":\"3-110112\",\"filter\":{\"areaCode\":\"\",\"distance\":\"\",\"govGrade\":\"\",\"marketCode\":\"\",\"restaurantType\":\"\",\"tradeAreaCode\":\"\"},\"lat\":\"39.966983795166\",\"lng\":\"116.30883026123\",\"pageIndex\":"+pageIndex+",\"pageSize\":20,\"userId\":\"a6b75273-0153-44e4-be0d-29cf006ebb22\"}}");
            //out.write(new String(xmlInfo.getBytes("ISO-8859-1"))); 
            out.flush(); 
            out.close(); 
            BufferedReader br = new BufferedReader(new InputStreamReader(con 
                    .getInputStream())); 
            String line = ""; 
            for (line = br.readLine(); line != null; line = br.readLine()) { 
                //System.out.println(line);
            return line;
            } 
          
        } catch (MalformedURLException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        }
return null; 
    }
 
    public static void main(String[] args) { 
    String url = "http://s.lvmama.com/hotel/C20180727O20180728P1U13?keyword=&mdd=%E5%8C%97%E4%BA%AC#list#list";
    try {
    System.out.println(getHtmlGet(url));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
       
    } 
   
}

你可能感兴趣的:(http)