我的GWT服务器端代码上的http代理 [英] http proxy on my GWT server-side code
问题描述
我有一个REST服务器作为后端,它提供了一组服务,并且它使用基本身份验证方法进行访问.
I have an REST server as backend, it provides a set of services, also, it uses basic authentication method for access.
现在我需要创建一个GWT前端,因此,我需要从GWT前端对REST后端执行http调用
Now I need to create an GWT frontend,so, I need to perform http calls to the REST backend from the GWT frontend
经过研究,我发现HttpBuilder可以处理对后端的http请求,但是在尝试执行跨站点请求时似乎很痛苦,并且还附带了一些与Safari浏览器有关的限制.
After some research I found the HttpBuilder to handle http requests to the backend, but it seem to be a pain when trying to perform cross-site requests, and also it comes with some restricions related with Safari browser.
然后我发现了这个 https://developers.google.com/web-toolkit/doc/latest/tutorial/Xsite 文章,其中讨论了您自己的服务器上的代理",因此它似乎是我一直在寻找的解决方案,但我没有找到更多信息,或一个例子.它说我可以创建服务器端代码以从远程服务器(后端)下载数据,因此,我应该在 http客户端上创建一个 http客户端 >服务器端代码,并实现使用该服务向后端发出请求的服务集?(如果是)如何处理用户身份验证和会话?如果没有,请给我个灯.
Then I found this https://developers.google.com/web-toolkit/doc/latest/tutorial/Xsite article, where it talks about an "Proxy on your own server", so it looks to be the solution I was looking for, but I did not find more information, or an example. It says that I could create server-side code to download the data from remote server (backend), so, should I create a http client like the apache client on server-side code, and implement a set of services that use it to make request to the backend?, if yes, how to handle the user authentication and the session? if not, give me a light please.
谢谢
推荐答案
it seem to be a pain when trying to perform cross-site requests,
实际上,如果我们可以在Servlet Response Header中进行设置,则可以从GWT RequestBuilder发出跨站点请求
Actually you can make Cross Site Requests from GWT RequestBuilder if we can set in Servlet Response Header
Response.setHeader("Access-Control-Allow-Origin"," http://yourrestserviceur.com/url);
Response.setHeader("Access-Control-Allow-Origin","http://yourrestserviceur.com/url");
should I create a http client like the apache client on server-side code, and implement
a set of services that use it to make request to the backend?
否,不是必需的.使用RequestBuilder
No, it is not required. use RequestBuilder
RequestBuilder示例:
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
try {
Request request = builder.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable exception) {
displayError("Couldn't retrieve JSON");
}
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
updateTable(asArrayOfStockData(response.getText()));
} else {
displayError("Couldn't retrieve JSON (" + response.getStatusText()
+ ")");
}
}
});
} catch (RequestException e) {
displayError("Couldn't retrieve JSON");
}
这篇关于我的GWT服务器端代码上的http代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!