java后台跨服务器请求方法

xiaoxiao2021-02-28  81

public static String getRequestResult(String newUrl,String type,Map<String, Object> map) throws IOException { InputStream inputStream = null; InputStreamReader reader = null; BufferedReader bufferedReader = null; StringBuffer resultBuffer = new StringBuffer(); String tempLine = null; try { // 统一资源 URL url = new URL(newUrl); // http的连接类 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设定请求的方法,默认是GET connection.setRequestMethod(type); // 设置是否向connection输出 connection.setDoOutput(true); // 设置是否从connection读入,默认情况下是true; connection.setDoInput(true); connection.setConnectTimeout(3000); // Post 请求不能使用缓存 connection.setUseCaches(false); // 设置字符编码连接参数 connection.setRequestProperty("Connection", "Keep-Alive"); // 设置字符编码 connection.setRequestProperty("Charset", "UTF-8"); if(map!=null&&map.size()>0){ //发送请求参数 DataOutputStream out = new DataOutputStream(connection.getOutputStream()); StringBuffer buffer = new StringBuffer(); for (Entry<String, Object> entry : map.entrySet()) { Object params = entry.getValue(); Object name = entry.getKey(); buffer.append(name+"="+params+"&"); } String param = buffer.toString(); param = param.substring(0,param.length()-1); out.writeBytes(param); out.flush(); out.close(); } inputStream = connection.getInputStream(); reader = new InputStreamReader(inputStream); bufferedReader = new BufferedReader(reader); while ((tempLine = bufferedReader.readLine()) != null) { resultBuffer.append(tempLine); resultBuffer.append("\n"); } } catch (IOException e) { e.printStackTrace(); }finally { if(bufferedReader!=null){ try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } if(reader!=null){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } if(inputStream!=null){ try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return resultBuffer.toString(); }

方法二

import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ProjectPathUtils { public static String getProjectPath(String type){ InputStream in = ProjectPathUtils.class.getResourceAsStream("/config/properties/projectPath.properties"); Properties prop = new Properties(); try { prop.load(in); String property = prop.getProperty(type); return property; } catch (IOException e) { e.printStackTrace(); }finally { try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return type; } }

propertis数据

getItem=http://192.168.1.151/item/getItem.action

httpUtil

/** * * @param url : 请求的url地址 * @param parameterMap: 请求参数 * @return */ public static String sendRequest(String url, Map<String,Object> parameterMap) throws IOException { String msg = null; HttpPost httpPost = new HttpPost(url); HttpEntity reqEntity = null; CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; if(parameterMap!=null && parameterMap.size()>0){ MultipartEntityBuilder builder = MultipartEntityBuilder.create(); for (Map.Entry<String,Object> entry:parameterMap.entrySet()) { Class clazz = entry.getValue().getClass(); if(clazz == String.class){ StringBody param = new StringBody( entry.getValue().toString(),ContentType.create("text/plain", Consts.UTF_8)); builder.addPart(entry.getKey(),param); } } reqEntity = builder.build(); httpPost.setEntity(reqEntity); } response = httpClient.execute(httpPost); try { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { msg = EntityUtils.toString(resEntity, Charset.forName("UTF-8")); } EntityUtils.consume(resEntity); } finally { response.close(); httpClient.close(); } return msg; }

调用

@RequestMapping(value="/item/getItem",method=RequestMethod.POST,produces = "application/json; charset=utf-8") String projectPath = ProjectPathUtils.getProjectPath("getItem"); String requestResult = HttpUtil.sendRequest(projectPath, null); return requestResult;
转载请注明原文地址: https://www.6miu.com/read-39043.html

最新回复(0)