http请求工具类
博客专区 > GTI 的博客 > 博客详情
http请求工具类
GTI 发表于1年前
http请求工具类
  • 发表于 1年前
  • 阅读 10
  • 收藏 1
  • 点赞 0
  • 评论 1

腾讯云 十分钟定制你的第一个小程序>>>   

package an.you.common.http;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
/**
 * http请求工具类
 * @author anyou
 *
 */
public class BaseUrlUtil {
	/**
	 * 封装请求地址的前部分
	 * @param request
	 * @return
	 */
	public static String getBaseUrl(HttpServletRequest request){
		String path = request.getContextPath();
		int port = request.getServerPort();
		String _port = ":"+port;
		if(port == 80 || port == 443)_port = "";
		String basePath = request.getScheme()+"://"+request.getServerName()+_port+path+"/";
		return basePath;
	}
	/**
	 * 获取请求地址的参数,多个参数以,分割
	 * @param request
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static String getPaream(HttpServletRequest request){
		Map map = request.getParameterMap();
		String returnStr = "";
		Iterator entries = map.entrySet().iterator(); 
		Map.Entry entry;
		while (entries.hasNext()) {
			entry = (Map.Entry) entries.next(); 
			String name = (String) entry.getKey(); 
			Object valueObj = entry.getValue();
			String value = "";
			if(valueObj instanceof String[]){ 
				String[] values = (String[])valueObj;
				for(int i=0;i<values.length;i++){ 
					 value = values[i] + ",";
				}
				value = value.substring(0, value.length()-1); 
			}else{
				value = valueObj.toString(); 
			}
			
			returnStr += name + "=" + value +"&";
		}
		if(returnStr.length()>0){
			returnStr = returnStr.substring(0, returnStr.length()-1);
		}
		return returnStr;
	}
	/**
	 * 获取客户端请求的IP地址
	 * @param request
	 * @return
	 */
	public static String getRequestIP(HttpServletRequest request){
		String spBillCreateIP = "";
		if (request.getHeader("x-forwarded-for") == null) {
			spBillCreateIP = request.getRemoteAddr();
		} else {
			String _ip = request.getHeader("x-forwarded-for");
			String[] str = _ip.replace(" ", "").split(",");
			if (str.length > 0) {
				spBillCreateIP = str[0];
			} else {
				spBillCreateIP = request.getHeader("x-forwarded-for");
			}

		}
		return spBillCreateIP;
	}
	/**
	 * 获取访问的浏览器和操作系统
	 * @param request
	 * @return
	 */
	public static String[] getRequestUseragent(HttpServletRequest request){
		String[] strs = new String[2];
		String brower = null;
		String userOs = null;
		String agent = request.getHeader("user-agent");
		agent = agent.substring(agent.indexOf("(")+1, agent.indexOf(")"));
		String[] _agents = agent.split(";");
		if(agent.contains("iPhone")){
			brower = "Safari";
			userOs = "iPhone";
		}else if(agent.contains("MSIE")){
			brower = _agents[1];
			userOs = _agents[2];
		}else if(agent.contains("Linux")){
			if(_agents[1].contains("Android"))userOs = _agents[1];
			else userOs = _agents[2];
			brower = _agents[_agents.length-1];
		}else{
			brower = _agents[0];
			userOs = _agents[0];
		}
		strs[0] = brower;
		strs[1] = userOs;
		return strs;
	}
}

 

共有 人打赏支持
粉丝 9
博文 31
码字总数 34993
评论 (1)
GTI
自顶一个
×
GTI
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: