环境:java8
- HttpURLConnection
// 只有输出
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:50000/users?status=0") ;
HttpURLConnection con = (HttpURLConnection) url.openConnection() ;
con.setDoInput(true) ;
con.setDoOutput(true) ;
con.setRequestMethod("GET") ;
con.setConnectTimeout(10000) ;
con.setReadTimeout(10000) ;
con.setRequestProperty("Content-Type", "application/json") ;
InputStream is = con.getInputStream() ;
BufferedReader br = new BufferedReader(new InputStreamReader(is)) ;
String line = null ;
while((line = br.readLine()) != null) {
System.out.println(line) ;
}
br.close();
is.close();
con.disconnect() ;
}
// 有输入也有输出
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:50000/users") ;
HttpURLConnection con = (HttpURLConnection) url.openConnection() ;
con.setDoInput(true) ;
con.setDoOutput(true) ;
con.setRequestMethod("POST") ;
con.setConnectTimeout(10000) ;
con.setReadTimeout(10000) ;
con.setRequestProperty("Content-Type", "application/json") ;
con.setRequestProperty("Accpet", "application/json") ;
OutputStream os = con.getOutputStream() ;
os.write("{\"username\": \"NB409\", \"password\": \"7777\", \"idNo\": \"7777\"}".getBytes()) ;
os.flush() ;
InputStream is = con.getInputStream() ;
BufferedReader br = new BufferedReader(new InputStreamReader(is)) ;
String line = null ;
while((line = br.readLine()) != null) {
System.out.println(line) ;
}
br.close();
is.close();
con.disconnect() ;
}
返回结果:
- Apache—HttpClient
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
public static void get(String url) throws Exception {
HttpClient httpClient = new HttpClient();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
GetMethod method = new GetMethod(url);
method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
method.setQueryString("status=0");
int statusCode = httpClient.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println(method.getStatusLine());
}
byte[] responseBody = method.getResponseBody();
System.out.println(new String(responseBody));
}
public static String post(String url) throws Exception {
HttpClient httpClient = new HttpClient();
PostMethod method = new PostMethod(url);
method.addRequestHeader("Accept", "application/json;charset=utf-8");
method.addRequestHeader("Content-Type", "application/json;chrset=utf-8");
RequestEntity requestEntity = new StringRequestEntity("{\"username\": \"NB509\", \"password\": \"7777\", \"idNo\": \"7777\"}",
"application/json", "UTF-8") ;
method.setRequestEntity(requestEntity) ;
String res = "";
int code = httpClient.executeMethod(method);
if (code == HttpStatus.SC_OK) {
res = method.getResponseBodyAsString();
System.out.println(res);
}
return res;
}
public static void main(String[] args) throws Exception {
get("http://localhost:50000/users");
post("http://localhost:50000/users") ;
}
- apache—CloseableHttpClient
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
public static void get(String url) throws Exception{
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet get = new HttpGet(url);
HttpResponse response = httpClient.execute(get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
String res = EntityUtils.toString(response.getEntity());
System.out.println(res) ;
}
}
public static void post(String url) throws Exception {
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
StringEntity s = new StringEntity("{\"username\": \"NB609\", \"password\": \"7777\", \"idNo\": \"7777\"}");
s.setContentEncoding("UTF-8");
s.setContentType("application/json;charset=utf-8");
post.setEntity(s);
HttpResponse response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
String res = EntityUtils.toString(response.getEntity());
System.out.println(res);
}
}
public static void main(String[] args) throws Exception {
get("http://localhost:50000/users?status=0");
post("http://localhost:50000/users") ;
}
- RestTemplate
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
public static void main(String[] args) {
RestTemplateBuilder builder = new RestTemplateBuilder();
RestTemplate template = builder.build() ;
System.out.println(template.getForObject("http://localhost:50000/users?status=0", Object.class)) ;
}
给个关注+转发呗,谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/80039.html