有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java服务器返回了URL:HTTP的HTTP响应代码:523

我想抓取一个网页,请求类型是post,但我得到一个错误: JAVA伊奥。IOException:服务器返回了URL:HTTP:的HTTP响应代码:523//

public static String readContentFromPost(String urlStr, String content) {
    URL url = null;
    HttpURLConnection con = null;
    StringBuffer sb = new StringBuffer();

    try {
        url = new URL(urlStr);
        con = (HttpURLConnection) url.openConnection();
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setRequestMethod("POST");
        con.setUseCaches(false);
        con.setInstanceFollowRedirects(true);
        con.setRequestProperty("Content-Type", "text/html;charset=utf-8");
        con.connect();

        DataOutputStream out = new DataOutputStream(con.getOutputStream());
        out.writeBytes(content);

        out.flush();
        out.close();

        BufferedReader br = new BufferedReader(new InputStreamReader(
                con.getInputStream()));

        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}

共 (0) 个答案