请教,使用HttpURLConnection是出现http500 内部服务器错误错误

wfly0001 2009-05-22 10:45:27
我这么写了一个简单的测试,还是报500的错误,但是其他的网站可以,比如百度,错误如下
java.io.IOException: Server returned HTTP response code: 500 for URL: http://www.dianping.com/beijing
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at Testdianping.main(Testdianping.java:9)
不知道到底该怎么改啊
Java代码
import java.io.*;
import java.net.*;
public class Testdianping {
public static void main(String[] args)
{
try {
URL url = new URL("http://www.dianping.com/beijing");
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream(),"utf-8"));
String str;
while((str = br.readLine()) != null)
{
System.out.println(str);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

import java.io.*;
import java.net.*;
public class Testdianping {
public static void main(String[] args)
{
try {
URL url = new URL("http://www.dianping.com/beijing");
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream(),"utf-8"));
String str;
while((str = br.readLine()) != null)
{
System.out.println(str);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
...全文
580 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yghsxdr2009 2011-08-23
  • 打赏
  • 举报
回复
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
芙蓉石的旋律 2011-05-23
  • 打赏
  • 举报
回复
怎样设置user-agent的啊,把代码贴出来
wfly0001 2009-06-19
  • 打赏
  • 举报
回复
自己解决了 谢谢dangjing20065141 我设置了user-agent就可以了 不过速度很慢 我把超时设置为2分钟都不行,不会是我这网速的问题吧,我用ie打开速度还行啊?
Sou2012 2009-05-23
  • 打赏
  • 举报
回复
UP...
dangjing20065141 2009-05-22
  • 打赏
  • 举报
回复
500 以上的响应码表明是服务器端异常,是服务器端程序出问题,和你写的程序应该没有关系!

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧