繁体   English   中英

我正在尝试从JAVA中的IP地址获取位置信息,但是却遇到了java.net.SocketException:连接重置错误

[英]I am trying to get location information from ip address in JAVA but I am getting a java.net.SocketException: Connection reset error

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(未知源)处的连接重置

要告诉Java代码应通过代理路由所有HTTP请求,请使用以下代码段:

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应该是您的公司用户名和密码。 现在应该可以使用了。

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
    }

}

现在工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM