http請求POST方式發送獲得返回值

 請求類


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Http請求工具類
 */
public class HttpRequestUtil {

	
	/**
	 * 向指定 URL 發送POST方法的請求
	 * @param url     發送請求的 URL
	 * @param param   請求參數,請求參數應該是  json 的形式。
	 * @param isproxy 是否使用代理模式
	 * @return 所代表遠程資源的響應結果
	 */
	public static String sendPost(String url, String param) {
		
		OutputStreamWriter out = null;
		BufferedReader in = null;
		String result = "";
		try {
			URL realUrl = new URL(url);
			HttpURLConnection conn = null;
			conn = (HttpURLConnection) realUrl.openConnection();
			/*if (isproxy) {// 使用代理模式
				@SuppressWarnings("static-access")
				Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new InetSocketAddress(proxyHost, proxyPort));
				conn = (HttpURLConnection) realUrl.openConnection(proxy);
			} else {
				conn = (HttpURLConnection) realUrl.openConnection();
			}*/
			// 打開和URL之間的連接

			// 發送POST請求必須設置如下兩行
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setRequestMethod("POST"); //POST方法

			// 設置通用的請求屬性

			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

			conn.connect();

			// 獲取URLConnection對象對應的輸出流
			out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
			// 發送請求參數
			out.write(param);
			// flush輸出流的緩衝
			out.flush();
			// 定義BufferedReader輸入流來讀取URL的響應
			in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			System.out.println("發送 POST 請求出現異常!" + e);
			e.printStackTrace();
		}
		// 使用finally塊來關閉輸出流、輸入流
		finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}

	
	public static void main(String[] args) {
		
		
		
		Map<String,Object> map = new HashMap<String,Object>();
		//主
		map.put("workNum", "111111");
		map.put("jpName", "周忠宇");
		map.put("depart", "10395-");
		map.put("cardId", "320305199410160415");
		
		
		
		//子1
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		Map<String,Object> map1 = new HashMap<String,Object>();
		map1.put("companyName", "公司1");
		map1.put("post", "崗位名1");
		map1.put("beginDate", "2020-05-05");
		Map<String,Object> map2 = new HashMap<String,Object>();
		map2.put("companyName", "公司2");
		map2.put("post", "崗位名2");
		map2.put("beginDate", "2020-5-5 23:33:32");
		list.add(map1);
		list.add(map2);
		map.put("dbJpExpJSONArray",list);
		
		//子2
		List<Map<String,Object>> list2 = new ArrayList<Map<String,Object>>();
		Map<String,Object> map3 = new HashMap<String,Object>();
		map3.put("department1", "一級部門1");
		map3.put("department2", "二級部門1");
		Map<String,Object> map4 = new HashMap<String,Object>();
		map4.put("department1", "一級部門2");
		map4.put("department2", "二級部門2");
		list2.add(map3);
		list2.add(map4);
		map.put("dbJpOrgJSONArray",list2);
		//子3
		
		
		
		
		ObjectMapper mapper = new ObjectMapper();
		String jsonStr = "";//======================入參入參入參入參入參入參入參入參入參入參入參入參入參入參入參入參入參入參==================================
		try {
			jsonStr = mapper.writeValueAsString(map);
		} catch (JsonProcessingException e1) {
			e1.printStackTrace();
		}
		
		
		
		//訪問接口
		String url = "http://192.168.1.144:9090/jphr/oaInterfaceController.do?fwoaCall";
		String para = "&motion=1&jsonStrData="+jsonStr;
				
		String result = sendPost(url, para);//返回值
		System.out.println(result);
	}

	

}

接收類


import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.util.DateUtilsEx;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.p3.core.utils.common.StringUtils;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeecg.configparam.controller.DbConfigParamController;
import com.jeecg.empblacklist.entity.DbEmpBlacklistEntity;
import com.jeecg.empblacklist.service.DbEmpBlacklistServiceI;
import com.jeecg.empblacklistlog.service.DbEmpBlacklistLogServiceI;
import com.jeecg.jpcontract.entity.DbJpContractEntity;
import com.jeecg.jpemployee.entity.DbJpEmployeeEntity;
import com.jeecg.jpemployee.service.DbJpEmployeeServiceI;
import com.jeecg.jpemployee.util.JSONUtils;
import com.jeecg.jpexp.entity.DbJpExpEntity;
import com.jeecg.jpkqbc.entity.DbJpKqbcEntity;
import com.jeecg.jpmoney.entity.DbJpMoneyEntity;
import com.jeecg.jporg.entity.DbJpOrgEntity;
import com.jeecg.jppost.entity.DbJpPostEntity;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**  
 * 寫入系統接口
 * 
 */
@Controller
@RequestMapping("/oaInterfaceController")
public class OaInterfaceController extends BaseController {

	@Autowired
	private SystemService systemService;

	
	/**
	  * 接口進行入轉調離
	 * fwoaCall 
	  * 動作類型   motion  入職  1 調 動 2 離職 3
	 * //oa 訪問
		String url = "http://"+IP+":"+port+"/jphr/interfaceOAController.do?fwoaCall&";
		String result = sendPost(url, sb.toString());
		System.out.println("返回結果信息:"+ result);
	 */
	@ResponseBody
	@RequestMapping(params = "fwoaCall")
	public String fwoaCall(HttpServletRequest request){
		ObjectMapper mapper = new ObjectMapper();
		Map<String,Object> map = Maps.newHashMap();
		String jsonObjectStr = null;
		boolean flag = true;//
		String resultStr = "成功!";//返回值
		try{
			String motion = request.getParameter("motion");//動作
			if(StringUtils.isNotBlank(motion)) {
				String jsonStrData = request.getParameter("jsonStrData");//數據
				
	            //解析json .........................


			}else {
				flag = false;
				//無動作類型
				resultStr = "無動作類型motion=[入職 1,調 動 2,離職3]";
			}
		}catch(Exception e){
			flag = false;
			resultStr = e +"";
			e.printStackTrace();
		}
		map.put("resultStr",resultStr);
		map.put("flag",flag);
		try {
			jsonObjectStr = mapper.writeValueAsString(map);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return jsonObjectStr;
	}
	
	
	
	
}

 

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