java.io.IOException:服务器返回URL的HTTP响应代码:405
我有一个在线servlet,我正试图联系它,以便进行一些基本的测试。以下是servlet代码:java.io.IOException:服务器返回URL的HTTP响应代码:405,java,ios,servlets,Java,Ios,Servlets,我有一个在线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.h
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);
}
}
然而,我一直得到相同的405错误。我做错了什么?您看到的是
HttpServlet
的默认实现doPost()
,因为您没有在索引中覆盖它
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_post_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}
它会立即发送405响应
这是因为您调用
conn.getOutputStream()
这使得URLConnection
默认情况下认为您发送的是POST
,而不是预期的GET
。您甚至没有使用OutputStream
那么为什么要打开它,然后flush()
它,再也不使用它了
wr = new OutputStreamWriter(conn.getOutputStream());
wr.flush();
您还可以检查请求方法是GET还是POST:
private static final int READ_TIMEOUT = 100000;
private static final int CONNECT_TIMEOUT = 150000;
public static final String POST = "POST";
public static final String GET = "GET";
public static final String PUT = "PUT";
public static final String DELETE = "DELETE";
public static final String HEAD = "HEAD";
private URL url = null;
private HttpURLConnection conn = null;
private OutputStream os = null;
private BufferedWriter writer = null;
private InputStream is = null;
private int responseCode = 0;
private String request(String method, String url, List<NameValuePair> params) throws IOException {
if(params != null && method == GET){
url = url.concat("?");
url = url.concat(getQuery(params));
}
this.url = new URL(url);
conn = (HttpURLConnection) this.url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECT_TIMEOUT);
conn.setRequestMethod(method);
conn.setDoInput(true);
conn.setDoOutput(true);
if(params != null && method == POST){
os = conn.getOutputStream();
writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
}
conn.connect();
responseCode = conn.getResponseCode();
is = conn.getInputStream();
String contentAsString = getStringFromInputStream(is);
return contentAsString;
}
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params){
if (first){
first = false;
} else {
result.append("&");
}
result.append(URLEncoder.encode(pair.getName(),"UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(),"UTF-8"));
}
return result.toString();
}
private static final int READ\u TIMEOUT=100000;
专用静态最终int CONNECT_超时=150000;
公共静态最终字符串POST=“POST”;
公共静态最终字符串GET=“GET”;
公共静态最终字符串PUT=“PUT”;
公共静态最终字符串DELETE=“DELETE”;
公共静态最终字符串HEAD=“HEAD”;
私有URL=null;
私有HttpURLConnection conn=null;
私有输出流os=null;
private BufferedWriter=null;
私有InputStream为空;
私有int响应代码=0;
私有字符串请求(字符串方法、字符串url、列表参数)引发IOException{
if(params!=null&&method==GET){
url=url.concat(“?”);
url=url.concat(getQuery(params));
}
this.url=新url(url);
conn=(HttpURLConnection)this.url.openConnection();
conn.setReadTimeout(读取超时);
连接设置连接超时(连接超时);
conn.setRequestMethod(方法);
conn.setDoInput(真);
连接设置输出(真);
if(params!=null&&method==POST){
os=conn.getOutputStream();
writer=new BufferedWriter(新的OutputStreamWriter(os,“UTF-8”));
write(getQuery(params));
writer.flush();
writer.close();
os.close();
}
连接();
responseCode=conn.getResponseCode();
is=conn.getInputStream();
字符串contentAsString=getStringFromInputStream(is);
返回contentAsString;
}
私有字符串getQuery(列表参数)引发UnsupportedEncodingException{
StringBuilder结果=新建StringBuilder();
布尔值优先=真;
for(NameValuePair对:params){
如果(第一){
第一个=假;
}否则{
结果。追加(&);
}
append(URLEncoder.encode(pair.getName(),“UTF-8”);
结果。追加(“=”);
append(URLEncoder.encode(pair.getValue(),“UTF-8”);
}
返回result.toString();
}
private static final int READ_TIMEOUT = 100000;
private static final int CONNECT_TIMEOUT = 150000;
public static final String POST = "POST";
public static final String GET = "GET";
public static final String PUT = "PUT";
public static final String DELETE = "DELETE";
public static final String HEAD = "HEAD";
private URL url = null;
private HttpURLConnection conn = null;
private OutputStream os = null;
private BufferedWriter writer = null;
private InputStream is = null;
private int responseCode = 0;
private String request(String method, String url, List<NameValuePair> params) throws IOException {
if(params != null && method == GET){
url = url.concat("?");
url = url.concat(getQuery(params));
}
this.url = new URL(url);
conn = (HttpURLConnection) this.url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECT_TIMEOUT);
conn.setRequestMethod(method);
conn.setDoInput(true);
conn.setDoOutput(true);
if(params != null && method == POST){
os = conn.getOutputStream();
writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
}
conn.connect();
responseCode = conn.getResponseCode();
is = conn.getInputStream();
String contentAsString = getStringFromInputStream(is);
return contentAsString;
}
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params){
if (first){
first = false;
} else {
result.append("&");
}
result.append(URLEncoder.encode(pair.getName(),"UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(),"UTF-8"));
}
return result.toString();
}