扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

setrequestproperty()鎬庝箞浣跨敤

扬州沐宇科技
2023-12-19 20:48:04
浣跨敤

鍦↗ava涓紝鍙互浣跨敤setRequestProperty()鏂规硶鏉ヨ缃瓾TTP璇锋眰鐨勫ご閮ㄥ睘鎬с€傝鏂规硶鐨勮娉曞涓嬶細

public void setRequestProperty(String key, String value)

鍏朵腑锛?code>key琛ㄧず瑕佽缃殑澶撮儴灞炴€х殑閿紝value琛ㄧず瑕佽缃殑澶撮儴灞炴€х殑鍊笺€?/p>

浠ヤ笅鏄竴涓ず渚嬩唬鐮侊紝婕旂ず濡備綍浣跨敤setRequestProperty()鏂规硶璁剧疆HTTP璇锋眰鐨勫ご閮ㄥ睘鎬э細

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static void main(String[] args) {
        try {
            // 鍒涘缓URL瀵硅薄
            URL url = new URL("http://example.com");

            // 鎵撳紑杩炴帴
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 璁剧疆璇锋眰鏂规硶
            connection.setRequestMethod("GET");

            // 璁剧疆澶撮儴灞炴€?/span>
            connection.setRequestProperty("User-Agent", "Mozilla/5.0");
            connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

            // 鍙戦€佽姹?/span>
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

鍦ㄤ笂闈㈢殑渚嬪瓙涓紝鎴戜滑璁剧疆浜嗕袱涓ご閮ㄥ睘鎬э細User-Agent鍜?code>Accept-Language銆傝繖浜涘睘鎬у皢鍦ㄥ彂閫丠TTP璇锋眰鏃跺寘鍚湪璇锋眰澶翠腑銆?/p>

扫码添加客服微信