|
@@ -1,6 +1,12 @@
|
|
package com.sooka.utils;
|
|
package com.sooka.utils;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
|
+import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
@@ -10,14 +16,16 @@ import java.net.MalformedURLException;
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
import java.net.URLConnection;
|
|
import java.net.URLConnection;
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
|
|
+import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
+import java.util.Set;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
public class HttpUtil {
|
|
public class HttpUtil {
|
|
|
|
|
|
- public static String doGet(String geturl, String params) {
|
|
|
|
|
|
+ public static String doGet(String geturl, String params,Map headerparam) {
|
|
String realUrl = geturl + "?" + params;
|
|
String realUrl = geturl + "?" + params;
|
|
System.out.println(realUrl);
|
|
System.out.println(realUrl);
|
|
|
|
|
|
@@ -38,9 +46,23 @@ public class HttpUtil {
|
|
conn.setRequestProperty("connection", "Keep-Alive");
|
|
conn.setRequestProperty("connection", "Keep-Alive");
|
|
|
|
|
|
conn.setRequestProperty("contentType", "UTF-8");
|
|
conn.setRequestProperty("contentType", "UTF-8");
|
|
|
|
+ conn.setRequestProperty("content-Type", "application/json;charset=UTF-8");
|
|
// 3.使用 connect 方法建立到远程对象的实际连接�?
|
|
// 3.使用 connect 方法建立到远程对象的实际连接�?
|
|
- conn.connect();
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
+ if(headerparam != null){
|
|
|
|
+ Set<String> keys =headerparam.keySet();
|
|
|
|
+ Iterator<String> iterator = keys.iterator();
|
|
|
|
+ while(iterator.hasNext()){
|
|
|
|
+ String key =iterator.next().toString();
|
|
|
|
+ System.out.println("=======key======="+key);
|
|
|
|
+ Object value = headerparam.get(key);
|
|
|
|
+ System.out.println("=======value======="+value);
|
|
|
|
+ String v = value.toString();
|
|
|
|
+ conn.setRequestProperty(key,v);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ conn.connect();
|
|
// 4.远程对象变为可用。远程对象的头字段和内容变为可访问�??
|
|
// 4.远程对象变为可用。远程对象的头字段和内容变为可访问�??
|
|
// 4.1获取响应的头字段
|
|
// 4.1获取响应的头字段
|
|
Map<String, List<String>> headers = conn.getHeaderFields();
|
|
Map<String, List<String>> headers = conn.getHeaderFields();
|
|
@@ -71,7 +93,7 @@ public class HttpUtil {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- public static String doPost(String url, String param) {
|
|
|
|
|
|
+ public static String doPost(String url, String param,Map headerparam) {
|
|
PrintWriter out = null;
|
|
PrintWriter out = null;
|
|
BufferedReader in = null;
|
|
BufferedReader in = null;
|
|
String result = "";
|
|
String result = "";
|
|
@@ -86,6 +108,18 @@ public class HttpUtil {
|
|
conn.setRequestProperty("content-Type", "application/json;charset=UTF-8");
|
|
conn.setRequestProperty("content-Type", "application/json;charset=UTF-8");
|
|
|
|
|
|
//
|
|
//
|
|
|
|
+ if(headerparam != null){
|
|
|
|
+ Set<String> keys =headerparam.keySet();
|
|
|
|
+ Iterator<String> iterator = keys.iterator();
|
|
|
|
+ while(iterator.hasNext()){
|
|
|
|
+ String key =iterator.next().toString();
|
|
|
|
+ System.out.println("=======key======="+key);
|
|
|
|
+ Object value = headerparam.get(key);
|
|
|
|
+ System.out.println("=======value======="+value);
|
|
|
|
+ String v = value.toString();
|
|
|
|
+ conn.setRequestProperty(key,v);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
//
|
|
//
|
|
|
|
|
|
|
|
|
|
@@ -114,7 +148,7 @@ public class HttpUtil {
|
|
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
|
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
|
String line;
|
|
String line;
|
|
while ((line = in.readLine()) != null) {
|
|
while ((line = in.readLine()) != null) {
|
|
- result += "/n" + line;
|
|
|
|
|
|
+ result += "" + line;
|
|
}
|
|
}
|
|
System.out.println("result========"+result);
|
|
System.out.println("result========"+result);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -204,6 +238,44 @@ public class HttpUtil {
|
|
return os + " --- " + browser;
|
|
return os + " --- " + browser;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public static String getIPAddress(HttpServletRequest request) {
|
|
|
|
+ String ip = null;
|
|
|
|
+
|
|
|
|
+ //X-Forwarded-For:Squid 服务代理
|
|
|
|
+ String ipAddresses = request.getHeader("X-Forwarded-For");
|
|
|
|
+
|
|
|
|
+ if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
|
|
|
|
+ //Proxy-Client-IP:apache 服务代理
|
|
|
|
+ ipAddresses = request.getHeader("Proxy-Client-IP");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
|
|
|
|
+ //WL-Proxy-Client-IP:weblogic 服务代理
|
|
|
|
+ ipAddresses = request.getHeader("WL-Proxy-Client-IP");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
|
|
|
|
+ //HTTP_CLIENT_IP:有些代理服务器
|
|
|
|
+ ipAddresses = request.getHeader("HTTP_CLIENT_IP");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
|
|
|
|
+ //X-Real-IP:nginx服务代理
|
|
|
|
+ ipAddresses = request.getHeader("X-Real-IP");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //有些网络通过多层代理,那么获取到的ip就会有多个,一般都是通过逗号(,)分割开来,并且第一个ip为客户端的真实IP
|
|
|
|
+ if (ipAddresses != null && ipAddresses.length() != 0) {
|
|
|
|
+ ip = ipAddresses.split(",")[0];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //还是不能获取到,最后再通过request.getRemoteAddr();获取
|
|
|
|
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
|
|
|
|
+ ip = request.getRemoteAddr();
|
|
|
|
+ }
|
|
|
|
+ return ip;
|
|
|
|
+ }
|
|
|
|
+
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
String s = "云南";
|
|
String s = "云南";
|
|
s = URLEncoder.encode(s);
|
|
s = URLEncoder.encode(s);
|
|
@@ -214,7 +286,37 @@ public class HttpUtil {
|
|
|
|
|
|
HttpUtil httpUtil = new HttpUtil();
|
|
HttpUtil httpUtil = new HttpUtil();
|
|
// String str ="http://www.sohu.com";
|
|
// String str ="http://www.sohu.com";
|
|
- httpUtil.doGet(str, parm);
|
|
|
|
|
|
+ httpUtil.doGet(str, parm,null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public String doPost(String url, String map, String charset,Map headerParams) {
|
|
|
|
+ org.apache.http.client.HttpClient httpClient = null;
|
|
|
|
+ HttpPost httpPost = null;
|
|
|
|
+ String result = null;
|
|
|
|
+ try {
|
|
|
|
+ httpClient = new DefaultHttpClient();
|
|
|
|
+ httpPost = new HttpPost(url);
|
|
|
|
+ // 设置参数
|
|
|
|
+ httpPost.addHeader("Accept", "application/json");
|
|
|
|
+ httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
|
|
|
|
+ StringEntity stringEntity = new StringEntity(map);
|
|
|
|
+ stringEntity.setContentEncoding("UTF-8");
|
|
|
|
+ stringEntity.setContentType("application/json");
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ httpPost.setEntity(stringEntity);
|
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
|
+ if (response != null) {
|
|
|
|
+ HttpEntity resEntity = response.getEntity();
|
|
|
|
+ if (resEntity != null) {
|
|
|
|
+ result = EntityUtils.toString(resEntity, charset);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
+ ex.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|