Java HTTP 获取请求
我需要向 Java Web 服务发出一个 HTTP GET 请求: 我提出这个请求:
http://127.0.0.1:8080/MyService/services/service?method=myMethod&a=&b=test&startDate=2011-03-10 10:00&endDate=2011-03-10 19:00
当我调试我的应用程序时,参数会切换值。我已经尝试对 startDate 和 endDate 参数进行编码,但结果是相同的。
我做错了什么?
I need to make one HTTP GET Request to a Java Web Service:
I'm making this request:
http://127.0.0.1:8080/MyService/services/service?method=myMethod&a=&b=test&startDate=2011-03-10 10:00&endDate=2011-03-10 19:00
When I Debug my app the parameters come with the values switched. I already tried to encode the startDate and endDate parameters but the result is the same.
What am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该对 URL 进行编码,使其不包含空格。这可能是你的问题。请参阅 http://download.oracle.com/ javase/1.5.0/docs/api/java/net/URLEncoder.html
You should encode your URLs so they don't contain spaces. This could be your problem. See http://download.oracle.com/javase/1.5.0/docs/api/java/net/URLEncoder.html
正如@Mirkules 所说,您应该对参数值中的空格和冒号进行编码...即使您认为这没有任何区别!
除此之外,也许您的 servlet 代码期望
Request.getParameters()
按照参数在 URL 中出现的顺序传递参数。事实并非如此。如果参数的顺序很重要,您需要自己解析查询字符串。 (或者考虑修复您的 Web API,以便查询参数顺序无关。)As @Mirkules says, you should encode the spaces and colons in the parameter values ... even if you don't think it makes any difference!
Apart from that, maybe your servlet code is expecting
Request.getParameters()
to deliver the parameters in the order that they appear in the URL. This is not the case. If order of the parameters is significant, you need to parse the query string yourself. (Or consider fixing your web API so that the query parameter order is irrelevant.)