我使用下面的代码从我的Java类调用servlet:
URL url = new URL( "http://localhost:7001/Socket-war/Servlet" );
new BufferedReader(new InputStreamReader(url.openStream()));
获取异常:
java.io.FileNotFoundException: Response: '404: Not Found' for url: 'http://localhost:7001/Socket-war/Servlet'
一切正常;我以前在我的其他程序中也使用过它;但是现在提出了例外……
谁来告诉我为什么?任何意见或建议都将非常感激。谢谢! !
这个答案是对聊天发生的响应。
以下是我的servlet代码protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println("Hello World");
response.getWriter().flush();
response.getWriter().close();
}
下面是我的连接代码,它工作。
public static void main(String[] args) throws IOException {
System.setProperty("http.proxyHost", "proxy.***.com");
System.setProperty("http.proxyPort", "####");
Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication("user",
"pwd".toCharArray()));
}
};
Authenticator.setDefault(authenticator);
URL url = new URL( "http://localhost:8080/ImageCanvas/Servlet2" );
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while((line=br.readLine())!= null){
System.out.println(line);
System.out.flush();
}
br.close();
}