码迷,mamicode.com
首页 > Web开发 > 详细

HTTPUTILS

时间:2018-12-31 14:42:18      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:tst   system   ica   tor   catch   encode   throw   ace   basic   

maven依赖

<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.6</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.47</version>
		</dependency>

  

 

工具类

 

package com.yyjdemo.shardingjdbc.http;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

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

public class HttpUtils {
    public static void main(String[] args) throws IOException {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id","1");
//        String httppost = httppost("http://127.0.0.1:8080/jdbc/testpost",jsonObject);
//        System.out.println(httppost);
//        String httppost = httppostjson("http://127.0.0.1:8080/jdbc/testpostjson",jsonObject);
        String httppost = httpget("http://127.0.0.1:8080/jdbc/test",jsonObject);
    }

    public static String httpget(String url, JSONObject jsonObject) {
        try {
            URIBuilder uriBuilder = new URIBuilder(url);
            if(!jsonObject.isEmpty()){
                jsonObject.forEach((k,v)->{
                    uriBuilder.addParameter(k,v.toString());
                });
            }

            HttpGet httpget = new HttpGet(uriBuilder.toString());
            try(CloseableHttpClient httpclient = HttpClients.createDefault();
                CloseableHttpResponse execute = httpclient.execute(httpget);){
                HttpEntity entity = execute.getEntity();
                String s = EntityUtils.toString(entity);
                return s;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
     return null;
    }

    public static String httppostjson(String url, JSONObject jsonObject) {
        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader(HTTP.CONTENT_TYPE,"application/json");
            StringEntity stringEntity = new StringEntity(jsonObject.toString(),"utf-8");
            httpPost.setEntity(stringEntity);
            try(CloseableHttpClient httpclient = HttpClients.createDefault();
                CloseableHttpResponse execute = httpclient.execute(httpPost);){
                HttpEntity entity = execute.getEntity();
                String s = EntityUtils.toString(entity);
                return s;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private static String httppost(String url,JSONObject jsonObject){
        try {
            HttpPost httpPost = new HttpPost(url);
            List<NameValuePair> objects = new ArrayList<>();
            jsonObject.forEach((k,v)->{
                BasicNameValuePair basicNameValuePair = new BasicNameValuePair(k, v.toString());
                objects.add(basicNameValuePair);
            });
            StringEntity stringEntity = new UrlEncodedFormEntity(objects,"utf-8");
            httpPost.setEntity(stringEntity);
            try(CloseableHttpClient httpclient = HttpClients.createDefault();
                CloseableHttpResponse execute = httpclient.execute(httpPost);){
                HttpEntity entity = execute.getEntity();
                String s = EntityUtils.toString(entity);
                return s;
        }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}

  

HTTPUTILS

标签:tst   system   ica   tor   catch   encode   throw   ace   basic   

原文地址:https://www.cnblogs.com/yeyongjian/p/10201888.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!