我正在请求:
http://www.baseaddress.com/path/index1.html
根据我发送的论点,我将重定向到这两个之一:
http://www.baseaddress.com/path2/
或者
http://www.baseaddress.com/path/index2.html
问题是响应只返回:
index2.html
或/path2/
现在我检查第一个字符是否为/
,并根据此连接 URL。有没有一种简单的方法可以在不检查字符串的情况下做到这一点?
编码:
url = new URL("http://www.baseaddress.com/path/index1.php");
con = (HttpURLConnection) url.openConnection();
... some settings
in = con.getInputStream();
redLoc = con.getHeaderField("Location"); // returns "index2.html" or "/path2/"
if(redLoc.startsWith("/")){
url = new URL("http://www.baseaddress.com" + redLoc);
}else{
url = new URL("http://www.baseaddress.com/path/" + redLoc);
}
你认为这是最好的方法吗?