本文目录一览:
- 1、java HttpClient设置代理的话,用户名和密码怎么设置?
- 2、java如何获取用户真实的ip
- 3、Java 编写IP动态代理
- 4、java通过代理ip方式访问接口返回为null
- 5、java中如何获取用户的IP地址及禁止此人
- 6、java程序如何绑定服务器IP?
java HttpClient设置代理的话,用户名和密码怎么设置?
使用代理需要导入:commons-logging-1.1.jar,httpclient-4.0-beta2.jar ,httpcore-4.1-alpha1.jar 和 commons-codec-1.4.jar架包。
在连接代理时需要使用用户名和密码构造UsernamePasswordCredentials对象并作为参数传递给HttpClient对象。
具体用法如下:
public static void main(String args[])
{
StringBuffer sb = new StringBuffer();
//创建HttpClient实例
HttpClient client = getHttpClient();
//创建httpGet
HttpGet httpGet = new HttpGet("");
//执行
try {
HttpResponse response = client.execute(httpGet);
HttpEntity entry = response.getEntity();
if(entry != null)
{
InputStreamReader is = new InputStreamReader(entry.getContent());
BufferedReader br = new BufferedReader(is);
String str = null;
while((str = br.readLine()) != null)
{
sb.append(str.trim());
}
br.close();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(sb.toString());
}
//设置代理
public static HttpClient getHttpClient() {
DefaultHttpClient httpClient = new DefaultHttpClient();
String proxyHost = "proxycn2.huawei.com";
int proxyPort = 8080;
String userName = "china\\******";
String password = "*******“
httpClient.getCredentialsProvider().setCredentials(
new AuthScope(proxyHost, proxyPort),
new UsernamePasswordCredentials(userName, password));
HttpHost proxy = new HttpHost(proxyHost,proxyPort);
httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
return httpClient;
}
java如何获取用户真实的ip
1、如果服务器如果没有采用反向代理,而且客户端没有用正向代理的话,那么可以获取客户端的真实IP地址request.getRemoteAddr()
2、如果服务器如果没有采用反向代理,而且客户端有用正向代理的话,那么通过request.getRemoteAddr()获取客户端的IP地址是客户端 的代理服务器的地址,并不是客户端的真实地址,
3、如果客户端使用的是多层代理的话,服务器获得的客户端地址是客户端的最外围代理服务器的地址如果服务器如果采用反向代理服务器,不管客户端采用的是何种方式访问服务器。
//获得客户端真实IP地址的方法一:
public String getRemortIP(HttpServletRequest request) {
if (request.getHeader("x-forwarded-for") == null) {
return request.getRemoteAddr();
}
return request.getHeader("x-forwarded-for");
}
//获得客户端真实IP地址的方法二:
public String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
Java 编写IP动态代理
package com.tan.test;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.log4j.Logger;
public class TestProxyIp {
private static final Logger log = Logger.getLogger(TestProxyIp.class);
public static void main(String[] args) throws IOException {
System.setProperty("http.maxRedirects", "50");
System.getProperties().setProperty("proxySet", "true");
// 如果不设置,只要代理IP和代理端口正确,此项不设置也可以
String ip = "59.175.192.126";
ip = "221.214.180.130";
ip = "122.224.171.91";
ip = "58.221.213.166";
ip = "202.106.16.36";
ip = "121.8.191.34";
ip = "222.208.242.30";
ip = "219.239.90.85";
ip = "60.31.177.188";
System.getProperties().setProperty("http.proxyHost", ip);
System.getProperties().setProperty("http.proxyPort", "3128");
//确定代理是否设置成功
log.info(getHtml(""));
//log.info(getHtml(""));
}
private static String getHtml(String address){
StringBuffer html = new StringBuffer();
String result = null;
try{
URL url = new URL(address);
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0; NT 5.1; GTB5; .NET CLR 2.0.50727; CIBA)");
BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
try{
String inputLine;
byte[] buf = new byte[4096];
int bytesRead = 0;
while (bytesRead = 0) {
inputLine = new String(buf, 0, bytesRead, "UTF-8");
/*if (!"".equals(inputLine)) {
System.out.println(inputLine);
}*/
html.append(inputLine);
bytesRead = in.read(buf);
inputLine = null;
}
buf = null;
}finally{
in.close();
conn = null;
url = null;
}
//result = new String(html.toString().trim().getBytes("ISO-8859-1"), "UTF-8").toLowerCase();
//result=new String(html.toString().trim().getBytes("ISO-8859-1"), "GBK");
}catch (Exception e) {
e.printStackTrace();
return null;
}/*finally{
html = null;
}*/
return html.toString();
}
}
但是找不到有用的动态ip。
java通过代理ip方式访问接口返回为null
自动报错。null是Java中的关键字。null==0x0表示内存中编号为0的地址。该地址受到系统保护,任何程序读取,写入0x0地址,系统直接杀死程序会自动报错。一般用于在开发中初始化引用数据类型的变量,利用null报错。NullPointerException。null是一种特殊的值,可以把null赋予任何引用类型,也可以把null转化成任何类型。但是仅仅限于引用类型,不包括基本数据类型。
java中如何获取用户的IP地址及禁止此人
楼上的方法是不合理的。第一,如果用户使用代理,是无法获得真实Ip的0;第二,IF语句不能保证禁止IP,而且很多网站并不需要登录的。给楼主些方法,可供参考。
1。 获取用户IP
public String getRemortIP(HttpServletRequest request) {
if (request.getHeader("x-forwarded-for") == null) {
return request.getRemoteAddr();
}
return request.getHeader("x-forwarded-for");
}
这种方法总是获得用户的真实IP
2。禁止IP,可以考虑从服务器下手
(1) 如果用的是tomcat,那么在tomcat_home/conf/server.xml中的host/host之间加一行代码:
Valve className="org.apache.catalina.valves.RemoteAddrValve" deny="192.168.1.1"/
这样192.168.1.1这个ip访问webapp时会收到 403 错误
(2) 如果是apache那么:
Directory /var/web/dir1
Allow from all
Deny from 111.111.111.111
/Directory
上面限制单个IP,限制IP段用*号代替。如111.111.111.*
(3) 或者楼主也可以像楼上说的用代码实现,给你个JS代码:
SCRIPT LANGUAGE="java script"
!-- Begin
netscape = (navigator.appName.indexOf("Netscape") != -1);
version4 = (navigator.appVersion.indexOf("4.") != -1);
if (netscape version4) {
ip = "" + java.net.InetAddress.getLocalHost().getHostAddress();
if (ip.indexOf("235.12") = -1)
// 这是想要禁止访问的IP例如: 235.12.xxx.xxx
{
alert("You are not permitted to access this site.");
history.go(-1);
}
}
// End --
/script
将以上代码加入到head区域中。
java程序如何绑定服务器IP?
你把这个方法写进你的程序,程序开始后先获取IP,然后判断IP是否和你的一致,如果不一致直接return;即可.
/**
* 获取外网IP
* @param request
* @return
*/
public static String getIpAddr(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();
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null ipAddress.length() 15) { // "***.***.***.***".length()
// = 15
if (ipAddress.indexOf(",") 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
return ipAddress;
}
————————————————
版权声明:本文为CSDN博主「秋9」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接: