Java接口調用方式POST、GET,如何調用其他項目的接口

如何調用其他項目的接口

首先引入依賴包

<!-- httpclient依賴 -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.2</version>
		</dependency>
		<!-- httpclient緩存 -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient-cache</artifactId>
			<version>4.5</version>
		</dependency>
		<!-- http的mime類型都在這裏面 -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpmime</artifactId>
			<version>4.3.2</version>
		</dependency>

測試Post請求方式

/**
	 * 
	 * post請求
	 * 
	 * 銀行接口數據獲取
	 */
	public JSONObject sendPost(){
		//創建連接對象
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		//post請求
		HttpPost httpPost = new HttpPost("http://127.0.0.1:6868/yinhang");
		//參數拼接
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		for(int i=0; i<5; i++){
			NameValuePair valuePair = new BasicNameValuePair("key"+i,"value"+i);
			list.add(valuePair);
		}
		try {
			//請求報文,參數格式
			httpPost.addHeader("Content-type","application/x-www-form-urlencoded");  
			httpPost.setEntity(new UrlEncodedFormEntity(list,"UTF-8"));
			//發送請求
			HttpResponse response = httpClient.execute(httpPost);
			
			System.out.println("獲取返回服務器的狀態碼:----- "+response.getStatusLine().getStatusCode());
			
			if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
				//成功後響應數據
				String result = EntityUtils.toString(response.getEntity());
				JSONObject json = JSONObject.fromObject(result);
				System.out.println("接口返回數據:"+json.toString());
				return json;
			}
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				//釋放連接
				if(httpClient != null){
					httpClient.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return new JSONObject();
		
	}
	

測試Get請求方式

/**
	 * 
	 * get請求
	 * 
	 * 銀行接口數據獲取
	 */
	public JSONObject sendGet(){
		//創建連接對象
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		//post請求
		HttpGet httpGet = new HttpGet();
		//參數拼接
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		for(int i=0; i<5; i++){
			NameValuePair valuePair = new BasicNameValuePair("key"+i,"value"+i);
			list.add(valuePair);
		}
		String url = "http://127.0.0.1:6868/yinhang";
		try {
			//轉換
			String format = URLEncodedUtils.format(list, "UTF-8");
			httpGet.setURI(URI.create(url +"?"+ format));
			
			//發送請求
			HttpResponse response = httpClient.execute(httpGet);
			
			System.out.println("獲取返回服務器的狀態碼:----- "+response.getStatusLine().getStatusCode());
			
			if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
				//成功後響應數據
				String result = EntityUtils.toString(response.getEntity());
				JSONObject json = JSONObject.fromObject(result);
				System.out.println("接口返回數據:"+json.toString());
				return json;
			}
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				//釋放連接
				if(httpClient != null){
					httpClient.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return new JSONObject();
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章