首页 > 代码库 > Post And Get接口测试
Post And Get接口测试
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接口测试
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。