如何使用JAVA请求HTTP
package com.st.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import com.st.util.DesEcbUtil; /** * @description * * @author wangkang * * @date 2017-5-18 下午3:02:20 * * @version 1.0 */ public class TestHttps { /** * @description * * @author wangkang * @throws IOException * @throws UnsupportedEncodingException * * @date 2017-5-18 下午3:02:20 */ public static void main(String[] args) throws UnsupportedEncodingException, IOException {
String testUrl = "http://*"; URL url = new URL(testUrl); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.getOutputStream().write("请求内容".getBytes("GBK")); InputStream inStream=conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"UTF-8")); String line = null; StringBuffer buffer = new StringBuffer(); while((line = reader.readLine()) != null){ buffer.append(line); } System.out.println("结果:"+buffer.toString()); conn.disconnect(); } }