我有一个在线 servlet,我正在尝试联系它以进行一些基本测试。这是servlet代码:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class index extends HttpServlet {
private static final long serialVersionUID = 1L;
public index() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
long time1 = System.currentTimeMillis();
long time2 = time1 + 10000;
out.println(time1);
long i = 400000000l;
while (System.currentTimeMillis() < time2) {
i++;
}
out.print(time2);
}
}
现在,我正在尝试使用以下代码从服务器获取信息:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpRequest {
public static void main(String args[]) {
BufferedReader rd;
OutputStreamWriter wr;
try {
URL url = new URL("http://blahblahblah/index");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
wr = new OutputStreamWriter(conn.getOutputStream());
wr.flush();
conn.setConnectTimeout(50000);
rd = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
但是我不断收到相同的 405 错误。我究竟做错了什么?