前言:因实际需求,第三方提供了一个IP一个端口,加一段报文和报文参数的说明就需要开发,调用接口。最开始也是懵,后来才醒悟是socket
tcp请求不需要太难
String info = "";
try {
//1.建立连接
Socket sck = new Socket(ip,端口);
//2.传输内容
OutputStream os = sck.getOutputStream();
PrintWriter pw =new PrintWriter(os);//将输出流包装成打印流
pw.write(generateXml(linWXml)); //组装请求的xml报文
pw.flush();
sck.shutdownOutput();
//3.获取返回内容
InputStream is = sck.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null){
buffer.append(line);
}
info = buffer.toString();
//4.关闭连接
br.close();
sck.close();
is.close();
pw.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
log.info(info);
然后看结果,对于报文是xml返回的基本上也是xml,需要对xml进行转换等处理,方法网上一大堆。。