我试图从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

查看:198
本文介绍了我试图从JAVA的IP地址获取位置信息,但我得到一个java.net.SocketException:连接重置错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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
    }

}

这是我的代码snippest.I获取访问者的IP地址,但没有位置信息。
它给出:
java.net.SocketException:在java.net.SocketInputStream.read上重置
(未知源)
在java.net.SocketInputStream.read(Unknown Source )

This is my code snippest.I am getting IP address of visitor but not location information. It gives : java.net.SocketException: Connection reset at java.net.SocketInputStream.read(Unknown Source) at java.net.SocketInputStream.read(Unknown Source)

推荐答案

要告诉java代码,所有的HTTP请求都应该通过代理进行路由,请使用下面的代码片段:

To tell the java code that all HTTP request should be routed through the proxy use the below snippet:

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设置代理主机和端口。身份验证器应该是您的公司用户名和密码。这应该现在工作。

The System.setProperty sets the proxy host and port. The Authenticator should be your corporate username and password. This should work now.

这篇关于我试图从JAVA的IP地址获取位置信息,但我得到一个java.net.SocketException:连接重置错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆