target:http://www.52rd.com/bbs/Detail_RD.BBS_185755_11_1_1.html
charset:gb2312症状,获取html源码不完整,会是JDK的bug吗?用Apache的httpClient试过,一切正常,就URLConnection不行。贴上方法:public static String html2String(String sUrl,String charset) throws IOException {
int HttpResult;
URL url = new URL(sUrl);
URLConnection urlconn = url.openConnection();
StringBuffer resultBuffer = new StringBuffer();
urlconn.connect();
HttpURLConnection httpconn = (HttpURLConnection) urlconn;
HttpResult = httpconn.getResponseCode();
if (HttpResult == HttpURLConnection.HTTP_OK) {
InputStreamReader isr = new InputStreamReader(httpconn.getInputStream(),charset);
BufferedReader in = new BufferedReader(isr);
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
resultBuffer.append(inputLine+"\n");
}
in.close();
}
return resultBuffer.toString();
}
charset:gb2312症状,获取html源码不完整,会是JDK的bug吗?用Apache的httpClient试过,一切正常,就URLConnection不行。贴上方法:public static String html2String(String sUrl,String charset) throws IOException {
int HttpResult;
URL url = new URL(sUrl);
URLConnection urlconn = url.openConnection();
StringBuffer resultBuffer = new StringBuffer();
urlconn.connect();
HttpURLConnection httpconn = (HttpURLConnection) urlconn;
HttpResult = httpconn.getResponseCode();
if (HttpResult == HttpURLConnection.HTTP_OK) {
InputStreamReader isr = new InputStreamReader(httpconn.getInputStream(),charset);
BufferedReader in = new BufferedReader(isr);
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
resultBuffer.append(inputLine+"\n");
}
in.close();
}
return resultBuffer.toString();
}
解决方案 »
- java经典银行多线程问题 不要告诉我用sychronized那样效率太低
- 如何让输入到TextField里的数据为星号!!!!!!!!!!!!!!!!
- java中如何读文件,谁有相关的代码,请贴一个吧,谢谢
- 弹出式菜单问题
- 解析xml文件问题,帮忙看看
- 请问swing程序启动时有什么特别的优化措施没有,我的启动要8/9s,客户不停地点,一下出来4、5个程序界面,怎么办??
- java 类型记录
- 救人一命,胜造七级浮屠,小弟是新人
- 对于相同的Exception,如何分辨造成Exception的原因
- 统计一下,有多少湖南的兄弟姐妹。大家别扔。。。。我只是关心家乡。。
- 求教冒泡排序
- 求助:Exception in thread "main" java.lang.NoSuchMethodError:main是什么错误呀?谢谢!在线等……
try {
// Create a URL for the desired page
URL url = new URL("http://hostname:80/index.html");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
// str is one line of text; readLine() strips the newline character(s)
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
返回头没有content-length,结果就读不完