帮忙啊!
我想实现在同一个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();
}
}