日期:2014-05-17 浏览次数:20620 次
public String reuqestTrade(String request, String url, String port, String encode) { String str = "ERROR"; Socket socket = null; OutputStream os; InputStream is; try { socket = new Socket(); // 客户端给出IP和端口号 SocketAddress socketAddress = new InetSocketAddress(url, Integer.parseInt(port.trim())); socket.connect(socketAddress, Integer.parseInt(Help .getUrl("timeout"))); log.info("socket(客服端未限制端口) 已连接-->" + url + ":" + port); is = socket.getInputStream(); os = socket.getOutputStream(); os.write(request.getBytes()); os.flush(); byte[] serByte = new byte[2048]; String serverStr = null; int flag = is.read(serByte); if (flag != -1) { serverStr = new String(serByte); } log.info(" answer --> " + serverStr); return serverStr; } catch (UnknownHostException e) { log.error("systemerr", e); str = "ERROR"; return str; } catch (IOException e) { log.error("systemerr", e); str = "ERROR"; return str; } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { log.error("systemerr", e); str = "ERROR"; return str; } } } }