是因为 , 我在 用了 int 管道之后, 又 用了 out 管道, 所以报错了, 没测试。。。 又坑了。
public static String sendPostRequest(String path,String param) throws IOException {
String sendUrlString=path+param;
URL url = new URL(sendUrlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置post方式请求
conn.setRequestMethod("POST");
//将超时控制在 8S 内,因为没有 引入 多线程发送。为了防止RTX 那边有问题,延迟太久了,对用户体验不太好。
// 一般 反应是很快的,所以不考虑 使用线程池了。没必要搞复杂。
conn.setReadTimeout(3000); // 读取 超时 3秒 。
conn.setConnectTimeout(5000);//连接超时 5秒 。
//conn.setRequestProperty("Accept-Charset", "utf-8");
//conn.setRequestProperty("contentType", "utf-8");
conn.setRequestProperty("Content-type", "application/x-java-serialized-object");
//设置传入参数,post
conn.setDoOutput(true);
conn.connect(); // 主动建立间接 。,默认也是会建立的。
//传入参数
//conn.getOutputStream().write(param.toString().getBytes());
// 解决中文乱码, 在 Linux 下没有乱码,本地有 ,估计 Linux 已经 帮我们转码了一次了。
// 而且 Linux 下 的默认编码并没有 和 本地的一致,比如tomcat的啊, 系统的啊,没有 该为 默认中文的等。
// 既然这样 在 Linux 下没有 乱码,可以不用管,如果以后 可以 去 修改 服务器里面的 编码。
//BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
// 解决中文乱码,在 Linux 下有乱码,本地没有
//BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"GBK"));
// out 管道 必须放在 int 管道之前 使用的。这个是 HRRt 规定的
//可以不主动关闭, 默认会自动关闭的。特别是 随着 int 管道关闭同时关闭的。
conn.getOutputStream().flush(); // 这个最好有
conn.getOutputStream().close(); // 关闭代码可以不用。
InputStreamReader isreader = new InputStreamReader(conn.getInputStream());
BufferedReader reader =
new BufferedReader(isreader);
String readingString= "";
// 读取完所有内容
String readl=null;
while (( readl=reader.readLine() )!=null) {
readingString=readingString+readl;
readl=null;
}
// 必须放在 int 管道之前 使用的。, 不要放在 这里,会报错: Cannot write output after reading input.
//conn.getOutputStream().close();
// 养成 关闭 管道的习惯 。
conn.getInputStream().close();
isreader.close();
reader.close();
return readingString;
}
参考地址: http://www.blogjava.net/supercrsky/articles/247449.html
参考地址: http://blog.csdn.net/u010278162/article/details/51054664