Play Framework 控制层发起HTTP请求 (Send Http Request In Controller)

有时候需要在控制层,也就是Controller或者Action中发起HTTP去第三方站点(也有去本站,分布式的情况下。),这请求可能是同步请求,也可能是异步请求。Play框架都提供了支持。 所涉及的代码: Controller:app/controllers/WebServer.java

package controllers;

import java.util.Date;

import play.libs.WS;
import play.libs.F.Promise;
import play.libs.WS.HttpResponse;
import play.libs.WS.WSRequest;
import play.mvc.Controller;

public class WebServer extends Controller{

	/**
	 * 同步请求
	 * @param url 请求的第三方链接
	 */
	public static void sync(String url){
		WSRequest wsRequest = WS.url(url);
		HttpResponse response = wsRequest.get();
		renderText(response.getString());
	}

	/**
	 * 带有超时时间的同步请求
	 * @param url 请求的第三方链接
	 * @param time 超时的时间
	 */
	public static void syncWithTimeOut(String url, String time){
		WSRequest wsRequest = WS.url(url);
		wsRequest.timeout(time);
		String words = "";
		try{
			HttpResponse response = wsRequest.get();
			words = response.getString();
		}catch(Exception e){
			renderText("time out");
		}
		renderText(words);
	}

	/**
	 * 异步请求
	 * @param url 请求的第三方链接
	 * @throws InterruptedException
	 */
	public static void async(String url) throws InterruptedException{
		WSRequest wsRequest = WS.url(url);
		Promise promise = wsRequest.getAsync();
		HttpResponse response = await(promise);
		renderText(response.getString());
	}

	/**
	 * 检测本站链接
	 */
	public static void hello() {
		renderText("hello world");
	}

	/**
	 * 模拟费时任务链接
	 * @param time 任务耗费的时长
	 * @throws InterruptedException
	 */
	public static void timeOutWait(long time) throws InterruptedException{
		Date date1 = new Date();
		String beginDateString = date1.getHours() + ":" + date1.getMinutes() + ":" + date1.getSeconds();
		Thread.sleep(time);
		Date date2 = new Date();
		String endDateString = date2.getHours() + ":" + date2.getMinutes() + ":" + date2.getSeconds();
		String words = "out yeah! the param time is "+time+". begin: " + beginDateString + " ,end:" + endDateString;
		renderText(words);
	}
}

Routes: conf/routes

GET		/async					WebServer.async
GET		/sync					WebServer.sync
GET		/sync/{time}				WebServer.syncWithTimeOut
GET		/helloworld				WebServer.hello
GET		/timeout/{time}				WebServer.timeOutWait

测试类只有测试本站链接与外站链接,还有同步的超时测试,因为比对同步与异步的多个请求,用浏览器测试比较好。Functiontest:test/function/WebServerTest.java

package function;

import org.junit.Test;
import play.mvc.Http.Response;
import play.test.FunctionalTest;

public class WebServerTest extends BasisTest {
	@Test
	public void testWsSyncOutSite() {
		Response response = GET("/sync?url=http://www.baidu.com");
		assertIsOk(response);
		String out = response.out.toString();
		assertNotNull(out);
		//因为html转义问题,百度首页开始的对比这里省略,大家可以打印出来。
		//assertTrue(out.startsWith(""));
	}

	@Test
	public void testWsSyncInSite() {
		Response response = GET("/sync?url=http://localhost:9000/helloworld");
		assertIsOk(response);
		String out = response.out.toString();
		assertEquals("hello world", out);
	}

	@Test
	public void testWsSyncTimeIn() {
		Response response = GET("/sync/1s?url=http://localhost:9000/timeout/2000");
		assertIsOk(response);
		String out = response.out.toString();
		assertEquals("time out", out);
	}

	@Test
	public void testWsSyncTimeOut() {
		Response response = GET("/sync/1s?url=http://localhost:9000/timeout/500");
		assertIsOk(response);
		String out = response.out.toString();
		assertTrue(out.startsWith("out yeah! the param time is 500."));
	}
}

第一个是百度的首页,但是只截取了前100个字符。第二个是本站的链接,虽然是本站,但是如果走HTTP请求,url地址必须全,包括协议。 然后测试同步测试,在浏览器开三个标签页,分别输入:

http://localhost:9000/sync?url=http://localhost:9000/timeout/3000
http://localhost:9000/sync?url=http://localhost:9000/timeout/4000
http://localhost:9000/sync?url=http://localhost:9000/timeout/5000

分别回车确定。 异步的测试,再打开三个标签页,分别输入:

http://localhost:9000/async?url=http://localhost:9000/timeout/3000
http://localhost:9000/async?url=http://localhost:9000/timeout/4000
http://localhost:9000/async?url=http://localhost:9000/timeout/5000

分别回车确定。 结果如下: 同步:

out yeah! the param time is 3000. begin: 10:41:45 ,end:10:41:48
out yeah! the param time is 4000. begin: 10:41:48 ,end:10:41:52
out yeah! the param time is 5000. begin: 10:41:52 ,end:10:41:57

异步:

out yeah! the param time is 3000. begin: 10:42:57 ,end:10:43:0
out yeah! the param time is 4000. begin: 10:42:58 ,end:10:43:2
out yeah! the param time is 5000. begin: 10:42:59 ,end:10:43:4

可以看出同步的三个之间,时间相连,而异步的一个之间,时间没有相连。 这里暂时缺少对异步的超时限制。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值