首页 > 其他 > 详细

Post And Get接口测试

时间:2016-11-19 23:28:26      阅读:225      评论:0      收藏:0      [点我收藏+]
public class TestPost {
 
public static String do_post(String url, Map<String, String> params) throws IOException {
  String body = "{}";
  DefaultHttpClient httpclient = new DefaultHttpClient();
  try {
  HttpPost httpost = new HttpPost(url);
  //添加参数
  params.put("", "");
  params.put("", "");
  params.put("", "");
  params.put("", "");
  params.put("", "");
  params.put("", "");
  //设置格式
  //JSONObject jo = JSONObject.fromObject(params);
  JSONObject jo = new JSONObject(params);
  StringEntity entity = new StringEntity(jo.toString(), "UTF-8");
  httpost.setEntity(entity);
 
  HttpResponse response = httpclient.execute(httpost);
  HttpEntity rEntity = response.getEntity();
  body = EntityUtils.toString(rEntity);
  System.out.println(body);
  } finally {
  httpclient.getConnectionManager().shutdown();
  }
  return body;
  }
 
  public static String do_get(String url) throws ClientProtocolException, IOException {
  String body = "{}";
  DefaultHttpClient httpclient = new DefaultHttpClient();
  try {
  HttpGet httpget = new HttpGet(url);
  HttpResponse response = httpclient.execute(httpget);
  HttpEntity entity = response.getEntity();
  body = EntityUtils.toString(entity);
  } finally {
  httpclient.getConnectionManager().shutdown();
  }
  return body;
  }
 
 
public static void main(String[] args) {
 
String url = "*******************************************************";
Map<String, String> params = new HashMap<String, String>();
try {
do_post(url, params);
} catch (IOException e) {
e.printStackTrace();
}
}

Post And Get接口测试

原文:http://www.cnblogs.com/fqfanqi/p/6081514.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!