1.效果
調用阿里雲的接口 去定位IP地址php
2. 代碼
/** * 1. Java中遠程調用方法 * http://localhost:8080/mavenssm20180519/invokingUrl.action * @Title: invokingUrl * @Description: * @return void * @throws Exception * @throws @date 2018年7月22日 下午11:58:58 */ @RequestMapping("/invokingUrl.action") public void invokingUrl(HttpServletRequest request) throws Exception{ //------------------------java中發送請求---------------開始------------ //記錄登陸着的id信息 //(1)拿到用戶的ip String remoteAddr = request.getRemoteAddr(); //(2) 經過淘寶IP地址庫獲取IP位置 // 動態調用數據庫數據 PrintWriter out = null; BufferedReader in =null; String getData =""; String line; URL url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip="+remoteAddr);//import java.net.URL; URLConnection conn = url.openConnection();// import java.net.URLConnection; //發生post必須設置以下兩行 conn.setDoOutput(true); conn.setDoInput(true); //獲取URLConnection對象對應的輸出流 out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"UTF-8")); out.flush(); //定義 BufferedReader 輸入流的URL響應 in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8")); while((line = in.readLine())!=null){ getData += line; } System.out.println(getData); //------------------------java中發送請求---------------結束------------ } |
2. 在java發送get/post的請求
2.1 utils 類
package cn.mg.kindeditor.utils;java import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection;git public class sendRequestUtils { /** * 1.post方式發送請求 * @param requestUrl * @return * @throws Exception */ public static String sendPostRequest(String requestUrl) throws Exception{ //------------------------java中發送請求---------------開始------------ //記錄登陸着的id信息 //(1)拿到用戶的ip //(2) 經過淘寶IP地址庫獲取IP位置 // 動態調用數據庫數據 PrintWriter out = null; BufferedReader in =null; String getData =""; String line; URL url = new URL(requestUrl);//import java.net.URL; URLConnection conn = url.openConnection();// import java.net.URLConnection; //發生post必須設置以下兩行 conn.setDoOutput(true); conn.setDoInput(true); //獲取URLConnection對象對應的輸出流 out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"UTF-8")); out.flush(); //定義 BufferedReader 輸入流的URL響應 in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8")); while((line = in.readLine())!=null){ getData += line; } System.out.println(getData); return getData; } /** * 2.get方式發送請求 * @param url * @return */ public static String sendGetRequest(String url){ String result = ""; String line; StringBuffer sb=new StringBuffer(); BufferedReader in = null; try { URL realUrl = new URL(url); // 打開和URL之間的鏈接 URLConnection conn = realUrl.openConnection(); // 設置通用的請求屬性 設置請求格式 conn.setRequestProperty("contentType", "utf-8"); conn.setRequestProperty("content-type", "application/x-www-form-urlencoded"); //設置超時時間 conn.setConnectTimeout(60); conn.setReadTimeout(60); // 創建實際的鏈接 conn.connect(); // 定義 BufferedReader輸入流來讀取URL的響應,設置接收格式 in = new BufferedReader(new InputStreamReader( conn.getInputStream(),"utf-8")); while ((line = in.readLine()) != null) { sb.append(line); } result=sb.toString(); } catch (Exception e) { System.out.println("發送GET請求出現異常!" + e); e.printStackTrace(); } // 使用finally塊來關閉輸入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } } 數據庫 |
2.2 調用(應用)
2.2.1 post
效果:app
2.2.2 get
效果:maven
3. 附件
碼雲:post
https://gitee.com/Luck_Me/javaToGetOrPost/tree/master阿里雲
百度雲:url
連接:https://pan.baidu.com/s/1FJSngPT72DwNTrgsWV-cLg
提取碼:7tv2 spa