com.oneapm.base.tools.UrlPostMethod.java Source code

Java tutorial

Introduction

Here is the source code for com.oneapm.base.tools.UrlPostMethod.java

Source

package com.oneapm.base.tools;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * ??jsonurl
 * Created by tianjin on 3/29/16.
 */
public class UrlPostMethod implements Serializable {

    public static void urlPostMethod(String url, String json) {

        HttpClient httpClient = new HttpClient();
        PostMethod method = new PostMethod(url);
        try {
            if (json != null && !json.trim().equals("")) {
                RequestEntity requestEntity = new StringRequestEntity(json, "application/x-www-form-urlencoded",
                        "utf-8");
                method.setRequestEntity(requestEntity);
            }
            long startTime = System.currentTimeMillis();
            int x = httpClient.executeMethod(method);
            long elapsedTime = System.currentTimeMillis();

            System.out.println(x + ":" + (elapsedTime - startTime));

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            method.releaseConnection();
        }
    }

    public static String urlGetMethod(String url) {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet get = new HttpGet(url);
        HttpEntity entity = null;
        String json = null;
        try {
            CloseableHttpResponse response = httpClient.execute(get);
            json = EntityUtils.toString(response.getEntity(), "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json.toString();

    }

    public static void main(String[] args) {

        //        String s = " {\n" +
        //                "        \"warningContent\": \"??76.571%,50.0,70.0\",\n" +
        //                "        \"metricId\": \"??\",\n" +
        //                "        \"warningLevel\": \"levelBad\",\n" +
        //                "        \"resourceInstanceId\": \"123.125.105.253\",\n" +
        //                "        \"occureTime\": \"2016-07-11 14:53:00\",\n" +
        //                "        \"uniqueMark\": \"??-123.125.105.253-cBitpacketAccount\",\n" +
        //                "        \"ruleId\": \"??\",\n" +
        //                "        \"warningSource\": \"Ni\",\n" +
        //                "        \"domainName\": \"\",\n" +
        //                "        \"isConfirm\": \"?\",\n" +
        //                "        \"resourceName\": \"123.125.105.253\",\n" +
        //                "        \"resourceType\": \"\",\n" +
        //                "        \"ipAddress\": \"123.125.105.253\"\n" +
        //                "    }";
        //
        //
        //        String s1 = "{\n" +
        //                "        \"warningContent\": \"????5707344.267,1000000.0,4000000.0\",\n" +
        //                "        \"metricId\": \"????\",\n" +
        //                "        \"warningLevel\": \"levelBad\",\n" +
        //                "        \"resourceInstanceId\": \"TLSV1.2\",\n" +
        //                "        \"occureTime\": \"2016-07-11 15:56:00\",\n" +
        //                "        \"uniqueMark\": \"????-TLSV1.2-throughput\",\n" +
        //                "        \"ruleId\": \"????\",\n" +
        //                "        \"warningSource\": \"Ni\",\n" +
        //                "        \"domainName\": \"\",\n" +
        //                "        \"isConfirm\": \"?\",\n" +
        //                "        \"resourceName\": \"TLSV1.2\",\n" +
        //                "        \"resourceType\": \"?\",\n" +
        //                "        \"ipAddress\": \"\"\n" +
        //                "    }";
        //
        //        List<String> list = new ArrayList<>();
        //        for (int i = 0; i < 12001; i++) {
        //            list.add(s1);
        //        }

        //        UrlPostMethod.urlPostMethod("http://10.128.9.220/portal/api/warning/extern/ni/warning/add.do", "warnings=" + list.toString());
        //
        //
        //        for (int i = 0; i <= list.size() / 3000; i++) {
        //            UrlPostMethod.urlPostMethod("http://10.128.9.220/portal/api/warning/extern/ni/warning/add.do", "warnings=" + Arrays.asList(Arrays.copyOfRange(list.toArray(), i *
        //                    3000, (i + 1) * 3000 - 1 > list.size() ? list.size() : (i + 1) * 3000 - 1)).toString());
        //        }

        System.out.println(UrlPostMethod.urlGetMethod("http://10.128.9.207:8060/license_get").toString());
    }
}