Webプロジェクト呼び出し外部インタフェースの例


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

public class dtservice {

	public static void main(String[] args) throws Exception  {
		String requestUrl = "http://******/**/service/um/third/***.do";
		HttpPost http = new HttpPost(requestUrl);
		JSONObject caInfoJson = new JSONObject();
		
				try {
			BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
			CloseableHttpClient httpclient = HttpClients.custom()
					.setDefaultCredentialsProvider(credsProvider)
					.build();
			;
			
			caInfoJson.put("OLD_ZSWYBS", "*****"); //    
			
			NameValuePair pair1 = new BasicNameValuePair("jsonStr",
					caInfoJson.toString());
			List list = new ArrayList<>();
			list.add(pair1);
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
					list, "utf-8");
			http.setEntity(entity);
			String reslutDeptUser = printRestData(httpclient, http);
			
			System.out.println(reslutDeptUser);//reslutDeptUser     
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}

	/**
	 * post  。
	 * 
	 * @param httpclient
	 * @param http
	 * @return reslut   。
	 * @throws
	 * @throws Exception
	 */
	public static String printRestData(CloseableHttpClient httpclient,
			HttpPost http) throws Exception {
		CloseableHttpResponse response = null;
		
		InputStream in = null;
		InputStreamReader rd = null;
		BufferedReader br = null;
		String reslut = "";
		response = httpclient.execute(http);
		HttpEntity entity = response.getEntity();
		in = entity.getContent();
		rd = new InputStreamReader(in, "UTF-8");
		br = new BufferedReader(rd);
		String line = br.readLine();
		if (line != null) {
			while (true) {
				reslut = reslut.concat(line);
				line = br.readLine();
				if (line == null) {
					break;
				}
			}
		}
		// reslut = unicodeToUtf8(reslut);
		if (entity != null) {
			System.out.println("Response content length: "
					+ entity.getContentLength());
		}
		http.abort();

		try {
			if (response != null) {
				response.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if (in != null) {
				in.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if (rd != null) {
				rd.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if (br != null) {
				br.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			httpclient.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return reslut;
	}
}