HttpURLConnection网略请求

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