用的HttpUrlConnection模拟请求
但是请求的接口,想获得inputStream为空,
解决方法:
新增Content-Type请求头,明确告诉服务器处理什么样的数据。
conn.setRequestProperty("content-type", "text/html");
URL serverUrl = new URL("");
HttpURLConnection conn = (HttpURLConnection) serverUrl.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.connect();
String params = "hello";
conn.getOutputStream().write(params.getBytes());
conn.getOutputStream().flush();
conn.getOutputStream().close();
InputStream is = conn.getInputStream();
但是请求的接口,想获得inputStream为空,
解决方法:
新增Content-Type请求头,明确告诉服务器处理什么样的数据。
conn.setRequestProperty("content-type", "text/html");
本文介绍了一种常见的HTTP POST请求中inputStream为空的问题及其解决方案。通过在HTTP请求头中增加Content-Type属性并设置为text/html,明确告知服务器处理的数据类型,从而解决了请求接口时inputStream为空的问题。
2335





