public static String do_post(String url) throws IOException {
String body = "{}";
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httpost = new HttpPost(url);
// httpost.setEntity(new UrlEncodedFormEntity(name_value_pair, StandardCharsets.UTF_8));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
} finally {
httpclient.getConnectionManager().shutdown();
}
return body;
}
public static String do_get(String url) throws 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) throws IOException{
String body = do_get("http://localhost:8080/crsWebApp/backoffice/Group/show.do?id=1");
System.out.println(body);
}
本文提供了一个简单的Java示例,展示了如何使用HttpClient库发起GET和POST请求,并获取响应内容。示例中包括了基本的错误处理。
1885

被折叠的 条评论
为什么被折叠?



