100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Java调用REST接口(get post请求方法)

Java调用REST接口(get post请求方法)

时间:2019-04-04 15:03:44

相关推荐

Java调用REST接口(get post请求方法)

网上的调用方法实例千奇百怪,以下为本人自己整理的Java调用rest接口方法实例,包含get请求和post请求,可创建工具类方便调用,其中get、post请求解决了入出参中文乱码问题。

get方式请求

//get方式请求public String restCallerGet(String path, String param) {//path 接口路径 xxx/xxx/xxx//param 入参 ?xxx=x&xxx=x&xxx=x//接口ipString httpip = "http://127.0.0.1:8080";String data = "";//url拼接String lasturl = httpip + path + param;try{URL url = new URL(lasturl);//打开和url之间的连接HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();//请求头urlConn.setRequestProperty("Accept-Charset", "utf-8");urlConn.setRequestProperty("Content-Type", "application/json; charset=utf-8");urlConn.setDoOutput(true);urlConn.setDoInput(true);urlConn.setRequestMethod("GET");//GET和POST必须全大写urlConn.connect();int code = urlConn.getResponseCode();//获得响应码if(code == 200) {//响应成功,获得响应的数据//InputStream is = urlConn.getInputStream();//得到数据流(输入流)//byte[] buffer = new byte[1024];//int length = 0;//while ((length = is.read(buffer)) != -1) {//String res = new String(buffer, 0, length);//data += res;//}//System.out.println(data);//解决中文乱码BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream(),"UTF-8"));data = reader.readLine();}urlConn.disconnect(); //断开连接}catch (Exception e) {e.printStackTrace();}return data;}

post方式请求

//post方式请求public String restCallerPost(String path, String param) {//path 接口路径 xxx/xxx/xxx//param 入参json {}//接口ipString httpip = "http://127.0.0.1:8080";int responseCode;//String urlParam = "?aaa=1&bbb=2";String urlParam = "";String data = "";//url拼接String lasturl = httpip + path + urlParam;try {URL restURL = new URL(lasturl);HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();conn.setRequestMethod("POST");//请求头conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");conn.setDoOutput(true);//输入流//OutputStream os = conn.getOutputStream();//解决中文乱码OutputStreamWriter os = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");os.write(param);os.flush();// 输出response coderesponseCode = conn.getResponseCode();// 输出responseif(responseCode == 200){//输出流//BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));//解决中文乱码BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));data = reader.readLine();} else {data = "false";}// 断开连接os.close();conn.disconnect();} catch (Exception e) {e.printStackTrace();}return data;}

main方法调用测试

public static void main(String[] args) {// TODO Auto-generated method stub//接口路径String pathGet = "/xxx/xxx/getFunction";String pathPost = "/xxx/xxx/postFunction";//getString paramGet = "?aaa=1&bbb=2";//RestCallerUtil为自行封装的工具类RestCallerUtil rcuGet = new RestCallerUtil();String resultDataGet = rcuGet.restCallerGet(pathGet, paramGet);System.out.println(resultDataGet);//postString paramPost = "{'aaa':'1','bbb':'2'}";//RestCallerUtil为自行封装的工具类RestCallerUtil rcuPost = new RestCallerUtil();String resultDataPost = rcuPost.restCallerPost(pathPost, paramPost);System.out.println(resultDataPost);}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。