使用Java获取HTTP请求信息

时间:2014-09-03 08:39:17

标签: java google-app-engine

我想知道如何从Java获取HTTP请求信息,我使用的是Restlet框架,但我不想用它来限制它。因为我一直在努力用restlet来解决它,但我不能。我附上了我的GAE控制台日志的屏幕截图,

enter image description here

我需要获得locahost(域名),在实际生产环境中可能是abc.com或xyz.com。

这是我为此应用程序提供的restlet代码:

    Redirector proxy = new Redirector(getContext(), target,
            Redirector.MODE_SERVER_OUTBOUND){

        String target = "https://dl.x.com/u/123/";

        @Override
        public void handle(Request request, Response response) {
            String path = request.getResourceRef().getHostIdentifier() +
                request.getResourceRef().getPath();
            try {
                URL url = new URL(path);
                String host = url.getHost();
                String pathParts = url.getPath();
                if(pathParts.isEmpty() || pathParts.equals(ROOT_URI)){
                    pathParts = "/index.html"; // Hard-wired for now
                }
                String targetPattern = target + host + pathParts;
                System.out.println("Target URL = " + targetPattern);
                this.setTargetTemplate(targetPattern);
            } catch (MalformedURLException e){
                e.printStackTrace();
            }
            super.handle(request, response);
        }
    };
    getConnectorService().getClientProtocols().add(Protocol.HTTP);
    return proxy;
}

这里的问题是字符串host得到app-x.appspot.com的值,我需要得到图片中显示的那个。

1 个答案:

答案 0 :(得分:1)

您可以从请求对象获取此信息:

request.getRequestURI()