根据短链接获取长链接方法

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;

}

你可能感兴趣的:(根据短链接获取长链接方法)