zoukankan      html  css  js  c++  java
  • HTTP请求 Java API

    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);
                    }
                }
            }
        }
    }
    
  • 相关阅读:
    jquery datatable[表格处理]之AdminLTE
    kdevtmpfsi挖矿病毒导致服务器cpu高负荷运行
    Python3 '%Y-%m-%dT%H:%M:%S.000Z' 时间格式转换
    python-生成动态路由轨迹图(scapy模块)
    Python之XlsxWriter模块(数据报表)
    python之web服务质量探测(pycurl模块)
    Django-图片验证码
    最简容器化动手小实践——再战flappybird
    基于OAS设计可扩展OpenAPI
    Istio最佳实践:在K8s上通过Istio服务网格进行灰度发布
  • 原文地址:https://www.cnblogs.com/wh984763176/p/13393043.html
Copyright © 2011-2022 走看看