你是一名java程序员,请用apache http 封装一个HttpUtils工具类
时间: 2024-01-16 07:02:23 浏览: 153
Java实现Http工具类的封装操作示例
好的,我可以为你编写一个简单的 `HttpUtils` 工具类,使用 Apache HttpClient 来进行 HTTP 请求的封装。以下是一个示例:
```java
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
public class HttpUtils {
private static final String DEFAULT_CHARSET = "UTF-8";
public static String sendGetRequest(String url) throws IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
HttpResponse response = httpClient.execute(request);
return extractResponse(response);
}
public static String sendGetRequestWithParams(String url, List<NameValuePair> params) throws URISyntaxException, IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
URIBuilder uriBuilder = new URIBuilder(url);
uriBuilder.addParameters(params);
URI uri = uriBuilder.build();
HttpGet request = new HttpGet(uri);
HttpResponse response = httpClient.execute(request);
return extractResponse(response);
}
public static String sendPostRequest(String url, List<NameValuePair> params) throws IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(url);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, DEFAULT_CHARSET);
request.setEntity(entity);
HttpResponse response = httpClient.execute(request);
return extractResponse(response);
}
private static String extractResponse(HttpResponse response) throws IOException {
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toString(entity, DEFAULT_CHARSET);
} else {
return null;
}
}
// 示例用法
public static void main(String[] args) {
try {
// 发送 GET 请求
String getResponse = HttpUtils.sendGetRequest("https://www.example.com");
System.out.println("GET 响应:" + getResponse);
// 发送带参数的 GET 请求
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("param1", "value1"));
params.add(new BasicNameValuePair("param2", "value2"));
String getWithParamsResponse = HttpUtils.sendGetRequestWithParams("https://www.example.com", params);
System.out.println("带参数的 GET 响应:" + getWithParamsResponse);
// 发送 POST 请求
List<NameValuePair> postParams = new ArrayList<>();
postParams.add(new BasicNameValuePair("param1", "value1"));
postParams.add(new BasicNameValuePair("param2", "value2"));
String postResponse = HttpUtils.sendPostRequest("https://www.example.com", postParams);
System.out.println("POST 响应:" + postResponse);
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
}
}
```
在这个示例中,`HttpUtils` 类提供了三个静态方法来发送 GET 和 POST 请求,并返回响应结果。你可以根据需要添加其他方法和参数验证逻辑。请注意,示例中使用了默认的字符集编码为 UTF-8,你可以根据实际情况进行调整。
阅读全文