远程调用服务器方法:POST方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lixin2151408/article/details/71419885
通过调用远程服务器的接口,通过本服务器来操作远程服务器

此方法:主要关键是参数map的转化:map转化成String 时候,需要着重注意红色标记部分。转化成通过“&”连接的字符串。
(这个方法是我抄公司一个老师写的..因为我百度到的很多方法都不能用..)
controller层:
@RequestMapping(value = "ywjk/jgqlsavetablepost",method = RequestMethod.POST)
@ResponseBody
public String jgqlsavetablepost(HttpServletRequest request,HttpServletResponse response)throws Exception{
String c = request.getParameter("c");
configsetService.jgconfigshi("c", c, "jgqlsavetableforpost");
return "添加成功!";
}


因为controller层是通过servlet去获取前台传送的数据,因此在service层传map时,需要将前台传入的数据置入新建的map中:
而controller层的方法仅用于操作远程服务器用;需另写一个控制器去获取数据并同时调用此方法;
service层:
public void jgconfigshi(String mapname,String jgConfigJson,String urlend) {
	String sql = "select ip from xzqh where IP is not null and ip<>':8080'";
	List list = commonDao.getDataListByFullSql(sql);
	Map map = new HashMap();
	if(list.size()!=0){
	Map map2 = new HashMap();
	for(int i=0;i<list.size();i++){
		map = (Map)list.get(i);
		String ip = map.get("IP").toString();
		ip = "http://"+ip+"/sharesearch/app/search/model/ywjk/"+urlend+"";
		System.err.println(ip);
		map2.clear();
		map2.put(mapname, jgConfigJson);
		SmOthersService.sendPostRequest(ip, map2);
	}
	map2.clear();
	}
}
/**
	 * 
	 * @param urlstr     调用接口的URL
	 * @param _urlParam	  调用接口需要的map
	 * @return
	 */
	public static String sendPostRequest(String urlstr, Map<String, String> _urlParam) {
		String strresponse = null;
		try {
			URL url = new URL(urlstr);
			String urlParamStr = "";
			for (Entry<String, String> entry : _urlParam.entrySet()) {
				urlParamStr += entry.getKey() + "=" + URLEncoder.encode(entry.getValue().toString().trim(), "utf-8") + "&";
			}
			HttpURLConnection connet = (HttpURLConnection) url.openConnection();			
			connet.setConnectTimeout(5000);
			connet.setReadTimeout(30000000);
			connet.setDoOutput(true);
			connet.setDoInput(true);
			connet.setUseCaches(false);
			connet.setRequestMethod("POST");					
			connet.setRequestProperty("Accept-Charset", "UTF-8");
			connet.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
			DataOutputStream doutstream = new DataOutputStream(connet.getOutputStream());
			doutstream.writeBytes(urlParamStr);
			if (connet.getResponseCode() != 200) {
				throw new IOException(connet.getResponseMessage());
			}
			BufferedReader brd = new BufferedReader(new InputStreamReader(connet.getInputStream(), "utf-8"));

			StringBuilder sb = new StringBuilder();
			String line;
			while ((line = brd.readLine()) != null) {
				sb.append(line);
			}
			brd.close();doutstream.close();
			strresponse = sb.toString();
			System.out.println(sb.toString());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return strresponse;
	}


猜你喜欢

转载自blog.csdn.net/lixin2151408/article/details/71419885