直接上代码
服务端:
@RestController public class Api { @RequestMapping(value = "/api",method = RequestMethod.POST) public byte[] getbytes(HttpServletRequest request) throws IOException { byte[] inbytes=this.tobytes(request); byte[] outbytes=new byte[10]; outbytes[0]=1; return outbytes; } byte[] tobytes(HttpServletRequest request){ String needkey=""; for(String key : request.getParameterMap().keySet()){ needkey=key; } return needkey.getBytes(); } }
客户端:
public static byte[] WFP_SYS_BytesCA(String serverAdd, byte[] argsBts) throws Exception { HttpURLConnection conn = null; try { String surl = serverAdd; URL url = new URL(surl); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); OutputStream outStream = conn.getOutputStream(); outStream.write(argsBts); outStream.close(); InputStream inStream = conn.getInputStream(); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int n = 0; while (-1 != (n = inStream.read(buffer))) { output.write(buffer, 0, n); } return output.toByteArray(); } finally { conn.disconnect(); } }