- 1 byte[] body = new byte[512]; // 需要发送的body数据
- 2 URL url = new URL(buildSpiderURL(host));
- 3 HttpURLConnection conn = null;
- 4
- try {
- 5 conn = (HttpURLConnection) url.openConnection();
- 6 conn.setRequestMethod("POST");
- 7 conn.setRequestProperty(key, value); // 设置消息头
- 8 conn.setDoOutput(true); // post发送数据时必须设置为true
- 9 conn.connect();
- 10 conn.getOutputStream().write(body);
- 11 conn.getOutputStream().flush();
- 12 conn.getOutputStream().close();
- 13
- } catch(Exception ex) {
- 14 //todo something
- 15
- } finally {
- 16 conn.disconnect();
- 17
- }
来源: