/**
* 发送请求
* @param ip
* @param port
* @param msg
* @throws Exception
*/
private String sendMsg(String ip, int port, String vreqxml) throws Exception{
String result="";
Socket socket=null;
DataOutputStream dos=null;
DataInputStream dis=null;
try{
InetSocketAddress it = new InetSocketAddress(ip,port);
//InetSocketAddress it = new InetSocketAddress("172.16.xxxx.xxx",6688);
socket=new Socket();
logger.info("---sendMsg.timeout="+timeout);
socket.connect(it,timeout * 1000);
socket.setSoTimeout(timeout * 1000);
DigitSignature ds=new DigitSignature("C:\\icbc\\");
byte[] data_sign= ds.genValiInfo(vreqxml.getBytes("utf-8") );
dos=new DataOutputStream(socket.getOutputStream());
byte[] data=new byte[vreqxml.getBytes("utf-8").length+data_sign.length];
System.arraycopy(vreqxml.getBytes("utf-8"),0,data,0,vreqxml.getBytes("utf-8").length);
System.arraycopy(data_sign,0,data,vreqxml.getBytes("utf-8").length,data_sign.length);
logger.info("data="+ HexTool.bytesToHexString(data) );
logger.info("data_sign="+ HexTool.bytesToHexString(data_sign) );
int length=data.length;
logger.info("length="+data.length);
dos.writeInt(length);
//dos.write(int2byte(length),0,4);
dos.write(data, 0, data.length);
dos.flush();
while (true) {
dis=new DataInputStream(socket.getInputStream());
int rsplength=dis.readInt();
logger.info("rsplength="+rsplength);
byte rspbyte[]=new byte[rsplength];
byte content[]=new byte[rsplength-128];
dis.read(rspbyte);
logger.info("content-->"+content.length);
System.arraycopy(rspbyte,0,content,0,rsplength-128);
result=new String(content,"utf-8");
logger.info("rsp="+result);
break;
}
dis.close();
dos.close();
}catch(Exception e){
e.printStackTrace();
dis.close();
dos.close();
socket.close();
throw new Exception ("请求出现异常:"+e.toString());
}finally{
if(dis!=null)dis.close();
if(dos!=null)dos.close();
if(socket!=null)socket.close();
}
return result;
}
简单例子