String htmlContent = null;
HttpURLConnection urlConn = null;
try {
URL u = new URL(urlName);
urlConn = (HttpURLConnection) (u.openConnection());
urlConn.setConnectTimeout(40000);
urlConn.setReadTimeout(60000);
urlConn.setRequestMethod("POST");
urlConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
urlConn.setFollowRedirects(true);
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
urlConn.setUseCaches(false);
urlConn.setAllowUserInteraction(false);
urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConn.setRequestProperty("Content-Language", "en-US");
urlConn.setRequestProperty("Cookie", cookie); BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
htmlContent += line;
}
in.close();
// WriteLog.printlnNowDate();
} catch (Exception e) {
e.printStackTrace();
} finally {
urlConn.disconnect();
urlConn = null;
} return htmlContent;我又一个程序,以上是读取网页代码,因为需要不停的读取一系列的网页,所以多线程运行。 可是刚开始运行能够正常的拿到数据,当运行半小时后就会抛出java.io.IOException: Server returned HTTP response code: 400 for URL ,然后以上代码再也取网页数据。全部是400错误。 谁能告诉我为什么呢?
非常感谢
HttpURLConnection urlConn = null;
try {
URL u = new URL(urlName);
urlConn = (HttpURLConnection) (u.openConnection());
urlConn.setConnectTimeout(40000);
urlConn.setReadTimeout(60000);
urlConn.setRequestMethod("POST");
urlConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
urlConn.setFollowRedirects(true);
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
urlConn.setUseCaches(false);
urlConn.setAllowUserInteraction(false);
urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConn.setRequestProperty("Content-Language", "en-US");
urlConn.setRequestProperty("Cookie", cookie); BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
htmlContent += line;
}
in.close();
// WriteLog.printlnNowDate();
} catch (Exception e) {
e.printStackTrace();
} finally {
urlConn.disconnect();
urlConn = null;
} return htmlContent;我又一个程序,以上是读取网页代码,因为需要不停的读取一系列的网页,所以多线程运行。 可是刚开始运行能够正常的拿到数据,当运行半小时后就会抛出java.io.IOException: Server returned HTTP response code: 400 for URL ,然后以上代码再也取网页数据。全部是400错误。 谁能告诉我为什么呢?
非常感谢
解决方案 »
- java线程交替问题
- applet的下载和刷新
- JEditorPane setPage() 问题?? 急~
- 单选框的问题···
- 自己写了一个正则表达式工具(2)
- 如何在JButton 设置“ENTER”快捷键.
- 关于char的问题及数据包制作的问题
- [求助]怎样用URLConnection 类,建立与http服务器的连接后,再检验http服务器返回的状态代码,如200,500,401用户名密码错误等?
- 用jar打包的问题
- 初学者的问题:http://localhost:8080/index.html成功显示,但http://localhost:8080/index.jsp不能成功显示
- 我想问道Java中的练习题
- 测试的软件,类loadrunner for java ?
运行到400错误的时候看看当前请求的URL是否正确。
1. 服务器有防攻击机制,拦截了。检查方法是在出问题的时候不关程序,然后用浏览器访问一下看看有没有问题。
2. 连接被耗光了。检查方法是在出问题的时候运行一下netstat,看看有多少连接数。因为windows释放连接并没有想象中那么快。不一定是上面的原因,逐步排查吧。
2.连接耗光也应该不可能,程序中每次连接都关闭了,通过netstat也显示正常。而且出问题地时候用浏览器也能正常打开网页。这个不好说,但是也应该不会。能给我说说应该注意的嘛现在主要问题为什么运行一会儿(时间不确定)会抛出400错误。而且在网络不稳定的时候这种情况发生几率还要高一些。
抛出400错误后,就一直不能正常拿到网页,每次请求都是400错误。大家再提醒提醒思路,我好查查错啊
urlConn.setReadTimeout(60000);
注释掉试试。。
这个线程大概流程是怎样的?
欢迎访问我的java博客http://www.java339.cn
又或者你自己同时的outgoing 连接太多,造成网络拥堵,类似于开bt的同时看网页,造成问题我以前的网页抓取程序也遇上过 后来用Thread.sleep控制一下链接间隔就好了
import java.io.*;
import java.net.*;
import java.awt.*;
import java.applet.*;
public class ReaderURL extends Applet{
URL fileUrl;
TextArea show=new TextArea(100,100);
public void init(){
String url="http://www.hao123.com/index.html";
try{
fileUrl=new URL(url);
}catch(MalformedURLException e){
}
add(show);
}
public void paint(Graphics g){
InputStream filecon=null;
DataInputStream fileData=null;
String fileline;
try{
filecon=fileUrl.openStream();
fileData=new DataInputStream(filecon);
while((fileline=fileData.readLine())!=null){
show.append(fileline+"\n");
}
}catch(IOException ioe){
}
}
}