我来为您详细讲解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请求的几种方式(附源码)
基础教程推荐
猜你喜欢
- Ajax请求响应中用window.open打开新窗口被拦截的解决方法 2023-01-20
- CSS简单实现网页悬浮效果(对联广告) 2023-12-21
- 上传头像后导航栏中头像同步(Vue中监听sessionStorage) 2023-10-08
- jquery实现网页定位导航 2023-12-09
- 兼容Firefox和IE的onpropertychange事件oninput 2023-12-13
- vue实现三级页面跳转功能 2023-07-09
- js实现获取鼠标当前的位置 2023-11-30
- JS的鼠标监听mouseup鼠标抬起失效原因及解决 2023-07-10
- TWebBrowser 与 MSHTML(2): 获取 window 对象的时机 2023-10-26
- Spring Boot + Vue3 前后端分离实战wiki知识库系统 2023-10-08
