使用 HttpUnit 中的 WebRequest 之类的东西向 ServletUnit 提交表单的最简单方法
我想以编程方式创建一个带有字段等的表单,但是我无法找到公共工厂等来创建 WebForm(类)。完成此操作后,我想提交表单并让我的 servlet 对表单执行操作。
我注意到测试使用的一种方法是创建一个 PseudoServer,它是一个简单的套接字服务器。然后测试最终向某个 url 发出请求,该 url 会回复一些包含表单的任意 html。问题是我无法注册自己的自定义 servlet 来执行操作。
因此,如果我希望将 servletunit 单元化,我就会陷入想要一种表单但无法创建表单之间的困境。
- 有没有办法将表单提交到 servlet 单元内的 servlet?
- 有没有办法将 httpunit 的部分表单提交内容与 servlet 单元结合起来?
我猜可能不是因为它(httpunit)想要通过套接字提交表单,而 servletunit 根本不使用套接字。
根据 Andrey 的建议和我过去的实验,我尝试调用 WebRequest 上的许多方法来尝试传达发布到服务器的表单中存在的内容。
- selectFile() - 选择要上传的文件
- setHeaderField() 设置内容类型/字符集/编码。
I would like to programmatically create a form with fields etc, however i have not been able to find a public factory etc to create a WebForm(class). Once this is done i would like to then submit the form and have my servlet do stuff with the form.
One approach i noticed the tests use is to create a PseudoServer which is a simple socket server. The tests then eventually make a request to some url which replies with some arbitrary html which includes a form. The problem with this is i cant register my own custom servlet to do stuff.
Im thus stuck between wanting a form but being unable to create one, if i wish to unit servletunit.
- Is there a way to submit forms to a servlet inside servlet unit ?
- Is there a way to combine parts of httpunit the form submitting stuff w/ servlet unit ?
Im guessing probably not because it(httpunit) wants to submit a form via socket and servletunit does not use sockets at all.
As per Andrey's suggestion and my past experimenting i have attempted to to call numerous methods on WebRequest to attempt to communicate the stuff that exists in a form being posted to a server.
- selectFile() - to pick the file to be uploaded
- setHeaderField() to set content type/charset/encoding.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 PostMethodWebRequest 将 POST 请求发送到任何 HTTP URL:
然后直接在请求对象中设置表单参数:
You can use PostMethodWebRequest to send POST request to any HTTP URL:
And then just set form parameters directly in the request object: