代码片段-Http请求的调用和创建

使用的依赖包

org.apache.httpcomponents
httpclient
4.3.6


http请求发送

public static sendMessage(String sendUrl,String sendData){
    //获取默认的http请求客户端
    //默认是创建的短连接,获取长链接???
    CloseableHttpCilent client = HttpClient.createDefault();
    //创建post请求
    HttpPost post = new HttpPost(sendUrl);
    //构造请求数据,++++====定义为json格式====++++
    StringEntity se = new StringEntity(sendData,ContentType.APPLICATION.JSON);
    //post中添加请求数据
    post.setEntity(se);
    //创建http请求返回的字符串
    String response = null;
    //创建Http请求的返回数据
    CloseableHttpResponse httpResponse = null;
    try{
        //客户端发送请求,并返回结果
        httpResponse = client.execute(post);
        //对返回的结果参数进行校验,200为正常返回
        if(httpResponse.getStatusLine().getStatusCode() == 200){
            //获取返回的实体类
            HttpEntity entity = httpResponse.getEntity();
            //将实体类转换成String
            response = EntityUtils.toString(entity);
        }
    }catch(IOException e){
        e.printStack();
    }finally{
        try{
            if(httpResponse!=null){
                httpResponse.close();
            }
        }catch(IOException e){
            e.printStack();
        }finally{
            try{
                if(client!=null){
                    client.close();
                }
            }catch(IOException e){
                e.printStack();
            }
        }
    }
    return response;
}

java原生API进行http请求

public static void main(String[] args) {
    try {
       JSONObject obj = new JSONObject();
        obj.put("key", "d1b04deac17643b6a84ce6036bbbb6b6"); //  机器人key  具体申请的机器人对应的key
        obj.put("info", "你好呀");
        System.out.println(obj);
        // 创建url资源
        URL url = new URL("http://www.tuling123.com/openapi/api");
        // 建立http连接
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        // 设置允许输出
        conn.setDoOutput(true);
        //    conn.setDoInput(true);
        // 设置不用缓存
        //    conn.setUseCaches(false);
        // 设置传递方式
        conn.setRequestMethod("POST");
        // 设置维持长连接
        conn.setRequestProperty("Connection", "Keep-Alive");
        // 设置文件字符集:
        conn.setRequestProperty("Charset", "UTF-8");
        // 转换为字节数组
        byte[] data = (obj.toString()).getBytes();
        // 设置文件长度
        conn.setRequestProperty("Content-Length", String
                .valueOf(data.length));
        // 设置文件类型:
        conn.setRequestProperty("Content-Type", "json");
        // 开始连接请求
        conn.connect();
        OutputStream out = conn.getOutputStream();
        // 写入请求的字符串
        out.write((obj.toString()).getBytes());
        out.flush();
        out.close();
        System.out.println(conn.getResponseCode());
        // 请求返回的状态
        if (conn.getResponseCode() == 200) {
            System.out.println("连接成功");
            // 请求返回的数据
            InputStream in = conn.getInputStream();
            String a = null;
            try {
                byte[] data1 = new byte[in.available()];
                in.read(data1);
                a = new String(data1,"utf-8");
                System.out.println(a);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } else {
            System.out.println("no++");
        }
    } catch (Exception e) {
        r.printStack();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值