记录一次socket交互

完整的请求调用

 String encoding = "UTF-8";
        String requestXml = "";
        Socket socket = null;
        DataOutputStream out = null;
        InputStream in =null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        try {
            socket = new Socket("127.0.0.1",8080);
            out = new DataOutputStream(socket.getOutputStream());
            byte[] paramsBytes = requestXml.getBytes(encoding);
            out.write(paramsBytes);
            out.flush();
            System.out.println("从服务端接收数据");
            in = socket.getInputStream();
            inputStreamReader = new InputStreamReader(in,encoding);
            bufferedReader = new BufferedReader(inputStreamReader);
            String s = null;
            StringBuffer stringBuffer = new StringBuffer();
            while ((s = bufferedReader.readLine()) != null){
                stringBuffer.append(s);
            }
            String receiveXml = stringBuffer.toString();
            System.out.println("获取到的相应报文"+receiveXml);
        }catch (Exception e){
            System.out.println("发生错误了");
        }finally {
            if(bufferedReader != null){
                bufferedReader.close();
            }
            if(inputStreamReader != null){
                inputStreamReader.close();
            }
            if(in != null){
                in.close();
            }
            if(out != null){
                out.close();
            }
            if(socket != null){
                socket.close();
            }
        }

获取到的相应报文如下所示

 String response = "
111
";

解析报文

Bob o = xmlToBean(response, Bob.class);
public static  T xmlToBean(String xml,Class load) throws Exception{
    JAXBContext jaxbContext = JAXBContext.newInstance(load);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    return (T)unmarshaller.unmarshal(new StringReader(xml));
}

i

mport javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;

//这个很重要,表明头元素是谁
@XmlRootElement(name = "bob")
@XmlAccessorType(XmlAccessType.FIELD)
public class Bob implements Serializable {

    private Header header;

    private Body body;

    public Header getHeader() {
        return header;
    }

    public void setHeader(Header header) {
        this.header = header;
    }

    public Body getBody() {
        return body;
    }

    public void setBody(Body body) {
        this.body = body;
    }

    @Override
    public String toString() {
        return "Bob{" +
                "header=" + header +
                ", body=" + body +
                '}';
    }
}
import javax.xml.bind.annotation.XmlAccessType;`在这里插入代码片`
import javax.xml.bind.annotation.XmlAccessorType;
import java.io.Serializable;

@XmlAccessorType(XmlAccessType.FIELD)
public class Body implements Serializable {
    @Override
    public String toString() {
        return "Body{}";
    }
}

解析结果如下

Bob{header=Header{txCode='111'}, body=Body{}}

你可能感兴趣的:(java,servlet,开发语言)