Post简单调用其他项目的接口

package com.timespark.util;


import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import java.util.ArrayList;
import java.util.List;

/**
 * Author:szy
 * Date:2019/7/29
 * Description:
 */
public class HttpClientTest {
    public static void main(String[] args) {

            //目标URL
            String url = "http://172.168.28.49:8080/fireManager/send/test";
            //创建一个默认的HttpClient
            HttpClient httpclient = new DefaultHttpClient();

            //以post方式请求网页
            try {
                HttpPost httppost = new HttpPost(url);
                //添加HTTP POST参数
                List nvps = new ArrayList();
                //打印请求地址
                System.out.println("executing request " + httppost.getRequestLine().getUri());
                //创建响应处理器处理服务器响应内容    
                ResponseHandler responseHandler = new BasicResponseHandler();
                //执行请求并获取结果    
                String responseBody = httpclient.execute(httppost, responseHandler);
                System.out.println(responseBody);

            } catch (Exception e) {
                e.printStackTrace();
            } finally {

                // 当不再需要HttpClient实例时,关闭连接管理器以确保释放所有占用的系统资源
                httpclient.getConnectionManager().shutdown();
            }
        }



}

 

你可能感兴趣的:(IDEA,Java)