【问题标题】:"java.lang.IllegalStateException: Cannot set request property after connection is made" error in AndroidAndroid中的“java.lang.IllegalStateException:连接后无法设置请求属性”错误
【发布时间】:2015-05-10 03:10:21
【问题描述】:

我是 Android 新手,正在尝试获取网页的 HTML 内容,这时我遇到了这个问题。

java.lang.IllegalStateException: Cannot set request property after connection is made

代码:

public static String getHTMLPage(String inputURL, String host, String referer, String cookie, String breakString) {

    String htmlContent = "";
    try {
        URL u = new URL(inputURL);
        HttpURLConnection uc = (HttpURLConnection) u.openConnection();
        uc.setRequestProperty("Host", host);
        uc.setRequestProperty("Connection", "keep-alive");
        if (!referer.isEmpty()) {
            uc.setRequestProperty("Referer", referer);
        }
        uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1");
        uc.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        uc.setRequestProperty("Accept-Encoding", "html");
        uc.setRequestProperty("Accept-Language", "en-US,en;q=0.8");
        uc.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");

        if (!cookie.isEmpty()) {
            uc.setRequestProperty("Cookie", cookie);
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        String line = "";
        while ((line = br.readLine()) != null) {
            if (!breakString.isEmpty() && line.contains(breakString)) {
                break;
            }
            htmlContent += line;
        }
        br.close();
    } catch (Exception e) {
        System.out.println(e);
    }
    return htmlContent;
}

基本上,我已经在纯java 应用程序中编写了这个(它工作正常),我只是想重复使用它。我用谷歌搜索并找到了几个 SO 页面(1234),但它们都没有直接解决特定于HTTPURLConnection 的问题。 (不过,有些人建议 HTTPClient,发现它已被弃用,我不想使用。)

添加getResponseCode() 成功返回值为200。

System.out.println("RESPONSE CODE : "+uc.getResponseCode());

我查看了reference 页面以及哪些状态

public void setRequestProperty (String field, String newValue)

设置指定请求头域的值。该值将 仅由当前 URLConnection 实例使用。这种方法可以 仅在建立连接之前调用。

如何在建立连接之前设置请求属性?根据代码,只有在打开特定 URL 的连接(并使用 HTTPURLConnection 进行转换)后,我们才能获取 HTTPURLConnection 对象

         HttpURLConnection uc = (HttpURLConnection) u.openConnection();

使用HTTPURLConnectionsetRequestProperty 一起阅读HTML 页面的正确方法是什么?

【问题讨论】:

    标签: java android httpurlconnection


    【解决方案1】:

    连接不是由“URL.openConnection()”创建的。它是在您获得输入或错误流或响应代码时创建的。

    NB 你不应该打电话给setDoOutput(true) 除非你打算做输出,你不在这里。它将 HTTP 方法设置为 POST。

    【讨论】:

    • @ EJP :如果没有setDoOutput,同样的问题。我在这里发帖时忘记删除这些行了。
    • @ EJP :顺便说一句,如果连接不是由openConnection() 方法创建的,那么为什么会抛出Cannot set request property after connection is made 的错误?我还需要做什么才能让它发挥作用?
    • @Dinesh 它没有,你发布的代码。您必须调用了打开连接的else。见here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多