java 从 servlet 调用外部 Web 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2852424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call an external web service from a servlet
提问by pAkY88
I'm developing a servlet that gets a name of a web service and could be forward the request to an external web service, for example: http://www.webservice.com/...
我正在开发一个获取 Web 服务名称的 servlet,并且可以将请求转发到外部 Web 服务,例如: http://www.webservice.com/...
I have build a response wrapper that intercept response output but I can't forward request to an external web service, it works only if I redirect the request to a servlet that is on same server.
我已经构建了一个拦截响应输出的响应包装器,但我无法将请求转发到外部 Web 服务,它仅在我将请求重定向到同一服务器上的 servlet 时才有效。
Example:
例子:
request.getRequestDispatcher("aMyServlet").forward(request, response) // WORKS
request.getRequestDispatcher("http://www.webservice.com/...").forward(request, response)
Doesn't because Tomcat searches http://www.webservice.com/...on the server as a local resource.
不会,因为 Tomcathttp://www.webservice.com/...在服务器上搜索作为本地资源。
How can I do external request?
如何进行外部请求?
Thanks
谢谢
采纳答案by Vineet
To make any request to an outside service, you'll have to explicitly make a new HTTP request and handle its response. Take a look at the HttpUrlConnectionclass.
要向外部服务发出任何请求,您必须明确发出新的 HTTP 请求并处理其响应。看一看HttpUrlConnection类。
回答by ring bearer
forwardmethod that you are using is used for communicating between server resources, (eg: servlet to servlet as you have found out) If you want to redirect to another location, you can use the HttpServletResponse's sendRedirectmethod.
Better option is to
Perform your own HTTP request and stream the results back to the
browser. This sounds harder than it is. Basically you create a
java.net.HttpURLConnectionwith the URL of the web site you want to
"redirect" to. This can actually contain the query parameters (as long as
they're not too large) since it will never be sent to the user's browser
either and will not appear in the browser URL bar. Open the connection, get
the content and write it to the Servlet's OutputStream.
forward您正在使用的方法用于服务器资源之间的通信,(例如:servlet 到 servlet,正如您所发现的)如果您想重定向到另一个位置,您可以使用该HttpServletResponse's sendRedirect方法。更好的选择是执行您自己的 HTTP 请求并将结果流式传输回浏览器。这听起来比实际更难。基本上,您java.net.HttpURLConnection使用要“重定向”到的网站的 URL创建一个
。这实际上可以包含查询参数(只要它们不是太大),因为它也永远不会发送到用户的浏览器,也不会出现在浏览器的 URL 栏中。打开连接,获取内容并将其写入 Servlet 的 OutputStream。
回答by mdma
You don't mention what kind of service you want to invoke, but either way, your servlet is functioning as a service client, so you should be looking at service client technologies.
您没有提到您想要调用哪种服务,但无论哪种方式,您的 servlet 都是作为服务客户端运行的,因此您应该关注服务客户端技术。
For invoking REST style services, java.net.URLor Apache Commons HttpClientcan be used to send a request for a URL and fetch the reponse.
对于调用 REST 风格的服务,java.net.URL或Apache Commons HttpClient可用于发送对 URL 的请求并获取响应。
For invoking SOAP services, you can use Apache Axis, or Java WSIT.
对于调用 SOAP 服务,您可以使用Apache Axis或Java WSIT。

