博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpURLConnection网略请求
阅读量:7211 次
发布时间:2019-06-29

本文共 3085 字,大约阅读时间需要 10 分钟。

package nettest;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.util.HashMap;import net.sf.json.JSONArray;import net.sf.json.JSONObject;public class TestOne {    public static void main(String[] args) {                getDataFromDH(null, null);  //        testFromJson();    }                private static void getDataFromDH(String method, String param) {        URL url = null;        // 自定义实体类,我自定义的是和对方返回的json对应的类型        // BackData data=null;        try {            // 此处参数baseurl、method、param合成一个完整的url 如            // http://192.168.29.200:8080/dahuaIS/rest/statistic/picrecord/参数            // 这里的参数param在后文会特别说明//            url = new URL("http://" + baseurl + method + param);            String urlStr = "";            urlStr = "http://api.beisenapp.com/recruitv2/103634/Applicant/ByMobile/19999999999";//            urlStr = "http://api.beisenapp.com/recruitv2/103634/Applicant/ById/157125218?language=1&photo_base64=1";            url = new URL(urlStr);            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 获取连接            // 这里的set方法主要设置一些请求头的参数            connection.setRequestMethod("GET");// 设置请求方式            connection.setRequestProperty("Host", "api.beisenapp.com");            connection.setRequestProperty("Connection", "Keep-Alive");            connection.setRequestProperty("Accept", "application/json");            connection.setRequestProperty("Content-Type", "application/json");            connection.setRequestProperty("Authorization", "Bearer dd28e");// 按照对方要求设置的相关认证内容            connection.connect();//            Gson gson = new GsonBuilder().create();// 这里使用到了google 的Gson.jar                                                    // 可以方便地把对象和json格式互转            InputStream is = connection.getInputStream();            InputStreamReader r = new InputStreamReader(is, "utf-8");            BufferedReader br = new BufferedReader(r);            String line = "";            StringBuffer sb = new StringBuffer(200);            while((line = br.readLine()) !=null){                sb.append(line);            }                 String temp = sb.substring(1, sb.length()-1);                JSONObject obj = JSONObject.fromObject(temp);                int ApplicantId = obj.getInt("ApplicantId");                JSONArray arr =  obj.getJSONArray("ApplyJobSummaries");                JSONObject map = (JSONObject) arr.get(0);                int JobId =   map.getInt("JobId");            // 最后关闭流            br.close();            r.close();            is.close();            connection.disconnect();// 断开连接        } catch (Exception e) { // 以下异常具体情况具体处理            e.printStackTrace();        }         // return data;    }            public static void testFromJson(){        //"httpResponse": {"StatusCode": 200,"SuppressEntityBody": false,}        JSONObject obj = JSONObject.fromObject("\"httpResponse\": {\"StatusCode\": 200,\"SuppressEntityBody\": false,}");    }}

 

转载于:https://www.cnblogs.com/lxh520/p/8339882.html

你可能感兴趣的文章
OpenStack快速入门-queens版本
查看>>
大数据驱动智能制造 物联网引爆工业革命商机
查看>>
一个比较完善的购物车类
查看>>
「镁客·请讲」Visense Studio冯樑杰:游戏基因的VR视频,最好的表现是真人实拍交互...
查看>>
让人欲罢不能的量子学
查看>>
美团在Redis上踩过的一些坑-2.bgrewriteaof问题
查看>>
C# StreamReader.ReadLine统计行数的问题
查看>>
异常测试实践与梳理
查看>>
多者异也
查看>>
tf:'hello tensorflow'
查看>>
RedisConf2018记录--Day 1 sessions
查看>>
CentOS的el5, el6, el7代表什么
查看>>
柏林纪行(中):Node.js Collaboration Summit
查看>>
IT网络通信大变革时代来临 2016中国极客大奖为您找到风向标
查看>>
如何下载WDK
查看>>
硬纪元干货|镁客网萨向东:推动硬科技产业落地,助力传统产业升
查看>>
SSDT&Shadow Hook的实现,完整代码。可编译
查看>>
Spring4-自动装配Beans-通过注解@Autowired在构造方法上
查看>>
MapReduce编程(四) 求均值
查看>>
ASP.NET MVC在IIS6下部署的小技巧
查看>>