如何通过url.openStream()发送POST数据?
我正在寻找教程或快速示例,我如何发送POST数据抛出openStream。
我的代码是:
URL url = new URL("http://localhost:8080/test"); InputStream response = url.openStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(response, "UTF-8"));
你可以帮帮我吗 ?
URL url = new URL(urlSpec); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(method); connection.setDoOutput(true); connection.setDoInput(true); // important: get output stream before input stream OutputStream out = connection.getOutputStream(); out.write(content); out.close(); // now you can get input stream and read. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { writer.println(line); }
使用Apache HTTP Compennts http://hc.apache.org/httpcomponents-client-ga/
教程: http : //hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html
寻找HttpPost – 有一些发送动态数据,文本,文件和表单数据的例子。
特别是Apache HTTP Components , 客户端将是最好的方式。 它吸收了很多你通常不得不亲手做的令人讨厌的编码