标签:
废话不多说,直接上code.
package mytest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import java.util.Map; import java.util.Set; import java.io.InputStream; public class MainEntry { public static void main(String[] args) throws Exception { String urlStr = "http://passport.cnblogs.com/user/signin"; URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); StringBuffer params = new StringBuffer(); params.append("input1") .append("=") .append("lucas.shanghai") .append("&") .append("input2") .append("=") .append("abc"); byte[] bytes = params.toString().getBytes(); PrintWriter printWriter = new PrintWriter(conn.getOutputStream()); printWriter.write(params.toString()); printWriter.flush(); Map headers = conn.getHeaderFields(); Set<String> keys = headers.keySet(); for(String key : keys) { System.out.println(key + ": " + conn.getHeaderField(key)); } int responseCode = conn.getResponseCode(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = ""; StringBuffer responseResult = new StringBuffer(); while((line = bufferedReader.readLine()) != null) { responseResult.append("/n").append(line); } System.out.println(responseResult.toString()); } }
标签:
原文地址:http://www.cnblogs.com/devtesters/p/4971792.html