不显示删除回复显示所有回复显示星级回复显示得分回复 卡在BufferederInputStream.read()和HttpURLConnection.getI

yangweiqing2 2010-03-26 01:41:59
各位高手大家好!
请问下在用java HttpURLConection类post数据时刚开时能够顺利完成但是如果联系多次进行就被卡在getInputStream()处无法运行下去,不知道大家在运用时有没有碰到,希望大侠指点。
StringBuffer result = new StringBuffer();
URL httpurl=null;
HttpURLConnection httpConn=null;
try {
httpurl = new URL(url);
httpConn = (HttpURLConnection) httpurl.openConnection();
httpConn.setRequestMethod("POST");
//httpConn.setReadTimeout(5000);
httpConn.setConnectTimeout(30000);
httpConn.setUseCaches(false);

httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.connect();
OutputStreamWriter outw = new OutputStreamWriter(httpConn.getOutputStream(), "utf-8");
outw.write("line=" + line + "");
outw.flush();
outw.close();
//InputStreamReader re=new InputStreamReader(httpConn.getInputStream(),"utf-8");
/*运行到下面这条语句就不卡主了无法继续运行,如果设置读取时间,当读取时间到时会发生异常,但下次还是会被卡住*/
BufferedReader ins = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "utf-8"));
// BufferedReader ins = new BufferedReader(re);

String lines;
while ((lines = ins.readLine()) != null) {
result.append(lines);
}
ins.close();
httpConn.connect();
...全文
64 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangweiqing2 2010-03-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ark032425 的回复:]
Java code

public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer result = new StringBuffer();
URL httpurl=null;
HttpURLConnecti……
[/Quote]

刚开始是可以运行,连续多次调用后就不能运行下去了,如果设定read时间httpConn.setReadTimeout(5000);时间到后就抛出异常 Read timed out,url为本地的一个jsp页面的url,不知道什么原因
  • 打赏
  • 举报
回复
[Quote=引用楼主 yangweiqing2 的回复:]
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.connect();
OutputStreamWriter outw = new OutputStreamWriter(httpConn.getOutputStream(), "utf-8");
outw.write("line=" + line + "");
outw.flush();
outw.close();
//InputStreamReader re=new InputStreamReader(httpConn.getInputStream(),"utf-8");
/*运行到下面这条语句就不卡主了无法继续运行,如果设置读取时间,当读取时间到时会发生异常,但下次还是会被卡住*/
[/Quote]
你前面输出的都关了,你还要得到哪里的数据?肯定等待数据了。
myylmyas1 2010-03-26
  • 打赏
  • 举报
回复
是不是跟多线程有关,链接次数 少没问题但多了就会出问题啊
myylmyas1 2010-03-26
  • 打赏
  • 举报
回复
没遇到过类似错误,我只做过获取方面的!没遇过post的,学习!
Ark032425 2010-03-26
  • 打赏
  • 举报
回复

public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer result = new StringBuffer();
URL httpurl=null;
HttpURLConnection httpConn=null;
try {
httpurl = new URL("http://www.123.com");
httpConn = (HttpURLConnection) httpurl.openConnection();
httpConn.setRequestMethod("POST");
// httpConn.setReadTimeout(5000);
httpConn.setConnectTimeout(30000);
httpConn.setUseCaches(false);

httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.connect();
// OutputStreamWriter outw = new OutputStreamWriter(httpConn.getOutputStream(), "utf-8");
// outw.write("keywords=help");
// outw.flush();
// outw.close();
// InputStreamReader re=new InputStreamReader(httpConn.getInputStream(),"utf-8");
/*运行到下面这条语句就不卡主了无法继续运行,如果设置读取时间,当读取时间到时会发生异常,但下次还是会被卡住*/
BufferedReader ins = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "utf-8"));
// BufferedReader ins = new BufferedReader(re);

String lines;
while ((lines = ins.readLine()) != null) {
result.append(lines);
}
System.out.println(result.toString());
ins.close();
httpConn.connect();
} catch (IOException e) {
System.out.println(e);
}

}

能行的,我运行没有卡住.你再试试

62,614

社区成员

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

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