param 为aaa=111&bbb=222.....格式 import java.io.*; import java.net.*; public class TestGetPost{ //发送带参数的GET请求 public static String sendGet(String url,String param) { String result = ""; try{ String urlName = url + "?"+param; URL U = new URL(urlName); URLConnection connection = U.openConnection(); connection.connect(); |
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine())!= null) { result += line; } in.close(); }catch(Exception e){ System.out.println("没有结果!"+e); } return result; } //发送带参数的Post请求
public static String sendPost(String url,String param){ String result=""; try{ URL httpurl = new URL(url); HttpURLConnection httpConn = (HttpURLConnection)httpurl.openConnection(); httpConn.setDoOutput(true); httpConn.setDoInput(true); PrintWriter out = new PrintWriter(httpConn.getOutputStream()); out.print(param); out.flush(); out.close(); BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream())); String line; while ((line = in.readLine())!= null) { result += line; } in.close(); }catch(Exception e){ System.out.println("没有结果!"+e); } return result; }
//测试 public static void main(String args[]){ // String s=TestGetPost.sendGet("http://www.java3z.com/cwbwebhome/article/article2/2340.jsp","id=960"); // System.out.println(s); String s1=TestGetPost.sendPost("http://www.java3z.com/cwbwebhome/learn.jsp","type_id=1&start=50&thRange=50"); System.out.println(s1); } }