帮忙啊!

zealot76 2006-03-15 02:22:28
我想实现在同一个URL和HttpURLConnection连接中仅改变参数串的值(即“?”后的值),可是在实际测试时确出现下述的错误。

好像是同一个HttpConnection对象(即下面的“uc”)在执行了getInputStream()方法后,在执行getOutputStream()方法时就会出错。

希望各位能帮我解决。


附部分代码

//定义为全局静态变量
public static URL url = null;
public static HttpURLConnection uc = null;


//初始化
public init() {
if (url == null && uc == null){
try {
url = new URL("http://localhost/test.asp?");
uc = (HttpURLConnection)url.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);

}
catch (MalformedURLException ex) {
}
catch (IOException ex1) {
}
}

}


//拼好参数串传给下面的函数
private String send(String strUrl){
try {
PrintStream vOut = new PrintStream(uc.getOutputStream());

//(注:第一次执行上述语句时没有错误,但第二次执行时出现如下错误
java.net.ProtocolException: Cannot write output after reading input.)

vOut.print(strUrl);
vOut.flush();
vOut.close();

BufferedReader read = new BufferedReader(new InputStreamReader(uc.getInputStream(), "GBK"));
int ch;
StringBuffer buffer = new StringBuffer();
while ( (ch = read.read()) != -1) {
buffer.append( (char) ch);
}
String ret = buffer.toString();

read.close();

return ret;

}catch (MalformedURLException e1) {
e1.printStackTrace();
}
catch (IOException e2) {
e2.printStackTrace();
}
}
...全文
214 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
zealot76 2006-03-24
  • 打赏
  • 举报
回复
问题已经解决了!
不过还是要特别感谢polarman(北极人)给了我重要的提示信息。
顶者有分
zealot76 2006-03-20
  • 打赏
  • 举报
回复
顶。。。。。。
Timsole 2006-03-19
  • 打赏
  • 举报
回复
ding
zealot76 2006-03-19
  • 打赏
  • 举报
回复
zx2002027 2006-03-18
  • 打赏
  • 举报
回复
学习
zealot76 2006-03-18
  • 打赏
  • 举报
回复
继续顶ing。。。。。。
zealot76 2006-03-17
  • 打赏
  • 举报
回复
我试过了,每次创建新的uc也不行!
还有什么更好的方法吗!?
我现在很急啊!各位,帮帮小弟啊!
螃蟹k3179 2006-03-17
  • 打赏
  • 举报
回复
还是每次创建一个新的uc吧
zealot76 2006-03-16
  • 打赏
  • 举报
回复
顶。。。。。。
zealot76 2006-03-16
  • 打赏
  • 举报
回复
继续加分!
zealot76 2006-03-15
  • 打赏
  • 举报
回复
可是实际的需求要求我实现这样的效果,那么我该如何实现呢?
caianheng123 2006-03-15
  • 打赏
  • 举报
回复
【领先数码商城】移动硬盘专卖店:http://shop33473501.taobao.com/
victor26 2006-03-15
  • 打赏
  • 举报
回复
顶。必须要完整的http请求才可以。
polarman 2006-03-15
  • 打赏
  • 举报
回复
这是一个HTTP请求示例,楼主看了就明白了
GET /test/test.jsp?id=1&name=tom HTTP/1.1
Accept: image/gif, image/jpeg,*/*
Accept-Language:zh-cn
Accept-Encoding:gzip,
deflateUser-Agent:Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)Host:localhost:9999
Connection: Keep-Alive
polarman 2006-03-15
  • 打赏
  • 举报
回复
楼主这种做法是不正确的
必须在URL对象构造的时候就拼好参数
楼主的想法是取得输出流,然后输出参数串,但事实上,URL和参数串并不是已顺序的方式提交的,HTTP协议中Get方法获取页面时的参数是URL的一部分。你在获取输入流的时候才会发送完整的HTTP请求
zealot76 2006-03-15
  • 打赏
  • 举报
回复
加分!
zealot76 2006-03-15
  • 打赏
  • 举报
回复
继续等待ing.......
zealot76 2006-03-15
  • 打赏
  • 举报
回复
在线等!

81,122

社区成员

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

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