今天调试一个http接口,调用端代码如下
URL url = new URL("http://www.AAA.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
ObjectOutputStream out = new ObjectOutputStream(connection.getOutputStream());
Map map = new HashMap();
map.put("type", "ttt");
out.writeObject(map);
out.flush();
out.close();
服务端代码如下:
InputStream is = ####;
ObjectInputStream ois = new ObjectInputStream(is);//异常产生出
Map map = (Map) ois.readObject();
ois.close();
is.close();
服务端在异常产生出报invalid stream header异常,跟踪代码发现是ObjectInputStream(InputStream in)方法中会检查stream的头信息readStreamHeader(),头信息异常导致invalid stream header产生,检查调用端的代码发现没有设置Content-Type,因为是用序列号的二进制流,类型为application/xml,加上一句connection.setRequestProperty("Content-Type", "application/xml"),问题解决