标签:request eal tac gen [] ace imp live user
package testHttp; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; public class PostAndGet { public static void main(String[] args) { sendGet("http://www.baidu.com"); } // Get public static void sendGet(String url) { BufferedReader bufferedReader = null; try { URL realurl = new URL(url); URLConnection openConnection = realurl.openConnection(); openConnection.setRequestProperty("accept", "*/*"); openConnection.setRequestProperty("connection", "Keep-Alive"); openConnection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); bufferedReader = new BufferedReader(new InputStreamReader(openConnection.getInputStream(), "gbk")); String line = null; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (bufferedReader != null) bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } // Post public static void sendPost(String url, String param) { BufferedReader bufferedReader = null; try { URL realurl = new URL(url); URLConnection openConnection = realurl.openConnection(); openConnection.setRequestProperty("accept", "*/*"); openConnection.setRequestProperty("connection", "Keep-Alive"); openConnection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); openConnection.setDoOutput(true); openConnection.setDoInput(true); byte[] bytes = param.getBytes(); openConnection.getOutputStream().write(bytes); openConnection.getOutputStream().flush(); bufferedReader = new BufferedReader(new InputStreamReader(openConnection.getInputStream(), "utf-8")); String line = null; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (bufferedReader != null) bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
标签:request eal tac gen [] ace imp live user
原文地址:http://www.cnblogs.com/jianxiaopo/p/7831503.html