hutool自定義請求頭請求接口


一.案列代碼

1.1 導入Maven依賴

<dependency>
   <groupId>cn.hutool</groupId>
   <artifactId>hutool-all</artifactId>
   <version>4.1.0</version>
</dependency>

 

1.2編寫測試類(使用Junit單元測試)

package com.grand.modelSMS;

import org.junit.Test;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;

public class JunitTest {

	@Test
	public void test1() {
		//1. 組建請求json參數
		JSONObject json = new JSONObject();
		json.put("appkey", "2e351b12c5030");
		json.put("androidTitle", "PUSHDEMO");
		Integer[] plat=new Integer[] {1};
		json.put("plats",plat);
		json.put("target", 4);
		String[] registrationId=new String[] {"16772de0f6071dd4024b25de"};
		json.put("registrationIds",registrationId);
		json.put("content","test");
		json.put("type", 1);
		String url=" http://api.push.mob.com/v2/push";
		/*發送post請求並接收響應數據
		 * 采用的是一種叫鏈式編程的方式):
			header對應的是請求頭。
			body對應的是請求體(包含參數和參數值)。
			HttpRequest里面包含Post、GET、Delete、Put等常用的RestFul方式。*/
		String post = HttpRequest.post(url)
				.header("key","2e351b12c5030")
				.header("sign","ee29e949d8c480a67e61e7921ac2dce7")
				.body(json)
				.execute().body();
		System.out.println(post);
	}

}

  方法解釋(上面采用的是一種叫鏈式編程的方式):
header對應的是請求頭。
body對應的是請求體(包含參數和參數值)。
HttpRequest里面包含Post、GET、Delete、Put等常用的RestFul方式。

1.3打印如下:

 

 以上代碼親測有效

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM