java get请求api,从Java Servlet向Web API发送get请求

Common question

Is is possible to send get reguest from Java servlet's doGet method ? I need to check some "ticket" against my Web API .NET service, so can I call to this service from my custom servlet in the doGet method ?

public class IdentityProviderServlet extends HttpServlet {

...

@Override

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

...

// make request to Web API and save received user name to response

...

}

Details

We have web-app (.NET, MVC5) that is used TIBCO Spotfire 7.0 as engine of analysis reports.

To allow our users to see reports inside web-app we use Spotfire WebPlayer (IIS web app) and JavaScript API. We authenticate our users in web app and then they are allowed to get request to WebPlayer leveraging JS API. In order to use already authenticated users we implemented custom authentication at WebPlayer based on keys-tickets as described here. Thus we created .NET assembly that is loaded by Spotfire WebPlayer and calls overridden function. In this function we call Web API service to validate user and get valid spotfire username, then I create IIdentity with received username.

When new version of TIBCO Spotfire 7.5 released we discovered that they removed support of custom authentication, because they changed architecture and now they support "external authentication". This approach could be implemented as as Java servlet that is used to authenticate user and then spotfire:

Retrieves the user name from the getUserPrincipal() method of

javax.servlet.http.HttpServletRequest

All this forced us to re-write our logic in Java. However we don't want to change overall workflow of our authentication and we want to stick to already working ticketing schema. I'm new to Java servlets, so my goal to implement the same authentication based on servlet. They have example where servlet class has methods doGet and doPost (link to zip with example). My assumption here that I can implement my own doGet and send request to Web API to validate ticket and get back username.

Does it make sense ?

解决方案

You can use the library Apache HTTP Components

Short example for doGet() (I didn't compile it):

import org.apache.http.*;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.config.RequestConfig;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.*;

import org.apache.http.entity.ContentType;

import org.apache.http.entity.InputStreamEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.message.BasicNameValuePair;

protected void doGet(HttpServletRequest req, HttpServletResponse resp)

{

String url="http://your.server.com/path/to/app?par1=yxc&par2=abc");

HttpGet get=new HttpGet(url);

httpClient = HttpClients.createDefault();

// optional configuration

RequestConfig config=RequestConfig.custom().setSocketTimeout(socketTimeoutSec * 1000).build();

// more configuration

get.setConfig(config);

CloseableHttpResponse internResponse = httpClient.execute(get);

int internResponseStatus = internResponse.getStatusLine().getStatusCode();

InputStream respIn = internResponseEntity.getContent();

String contentType = internResponseEntity.getContentType().getValue();

// consume the response

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值