我来为您详细讲解Java构造HTTP请求的几种方式。
我来为您详细讲解"Java构造HTTP请求的几种方式"。
1. 使用URLConnection发送HTTP请求
使用URLConnection可以方便的发送HTTP请求。下面是一个使用URLConnection发送get请求的示例代码:
public static String sendGetRequest(String url) throws Exception {
URL getUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
connection.connect();//建立链接
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String lines;
StringBuilder sb = new StringBuilder();
while ((lines = reader.readLine()) != null) {
sb.append(lines);
}
reader.close();
connection.disconnect();
return sb.toString();
}
使用HttpURLConnection发送HttpPost请求的示例代码:
public static String sendPostRequest(String url, String data) throws Exception {
URL postUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);//如果后台需要传参,则必须设置为true,否则会抛出ProtocolException异常
connection.getOutputStream().write(data.getBytes());
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
connection.disconnect();
return sb.toString();
}
2. 使用HttpClient发送HTTP请求
使用Apache的HttpClient包发送HTTP请求也是非常方便的,相关的jar包下载地址:http://hc.apache.org/downloads.cgi。下面是一个使用HttpClient发送get请求的示例代码:
public static String sendGetRequestWithHttpClient(String url) throws Exception {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
content.close();
return sb.toString();
}
使用HttpClient发送HttpPost请求的示例代码:
public static String sendPostRequestWithHttpClient(String url, String data) throws Exception {
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
StringEntity stringEntity = new StringEntity(data);
stringEntity.setContentType("application/json");
request.setEntity(stringEntity);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
content.close();
return sb.toString();
}
以上就是关于使用Java构造HTTP请求的几种方式,希望对您有所帮助。
沃梦达教程
本文标题为:java构造http请求的几种方式(附源码)
基础教程推荐
猜你喜欢
- jQuery实现单击和鼠标感应事件 2024-01-20
- CSS 鼠标样式和手指样式整理 2024-01-23
- 使用fileReader的一个坑及解决 2023-08-11
- 图文示例讲解useState与useReducer性能区别 2023-07-10
- 通用javascript代码判断版本号是否在版本范围之间 2024-01-04
- 浅谈Ajax技术实现页面无刷新 2022-12-15
- react中ref获取dom或者组件的实现方法 2023-07-09
- CSS实现Tab布局的简单实例(必看) 2023-12-20
- 《CSS3实战》笔记--渐变设计(二) 2022-11-13
- Ajax报错400的参考解决办法 2023-02-23