Chinaunix首页 | 论坛 | 博客
  • 博客访问: 399212
  • 博文数量: 93
  • 博客积分: 3006
  • 博客等级: 少校
  • 技术积分: 998
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-02 17:23
文章分类

全部博文(93)

文章存档

2011年(12)

2010年(38)

2009年(20)

2008年(23)

分类: 系统运维

2010-09-26 10:41:49

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("","id=960"); // System.out.println(s); String s1=TestGetPost.sendPost("","type_id=1&start=50&thRange=50"); System.out.println(s1); } }
阅读(2002) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-09-27 11:05:28

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com