admin
2024-01-23 81da61b828e29b7745e1382dfbbaeb685dc083ef
utils/src/main/java/org/yeshi/utils/HttpUtil.java
@@ -46,7 +46,7 @@
import org.yeshi.utils.entity.ProxyIP;
import net.sf.json.JSONObject;
import java.net.HttpURLConnection;
public class HttpUtil {
    public static List<String> getUrlListFromText(String text) {
@@ -657,4 +657,53 @@
        }
        return null;
    }
    public static String getLocation(String url,Map<String,String> headers, String method) {
        HttpClient client = new HttpClient();
        if ("GET".equalsIgnoreCase(method)) {
            try {
                URL uri = new URL(url);
                // 打开连接
                HttpURLConnection connection = (HttpURLConnection) uri.openConnection();
                // 设置HTTP请求方法为GET
                connection.setRequestMethod("GET");
                // 获取响应码
                int responseCode = connection.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    // 获取Location字段
                    String location = connection.getHeaderField("Location");
                    System.out.println("Location: " + location);
                } else {
                    System.out.println("Request failed with response code: " + responseCode);
                }
                // 关闭连接
                connection.disconnect();
            }catch(Exception e){
                e.printStackTrace();
            }
            return null;
        } else {
            PostMethod md = new PostMethod(url);
            if (headers != null) {
                Iterator<String> keys = headers.keySet().iterator();
                while (keys.hasNext()) {
                    String key = keys.next();
                    md.addRequestHeader(key, headers.get(key));
                }
            }
            try {
                client.executeMethod(md);
                Header[] responseHeaders = md.getResponseHeaders();
                return md.getResponseHeader("location").getValue();
            } catch (HttpException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
//        }
            return null;
        }
    }
}