如何从Rest Client的响应对象获取url对象

时间:2015-05-25 11:28:32

标签: java rest jersey

我正在尝试解决下面提到的问题

查询此网络服务100次:

http://www.randomwebsite.com/cgi-bin/random.pl 

响应包含一个简短的HTML文档,其中包含一个链接。查询此链接并将链接URL以及返回的HTTP响应代码存储在合适的数据结构中。 应用程序应输出由域名的第一个字符排序的所有链接URL列表(忽略http://和www前缀),并打印相关的HTTP代码(如果不是200)。

我正在尝试使用球衣客户端

Client client = Client.create();
            WebResource webResource2 = client.resource("http://www.randomwebsite.com/cgi-bin/random.pl");

            ClientResponse response2 = webResource2.accept("text/html").get(ClientResponse.class);

            System.out.println(response2);

            if (response2.getStatus() != 200) {
                throw new RuntimeException("Failed : HTTP error code : " + response2.getStatus());
            }

            String output2 = response2.getEntity(String.class);
            System.out.println("\n============getFtoCResponse============");
            System.out.println(output2);


GET http://www.westciv.com/ returned a response status of 200 OK

现在问题是存储我无法获取uri的URI。如何从响应对象获取URI。我可以在打印时看到响应2中的随机URL。但是无法获取对象查看URL。我无法从客户端获取,因为randomwebsite重定向到随机站点。

1 个答案:

答案 0 :(得分:4)

发送的响应是重定向,状态为302.您的客户端允许not following redirects automatically

client.setFollowRedirects(false);

您应该从Location响应标头中获取网址。

相关问题