java 使用HttpClient發送請求包含get,post

 pom依賴

 <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.6</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

 

package com.common.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.nio.charset.Charset;

public class HttpClientUtil {


    public static void main(String[] args) throws Exception {

        //1.查詢工作區配置
        String nameSpace = "sms";
        String config = "sms";
        String profile = "test";
        String s = sendGet("http:/api.query/v1.0/namespace/"+nameSpace+"/configs/"+config+"/profiles/"+profile+"/items","e3f793a59","smsApp");
        System.out.println(s);
    }

    public static String sendGet(String url,  String token, String application) throws Exception {
        HttpClient httpClient;
        HttpGet postMethod;
        HttpResponse response;
        String reponseContent;
        httpClient = HttpClients.createDefault();
        postMethod = new HttpGet(url);
        postMethod.addHeader("Content-type", "text/plain;charset=utf-8");
        postMethod.addHeader("accept", "*/*");
        postMethod.addHeader("connection", "Keep-Alive");
        postMethod.addHeader("token", token);
        postMethod.addHeader("application", application);
        response = httpClient.execute(postMethod);
        HttpEntity httpEntity = response.getEntity();
        reponseContent = EntityUtils.toString(httpEntity);
        EntityUtils.consume(httpEntity);
        return reponseContent;
    }

   //post請求
    public static String sendPost(String url, String token, String application,String json) {
        HttpClient httpClient;
        HttpPost postMethod;
        HttpResponse response;
        String reponseContent = null;
        try {
            httpClient = HttpClients.createDefault();
            postMethod = new HttpPost(url);
            //設置請求頭
            postMethod.addHeader("Content-type", "application/json; charset=utf-8");
            postMethod.addHeader("accept", "*/*");
            postMethod.addHeader("connection", "Keep-Alive");
            postMethod.addHeader("token", token);
            postMethod.addHeader("application", application);
            postMethod.setEntity(new StringEntity(json, Charset.forName("UTF-8")));
            response = httpClient.execute(postMethod);
            HttpEntity httpEntity = response.getEntity();
            reponseContent = EntityUtils.toString(httpEntity);
            EntityUtils.consume(httpEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return reponseContent;
    }

    //put請求
    public static String sendPut(String url, String token, String application,String json) {
        HttpClient httpClient;
        HttpPut postMethod;
        HttpResponse response;
        String reponseContent = null;
        try {
            httpClient = HttpClients.createDefault();
            postMethod = new HttpPut(url);
            //設置請求頭
            postMethod.addHeader("Content-type", "application/json; charset=utf-8");
            postMethod.addHeader("accept", "*/*");
            postMethod.addHeader("connection", "Keep-Alive");
            postMethod.addHeader("token", token);
            postMethod.addHeader("application", application);
            postMethod.setEntity(new StringEntity(json, Charset.forName("UTF-8")));
            response = httpClient.execute(postMethod);
            HttpEntity httpEntity = response.getEntity();
            reponseContent = EntityUtils.toString(httpEntity);
            EntityUtils.consume(httpEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return reponseContent;
    }


}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章