public static String getLocationMethod(String reqUrl) { DefaultHttpClient httpclient = new DefaultHttpClient(); String location = null; int responseCode = 0; try { final HttpGet request = new HttpGet(reqUrl); HttpParams params = new BasicHttpParams(); params.setParameter("http.protocol.handle-redirects", false); // 默认不让重定向 // 这样就能拿到Location头了 request.setParams(params); HttpResponse response = httpclient.execute(request); responseCode = response.getStatusLine().getStatusCode(); if(responseCode==302){ Header locationHeader = response.getFirstHeader("Location"); if (locationHeader != null) { location = locationHeader.getValue(); } } } catch (Exception e) { e.printStackTrace(); log.error("exception=", e.toString()); } return location; }
根据短链接获取长链接方法
最新推荐文章于 2024-06-13 16:39:20 发布