短信發送平臺 以及ip地址獲取

這裏的短信發送是須要本身花錢買短信發送條數的java

這裏分享一個在阿里雲上購買的一家短信發送方法api

/**
	 * 短信內容發送
	 */
	public static String getMessageTextAlerts(Map<String, Object> map) {
		//這裏的map主要包含手機號
		String phone = map.get("phone").toString();
		
	    String host = "http://mobai.market.alicloudapi.com";
	    String path = "/mobai_sms";
	    String method = "POST";
        //買了短信服務以後會給你一個appcode
	    String appcode = "xxxxxxxxx1984ed3900ffd3xxxxxxxxx";
	    Map<String, String> headers = new HashMap<String, String>();
	    headers.put("Authorization", "APPCODE " + appcode);
	    Map<String, String> querys = new HashMap<String, String>();
	    
        //隨機數本身生成
	    Random random = new Random();
	    String num = random.nextInt(10000)+"";
	    if(num.length() <= 3) {
	    	num = num + 1;
	    }
	    System.out.println("num------->"+num);
	    num = "code:"+num;
	    
	    querys.put("param", num);
	    querys.put("phone", phone);
	    querys.put("templateId", "TP1801293");
	    Map<String, String> bodys = new HashMap<String, String>();

	    try {
	    	HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
	    	System.out.println("response.toString()===>"+response.toString());
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
	    return num;
	}

 

附加一個ip地址的獲取方法,有時候登陸註冊須要一些邏輯判斷app

/** 
	* 獲取用戶真實IP地址,不使用request.getRemoteAddr();的緣由是有可能用戶使用了代理軟件方式避免真實IP地址, 
	* 
	* 但是,若是經過了多級反向代理的話,X-Forwarded-For的值並不止一個,而是一串IP值,究竟哪一個纔是真正的用戶端的真實IP呢? 
	* 答案是取X-Forwarded-For中第一個非unknown的有效IP字符串。 
	* 
	* 如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130, 
	* 192.168.1.100 
	* 
	* 用戶真實IP爲: 192.168.1.110 
	* 
	* @param request 
	* @return 
	*/
	public String getIp(HttpServletRequest request) {
		String ipAddress = null;
		//ipAddress = this.getRequest().getRemoteAddr();
		ipAddress = request.getHeader("x-forwarded-for");
		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = request.getHeader("Proxy-Client-IP");
		}
		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = request.getRemoteAddr();
			if (ipAddress.equals("127.0.0.1")) {
				//根據網卡取本機配置的IP
				InetAddress inet = null;
				try {
					inet = InetAddress.getLocalHost();
				} catch (UnknownHostException e) {
					e.printStackTrace();
				}
				ipAddress = inet.getHostAddress();
			}
	
		}
	
		return ipAddress;
	}
相關文章
相關標籤/搜索