`

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

阅读更多
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&currentPage="+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();
}
       
    } 
   
}
分享到:
评论

相关推荐

    苹果JBL无所不能遥控器操作说明

    苹果JBL“无所不能”遥控器是一款功能强大的遥控设备,适用于多种场合。通过本文详细介绍的操作指南和安全须知,使用者可以更好地了解如何正确、安全地使用这款遥控器,从而获得更好的用户体验。在日常使用过程中,...

    让迅雷无所不能下载.rar

    总的来说,"让迅雷无所不能下载"的补丁可能是一种针对迅雷的优化工具,旨在扩展其下载能力。然而,考虑到安全性和合法性,用户在使用这类工具时应保持警惕,避免潜在的风险。如果需要增强迅雷的下载能力,建议关注...

    让迅雷无所不能下载、通吃一切

    让迅雷无所不能下载、通吃一切 让迅雷无所不能下载、通吃一切

    让迅雷无所不能的工具 迅雷下载其他资源 电驴 快车

    这两种下载方式与迅雷基于BTP(BitTorrent Protocol)的下载机制不同,因此直接在迅雷中打开这些链接可能无效。 为了让迅雷下载电驴和快车的资源,可以采用以下几种方法: 1. **转换链接**:有些在线服务或软件...

    电脑测试软件很好用无所不能

    标题中的“电脑测试软件很好用无所不能”表明我们讨论的是一款功能强大的计算机系统诊断和测试工具,它能够全面地分析和评估电脑硬件和软件的性能。这类软件通常包含多个模块,用于测试处理器、内存、硬盘、显卡等...

    VB无所不能系列(包含文档说明和代码示例)

    根据提供的文件信息,以下是关于VB无所不能系列的详细知识点。 ### 1. 指针 指针在VB中的使用与C语言等其他编程语言中的使用有所不同,因为VB是一种高级语言,它对内存访问进行了封装。在VB中,可以通过API调用和...

    VB无所不能,叫你加深了解

    网络精选教程,希望大家不要忘记VB,包括GDI绘画等详解的例子

    超级模块4.2无所不能

    无所不能 相当强大 目前最强大内容最丰富的超级模块

    让迅雷无所不能【绿色补丁】

    而“让迅雷无所不能【绿色补丁】”显然是一种针对迅雷软件的修改或优化,旨在扩展其功能,尤其是使其能够处理原本专属于其他下载工具,如快车(FlashGet)的下载链接。 首先,我们要理解什么是“绿色补丁”。在...

    快乐网管之无所不能软件

    【快乐网管之无所不能软件】是一款针对初级用户的自动化工具,类似于知名的【按键精灵】,旨在简化用户在日常计算机操作中的复杂流程。该软件通过模拟鼠标点击和键盘输入,能够执行一系列预先设定好的任务,极大地...

    互联网 智慧能源-无所不能 完整

    智慧能源-无所不能 完整

    ChatGPT 真能上天入地?无所不能?

    ChatGPT 真能上天入地?无所不能?

    懂得父母并非无所不能是成人的标志之一

    然而,为了满足题目要求,我们可以从中提取一些相关的人生智慧和心理成长的建议,这些内容虽然不属于IT领域,却能为个人成长提供指导和启示,进而间接对个人在职场中的表现和决策能力产生积极影响。 一、个人成长与...

    万能编程器软件---无所不能

    总的来说,"万能编程器软件---无所不能"不仅是一个工具,更是一种解决方案,它简化了对各种类型芯片编程的过程,提高了开发效率。无论是用于学习、原型设计还是大规模生产,这样的软件都是电子工程师的得力助手。其...

    真正的无所不能!

    标题中的“真正的无所不能!”可能是指这个压缩包包含了大量的IT学习资源,涵盖了多个领域的知识,而描述中的“我所下的所有书的整合!绝对经典!只要你能想到的这里面都有!”进一步证实了这一点,说明这个集合包含...

    能源互联网白皮书-to 无所不能.pptx

    能源互联网白皮书-to 无所不能.pptx

    迅雷5专用让迅雷无所不能下载

    它会自动为下载的数据分配内存缓冲区,减少硬盘读写次数,保护硬盘的同时提高了下载效率。同时,迅雷5还具备断点续传功能,即使下载过程中因网络问题中断,也能在恢复连接后继续从上次中断的地方下载,无需重新开始...

Global site tag (gtag.js) - Google Analytics