基于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

猜你喜欢

转载自lobin.iteye.com/blog/2368721