gpt4 book ai didi

java httpunit.httpexception :Error on HTTP request: 405 Method Not Allowed [url]

转载 作者:行者123 更新时间:2023-12-04 05:25:19 24 4
gpt4 key购买 nike

我已经编写了 servlet。我想测试我的 servlet 类的功能,所以我为我的 servlet 编写了一个测试用例。但是它抛出以下异常。

com.meterware.httpunit.HttpException: Error on HTTP request: 405 Method Not Allowed [http://localhost:8090/getPrintSettings]
at com.meterware.httpunit.WebClient.validateHeaders(WebClient.java:623)
at com.meterware.httpunit.WebClient.updateClient(WebClient.java:500)
at com.meterware.httpunit.WebWindow.updateWindow(WebWindow.java:192)
at com.meterware.httpunit.WebWindow.getSubframeResponse(WebWindow.java:183)
at com.meterware.httpunit.WebWindow.getResponse(WebWindow.java:158)
at com.meterware.httpunit.WebClient.getResponse(WebClient.java:122)
at com.vxoom.qit.basic.GetPrintSettingsTest.testGetForm(GetPrintSettingsTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

这是我的 Servlet 类:
public class GetPrintSettings extends HttpServlet {

private Logger log;
DB db;
Connection connection;


public void init() throws ServletException { //initialise servlet
log = Logger.getRootLogger();
db = new DB();
connection = db.connect();
super.init();
}

public void destroy() { //destroy servlet
try {
connection.close();
}
catch (SQLException e) {
log.error("AddAddress: SQL Exception: "+e.getMessage());
e.printStackTrace();
}
super.destroy();
}


protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

int userType = Integer.parseInt(request.getParameter("user"));
response.setContentType("application/json");
PrintWriter out = response.getWriter();

//Get print settings from JavaClient
String json = getPrintSettings();

if(json == null) {
out.print("{success:false, msg:'Could not send request. Please try again'}");
out.close();
return;
}

//Send the reply to the web client
out.print(json);
out.close();
}

}

这是我的服务器测试用例
  public class GetPrintSettingsTest extends TestCase {

public static void main( String args[] ) {

junit.textui.TestRunner.run(GetPrintSettingsTest.class );
}

public void testGetForm() throws Exception {
WebConversation web = new WebConversation();
WebRequest request = new GetMethodWebRequest("http://localhost:8090/getPrintSettings" );
WebResponse response = web.getResponse(request);
request.setParameter("user", "1");
response = web.getResponse( request );

}

}

我用过httpunit1.7 请帮帮我...

提前致谢:)

最佳答案

那是因为你实现了doPost()并访问 GET .

关于java httpunit.httpexception :Error on HTTP request: 405 Method Not Allowed [url],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13264746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com