HttpUtil工具类发送post请求

import java.io.IOException;
 
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
 
public class HttpUtils {
    
    private static String charset = "utf-8";
    
    public static String sendPost (String url, String json) throws ClientProtocolException, IOException{
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpClient client = HttpClients.createDefault();
        
        StringEntity entity = new StringEntity(json, charset);//解决中文乱码问题    
        entity.setContentEncoding(charset);    
        entity.setContentType("application/json");    
        httpPost.setEntity(entity);
        HttpResponse response = client.execute(httpPost);
        
        if(response.getStatusLine().getStatusCode() == 200){
            HttpEntity httpEntity = response.getEntity();
            return EntityUtils.toString(httpEntity, charset);
        }
        return null;
    }
}

 

package com.ali.mayikt.http;


import com.ali.mayikt.http.entity.VisitorApprovalInfo;
import com.ali.mayikt.http.entity.VisitorNoticeInfo;
import com.ali.mayikt.http.entity.vo.VisitorRegisterVo;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

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

/**
 * @Auther: edenCui
 * @Date: 2019/8/2 19:19
 * @Description:
 */
public class testHttpUtils {
    /**
     * 用junit测试
     */
    @Test
    public void sendPost() {
        String result = HttpUtil.sendGet("http://192.168.0.101:9001/from","");
        String result2 = HttpUtil.sendPost("http://192.168.0.101:9001/from","");
        String result3 = HttpUtil.sendPost("http://192.168.0.101:7082/stream/sendMsg","");


        System.out.println("---"+result);
        System.out.println("---"+result2);

        System.out.println("---"+result3);
    }

    @Test
    public void sendPostByJson() {
        VisitorRegisterVo visitorRegisterVo = new VisitorRegisterVo();
        visitorRegisterVo.setCrtPersonId("2019080401");
        visitorRegisterVo.setCrtPersonNm("孙悟空");

        List  visitorNoticeInfoList = new ArrayList<>();
        VisitorNoticeInfo visitorNoticeInfo = new VisitorNoticeInfo( "001",  "布尔玛", "110");
        visitorNoticeInfoList.add(visitorNoticeInfo);
        VisitorNoticeInfo visitorNoticeInfo2 = new VisitorNoticeInfo( "002",  "琪琪", "119");
        visitorNoticeInfoList.add(visitorNoticeInfo2);
        visitorRegisterVo.setVisitorNoticeList(visitorNoticeInfoList);

        ArrayList visitorApprovalInfoList = new ArrayList<>();
        VisitorApprovalInfo visitorApprovalInfo = new VisitorApprovalInfo( "西界王", "1");
        visitorApprovalInfoList.add(visitorApprovalInfo);
        VisitorApprovalInfo visitorApprovalInfo2 = new VisitorApprovalInfo( "北界王", "1");
        visitorApprovalInfoList.add(visitorApprovalInfo2);
        visitorRegisterVo.setVisitorApprovalList(visitorApprovalInfoList);

        String jsonString = JSONObject.toJSONString(visitorRegisterVo);
 

        try {
            String result = HttpUtils.sendPost("http://192.168.0.101:6087/visitor/visitorRegister.json", jsonString);
            System.out.println("---"+result);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * 用main方法测试
     */
    public static void main(String[] args) {
//        Object  str=null;
//        String jsonString = JSONObject.toJSONString(str);
//        String result = RobotHttpUtil.dopost("http://192.168.0.101:9001/from", null, jsonString);

        String result = HttpUtil.sendGet("http://192.168.0.101:9001/from","");
        String result2 = HttpUtil.sendPost("http://192.168.0.101:9001/from","");


        System.out.println("---"+result);
        System.out.println("---"+result2);


    }
}

你可能感兴趣的:(HttpUtil工具类发送post请求)