I am new to Android and trying to get HTML content of a web page and that is when I came across this issue.
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 = "";
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/q=0.9,*/*;q=0.8");
uc.setRequestProperty("Accept-Encoding", "html");
uc.setRequestProperty("Accept-Language", "en-US,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)) {
htmlContent +=
br.close();
} catch (Exception e) {
System.out.println(e);
return htmlC
Basically, I have written this in a pure java application (where it is working fine) and I just wanted to re-use the same. I have googled and found few SO pages (,,,), but none of them addresses the problem specific to HTTPURLConnection directly. (Though, some advised about HTTPClient, which is found to be deprecated which I don't want to use.)
Adding getResponseCode() is returning the value as 200 successfully.
System.out.println("RESPONSE CODE : "+uc.getResponseCode());
I have looked the
page and which states
public void setRequestProperty (String field, String newValue)
Sets the value of the specified request header field. The value will
only be used by the current URLConnection instance. This method can
only be called before the connection is established.
How I can set the request property before making the connections ? As per the code, we are getting the HTTPURLConnection object only after opening connection for a specific URL (and casting it with HTTPURLConnection)
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
What is the correct way to use HTTPURLConnection to read HTML page along with setRequestProperty ?
解决方案 The connection isn't created by 'URL.openConnection().` It is created when you get the input or error stream or the response code.
NB you shouldn't call setDoOutput(true) unless you are going to do output, which you aren't here. It sets the HTTP method to POST.
本文地址: &
我是新来的Android和试图获得一个网页的HTML内容,这是当我碰到这个问题。
java.lang.IllegalStateException:不能设置请求属性连接后
公共静态字符串getHTMLPage(字符串inputURL,主机字符串,字符串引荐,cookie的字符串,字符串breakString){
串htmlContent =“”;
URL U =新的URL(inputURL);
HttpURLConnection的UC =(HttpURLConnection类)u.openConnection();
uc.setRequestProperty(“主机”,主机);
uc.setRequestProperty(“连接”,“保持活动”);
如果(!referer.isEmpty()){
uc.setRequestProperty(“引荐”,引用者);
uc.setRequestProperty(“用户代理”,“Mozilla的/ 5.0(Windows NT的5.1)为AppleWebKit / 535.1(KHTML,像壁虎)的Chrome / Safari浏览器13.0.782.112 / 535.1”);
uc.setRequestProperty(“接受”,“text / html的,是application / xhtml + xml的,应用/ XML; Q = 0.9 * / *; Q = 0.8”);
uc.setRequestProperty(“接受编码”,“HTML”);
uc.setRequestProperty(“接受语言”,“EN-US,连接; Q = 0.8”);
uc.setRequestProperty(“接收字符集”,“ISO-8859-1,utf-8的; Q = 0.7,*; Q = 0.3”);
如果(!cookie.isEmpty()){
uc.setRequestProperty(“曲奇”饼干);
BR的BufferedReader =新的BufferedReader(新的InputStreamReader(uc.getInputStream()));
串线=“”;
而((行= br.readLine())!= NULL){
如果(breakString.isEmpty()及!&放大器; line.contains(breakString)){
htmlContent + =行;
br.close();
}赶上(例外五){
的System.out.println(E);
返回htmlC} 基本上,我已经在纯的Java 应用程序(它是工作的罚款),写了这个,我只是想再次使用相同的。我用Google搜索,发现几页的篇幅(1,,,),但他们没有涉及具体到 HttpURLConnection类直接(尽管这一问题,一些建议约了HTTPClient ,它被发现被德precated,我不想使用。)添加 GETRESPONSE code()的返回值200成功。 的System.out.println(“响应code:”+ uc.getResponse code()); 我已经看过了参考页,其中规定 公共无效调用setRequestProperty(String域,字符串newValue)以
设定指定的请求头字段的值。该值将
仅由当前的URLConnection实例使用。这种方法可以
在建立连接之前只能被称为如何设置连接前请求属性?由于每code,我们只 HttpURLConnection类)
HttpURLConnection的UC =(HttpURLConnection类)u.openConnection(); 什么是正确的使用方法 HttpURLConnection类与调用setRequestProperty 陪读HTML页面? 解决方案 不被'URL.openConnection()创建的连接。'当你输入或错误流或响应code已创建 NB,你不应该叫 setDoOutput(真),除非你打算怎么办输出,你是不是在这里。它集HTTP方法POST。
本文地址: &
扫一扫关注官方微信