get和post请求

来源:互联网 发布:软件测试工作怎么样 编辑:程序博客网 时间:2024/05/22 18:00

GET请求

public String doGet(String url) throws IOException{        HttpClient client = new HttpClient();        GetMethod get = new GetMethod(url);        String text = "";        try {            client.executeMethod(get);            text  = get.getResponseBodyAsString();        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally{            get.releaseConnection();        }        return text;    }



POST请求
 
  
 public String doPost(String url){       HttpClient client = new HttpClient();       PostMethod post = new PostMethod(url);       post.setParameter("OrderCode", "");       post.setParameter("ShipperCode", "SF");       post.setParameter("LogisticCode", "118650888018");       String restString = "";       try {        client.executeMethod(post);        restString = post.getResponseBodyAsString();       } catch (HttpException e) {        // TODO Auto-generated catch block        e.printStackTrace();      } catch (IOException e) {        // TODO Auto-generated catch block        e.printStackTrace();      }        return restString;    }