首页 > 编程语言 > 详细

HTTP请求 Java API

时间:2020-07-28 22:51:33      阅读:90      评论:0      收藏:0      [点我收藏+]

1.导入依赖

        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.0.1</version>
        </dependency>

2.Post请求

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import java.io.IOException;

/**
 * @description: TODO
 * @author: HaoWu
 * @create: 2020/7/18 16:46
 */
public class AlarmNotifyTest {
    public static void main(String[] args) throws IOException {
        //1.创建客户端
        HttpClient client = new HttpClient();
        String alarmName = "azkaban的的任务运行情况";
        String alarmContent = "azkaban的任务执行完毕";
        String url = String.format("http://api.aiops.com/alert/api/event?app=19663a74-69f9-462f-aac7-6e46c7c0bf1d&eventId=yyy&eventType=trigger&alarmContent=%s&alarmName=%s&priority=2", alarmContent, alarmName);
        //2.创建post方法,封装参数
        PostMethod method = new PostMethod(url);
        String content = "";
        StringRequestEntity stringRequestEntity = new StringRequestEntity(content, "application/json", "UTF-8");
        method.setRequestEntity(stringRequestEntity);
        //3.执行http请求
        int code = client.executeMethod(method);
        if (code == 200) {
            method.getResponseBodyAsString();
        }
    }
}

3.Get请求

import java.io.*;

/**
 * @description: TODO
 * @author: HaoWu
 * @create: 2020/7/12 16:13
 */
public class PrintIpProvinceCity {
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("F:\\pmt.json");
        BufferedReader br = new BufferedReader(fr);
        String jsonStr = "";
        String[] arrs = null;
        while ((jsonStr = br.readLine()) != null) {
            if (JsonUtils.IsJson(jsonStr)) {
                if (!JsonUtils.IPIsNull(jsonStr)) {
                    String ip = JsonUtils.getIP(jsonStr);
                    String url = "https://restapi.amap.com/v3/ip?ip=" + ip + "&key=f75418e64363b8a96d3565108638c5f1";
                    String province = HttpUtils.getProvinceAndCity(url).getString("province");
                    String city = HttpUtils.getProvinceAndCity(url).getString("city");
                    System.out.println("ip:" + ip + ",province:" + province + ",city:" + city);
                }
            }
        }
    }
}

HTTP请求 Java API

原文:https://www.cnblogs.com/wh984763176/p/13393043.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!