How to check programatically if url of page is redirecting?

前端 未结 2 1214
刺人心
刺人心 2021-01-19 08:12

I am trying to extract the content of a webpage A. Using groovy I\'ve tried the following

......
String urlStr = \"url-of-webpage-A\"
String pageText = urlSt         


        
2条回答
  •  滥情空心
    2021-01-19 08:22

    In Java you can use URL.openConnection() to get a HttpURLConnection (you'll need to cast). On this you can call setInstanceFollowRedirects(false).

    Then you can use getResponseCode() and see if HTTP_MOVED_PERM (301), HTTP_MOVED_TEMP (302) or HTTP_SEE_OTHER (303). They all indicate redirection.

    If you need to know where you're being redirected to, then you can use getHeaderField("Location") to get the location header.

提交回复
热议问题