通过java获取401响应的HTTP请求

通过java获取401响应的HTTP请求,java,http,http-post,httpurlconnection,http-get,Java,Http,Http Post,Httpurlconnection,Http Get,我正在尝试使用java.net.HttpURLConnection通过java调用url。 下面是代码。 我得到401作为回应。url已打开 // HTTP GET request private void sendGet() throws Exception { String url = "http://10.10.200.151:8720/scheduler/stat.go?opt1=0&opt2=0&opt3=0"; URL

我正在尝试使用java.net.HttpURLConnection通过java调用url。 下面是代码。 我得到401作为回应。url已打开

// HTTP GET request
    private void sendGet() throws Exception {


        String url = "http://10.10.200.151:8720/scheduler/stat.go?opt1=0&opt2=0&opt3=0";


        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // optional default is GET
        con.setRequestMethod("GET");

        //add request header
        con.setRequestProperty("User-Agent", USER_AGENT);

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //print result
        System.out.println(response.toString());

    }

缺少某些内容。

HTTP状态401表示请求需要身份验证。
可能此url需要登录,服务器会通过cookie进行检查。

HTTP状态401表示请求需要身份验证。
可能此url需要登录,服务器会通过cookie进行检查。

请求中可能缺少正确的标题请求中可能缺少正确的标题