我正在尝试从 JAVA 中的 ip 地址获取位置信息,但我收到 java.net.SocketException: Connection reset error
Posted
技术标签:
【中文标题】我正在尝试从 JAVA 中的 ip 地址获取位置信息,但我收到 java.net.SocketException: Connection reset error【英文标题】:I am trying to get location information from ip address in JAVA but I am getting a java.net.SocketException: Connection reset error 【发布时间】:2014-04-25 04:57:16 【问题描述】:import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javaQuery.importClass.javaQueryBundle;
import javaQuery.j2ee.GeoLocation;
/**
* Servlet implementation class IP
*/
public class IP extends HttpServlet
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public IP()
super();
// TODO Auto-generated constructor stub
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
// TODO Auto-generated method stub
String ipAddress = request.getRemoteAddr();
System.out.println(ipAddress);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//Print out the IP address of the caller
out.println(request.getRemoteAddr());
GeoLocation $gl = javaQueryBundle.createGeoLocation();
System.out.println(ipAddress);
$gl.MAPTargetByIP(ipAddress, "test");
System.out.println($gl.Latitude);
System.out.println($gl.Longitude);
System.out.println($gl.Country);
System.out.println($gl.City);
System.out.println($gl.State);
System.out.println($gl.GoogleMap_URL);
System.out.println($gl.GoogleMap_URL_Bubble);
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
// TODO Auto-generated method stub
这是我的代码片段。我正在获取访问者的 IP 地址,但不是位置信息。 它给 : java.net.SocketException:连接重置 在 java.net.SocketInputStream.read(未知来源) 在 java.net.SocketInputStream.read(Unknown Source)
【问题讨论】:
您在防火墙后面吗?还是您使用代理访问互联网? 是的..但我在禁用防火墙设置后尝试了这个。但仍然是同样的问题。我正在使用我的组织代理访问互联网 啊,我明白了您的问题,您无法访问的原因是您被组织代理阻止了。您需要在代码中解决它。 【参考方案1】:要告诉java代码所有的HTTP请求都应该通过代理路由,使用下面的sn-p:
System.setProperty("http.proxyHost", "proxyHost");
System.setProperty("http.proxyPort", "proxyPort");
Authenticator authenticator = new Authenticator()
public PasswordAuthentication getPasswordAuthentication()
return (new PasswordAuthentication("USERNAME","PASSWORD".toCharArray()));
;
Authenticator.setDefault(authenticator);
System.setProperty 设置代理主机和端口。 Authenticator 应该是您的公司用户名和密码。现在应该可以了。
【讨论】:
【参考方案2】:import java.io.IOException;
import java.io.PrintWriter;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.model.*;
import com.maxmind.geoip2.record.Location;
import com.maxmind.geoip2.record.MaxMind;
import com.maxmind.geoip2.record.RepresentedCountry;
import javaQuery.importClass.javaQueryBundle;
import javaQuery.j2ee.GeoLocation;
/**
* Servlet implementation class IP
*/
public class IP extends HttpServlet
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public IP()
super();
System.setProperty("http.proxyHost", "proxyHostName");
System.setProperty("http.proxyPort", "proxyPort");
Authenticator authenticator = new Authenticator()
public PasswordAuthentication getPasswordAuthentication()
return (new PasswordAuthentication("USERNAME","PASSWORD".toCharArray()));
;
Authenticator.setDefault(authenticator);
// TODO Auto-generated constructor stub
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
// TODO Auto-generated method stub
String ipAddress = request.getRemoteAddr();
System.out.println(ipAddress);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//Print out the IP address of the caller
out.println(request.getLocalAddr());
GeoLocation $gl = javaQueryBundle.createGeoLocation();
System.out.println($gl.toString().length());
System.out.println("--");
System.out.println($gl.getMACAddressWindows());
$gl.MAPTargetByIP(ipAddress , "test");
System.out.println($gl.Latitude);
System.out.println($gl.Longitude);
System.out.println($gl.Country);
System.out.println($gl.City);
System.out.println($gl.State);
System.out.println($gl.GoogleMap_URL);
System.out.println($gl.GoogleMap_URL_Bubble);
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
// TODO Auto-generated method stub
现在一切正常。
【讨论】:
以上是关于我正在尝试从 JAVA 中的 ip 地址获取位置信息,但我收到 java.net.SocketException: Connection reset error的主要内容,如果未能解决你的问题,请参考以下文章