我正在编写一个程序,通过
HttpURLConnection
连接到一个servlet,但是我在检查url时遇到了困难。public void connect (String method) throws Exception {
server = (HttpURLConnection) url.openConnection ();
server.setDoInput (true);
server.setDoOutput (true);
server.setUseCaches (false);
server.setRequestMethod (method);
server.setRequestProperty ("Content-Type", "application / xml");
server.connect ();
/*if (server.getResponseCode () == 200)
{
System.out.println ("Connection OK at the url:" + url);
System.out.println ("------------------------------------------- ------- ");
}
else
System.out.println ("Connection failed");
}*/
我遇到了以下错误:
java.net.ProtocolException: Cannot write output after reading input.
如果我使用注释中的代码检查URL,它可以正常工作,但不带注释的代码出现该错误。不幸的是,我需要检查URL,所以我认为问题出在getResponseCode
方法上,但我不知道如何解决。
非常感谢。
getResponseCode()
后面跟着一个getOutputStream();
。你认为这会导致Cannot write output after reading input
吗? - Mushy