基于hessian协议进行rpc调用(http方式)

一个最简单的例子

基于hessian进行rpc调用(http方式)

 

先定义一个接口:

public interface TestService {

    public void test(String name);

}

 

实现这个接口:

public class TestServiceImpl implements TestService {

    @Override

    public void test(String name) {

        System.out.println("test:" + name);

    }

}

 

基于hessian进行rpc调用(http方式)

 

 

c 0x01 0x00

m 0x00 0x04 test

S 0x00 0x0A helloworld

z

 

 

@Test

public void invoke() throws IOException {

String url = "http://localhost:8080/springhessiantest/service/TestService";

URL u = new URL(url);

URLConnection connection = u.openConnection();

connection.setDoOutput(true);

connection.setDoInput(true);

OutputStream os = connection.getOutputStream();

 

ByteArrayOutputStream bos = new ByteArrayOutputStream();

 

bos.write('c');

bos.write(0x01);

bos.write(0x00);

 

 

// m 0x00 0x04 test

bos.write('m');

bos.write(0x00);

bos.write(0x04);

bos.write("test".getBytes());

 

// S 0x00 0x0A helloworld

bos.write('S');

String s = "helloworld";

int length = 10;

length = (length << 16) >>> 16;

bos.write(length >>> 8);

bos.write((length << 8) >>> 8);

bos.write(s.getBytes());

 

// z

bos.write('z');

 

os.write(bos.toByteArray());

 

InputStream is = connection.getInputStream();

 

StringBuilder sb = new StringBuilder();

byte[] buf = new byte[128];

int i = -1;

while ((i = is.read(buf)) != -1) {

sb.append(new String(buf, 0, i));

}

System.out.println(sb.toString());

}

 

 

运行输出:

test:helloworld

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值