码迷,mamicode.com
首页 > 编程语言 > 详细

java后台发送请求并获取返回值

时间:2018-01-11 23:48:51      阅读:559      评论:0      收藏:0      [点我收藏+]

标签:pre   autot   keep   width   catch   key   auto   head   user   

项目中需要前端发送请求给后端,而后端需要从另一个平台中取数据然后再透传给前端,通过下述代码将其实现.在此记录一下.

package com.autotest.utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
public class BackEndHttpRequest{
	/**
	 * 向指定的URL发送GET方法的请求
	 * @param url    发送请求的URL
	 * @param param  请求参数,请求参数应该是 name1=value1&name2=value2 的形式 
	 * @return       远程资源的响应结果 
	 */
	public static String sendGet(String url, String param) {
		String result = "";
		BufferedReader bufferedReader = null;
		try {
			//1、读取初始URL
			String urlNameString = url + "?" + param;
			//2、将url转变为URL类对象
			URL realUrl = new URL(urlNameString);
			
			//3、打开和URL之间的连接
			URLConnection connection = realUrl.openConnection();
			//4、设置通用的请求属性
			connection.setRequestProperty("accept", "*/*");
			connection.setRequestProperty("connection", "Keep-Alive");
			connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			//connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
			
			//5、建立实际的连接 
			connection.connect();
			//获取所有响应头字段
			Map<String, List<String>> map = connection.getHeaderFields();
			//遍历所有的响应头字段
			for(String key : map.keySet()) {
				System.out.println(key + "---->" + map.get(key));
			}
			
			//6、定义BufferedReader输入流来读取URL的响应内容 ,UTF-8是后续自己加的设置编码格式,也可以去掉这个参数
			bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
			String line = "";
			while(null != (line = bufferedReader.readLine())) {
				result += line;
			}
//			int tmp;
//            while((tmp = bufferedReader.read()) != -1){
//                result += (char)tmp;
//            }
			
		}catch (Exception e) {
			// TODO: handle exception
			System.out.println("发送GET请求出现异常!!!"  + e);
			e.printStackTrace();
		}finally {        //使用finally块来关闭输入流 
			try {
				if(null != bufferedReader) {
					bufferedReader.close();
				}
			}catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
		return result;
	}
	/**
	 * 向指定的URL发送POST方法的请求
	 * @param url    发送请求的URL
	 * @param param  请求参数,请求参数应该是 name1=value1&name2=value2 的形式 
	 * @return       远程资源的响应结果 
	 */
	public static String sendPost(String url, String param) {
		String result = "";
		BufferedReader bufferedReader = null;
		PrintWriter out = null;
		try {
			//1、2、读取并将url转变为URL类对象
			URL realUrl = new URL(url);
			
			//3、打开和URL之间的连接
			URLConnection connection = realUrl.openConnection();
			//4、设置通用的请求属性
			connection.setRequestProperty("accept", "*/*");
			connection.setRequestProperty("connection", "Keep-Alive");
			connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
		
			// 发送POST请求必须设置如下两行  
			connection.setDoInput(true);
			connection.setDoOutput(true);
			
			//5、建立实际的连接
			//connection.connect();
			//获取URLConnection对象对应的输出流
			out = new PrintWriter(connection.getOutputStream());
			//发送请求参数
			out.print(param);
			//flush输出流的缓冲
			out.flush();
			//
			
			//6、定义BufferedReader输入流来读取URL的响应内容
			bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
			String line;
			while(null != (line = bufferedReader.readLine())) {
				result += line;
			}
		}catch (Exception e) {
			// TODO: handle exception
			System.out.println("发送POST请求出现异常!!!"  + e);
			e.printStackTrace();
		}finally {        //使用finally块来关闭输出流、输入流 
			try {
				if(null != out) {
					out.close();
				}
				if(null != bufferedReader) {
					bufferedReader.close();
				}
			}catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
		return result;
	}
}

调用方法:

    public static void main(String[] args) {
        //发送 GET 请求
        String str1=BackEndHttpRequest.sendGet("http://localhost/services/getallcase/", "key=123&v=456");
        System.out.println(str1);
        
        //发送 POST 请求
        String str2=BackEndHttpRequest.sendPost("http://localhost/services/getallcase/", "key=123&v=456");
        System.out.println(str2);
    }

java后台发送请求并获取返回值

标签:pre   autot   keep   width   catch   key   auto   head   user   

原文地址:https://www.cnblogs.com/zhangwuji/p/8270693.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!