Java中如何实现一个示例代码,用于封装HTTP请求的工具类?
- 内容介绍
- 文章标签
- 相关推荐
本文共计278个文字,预计阅读时间需要2分钟。
Java实现HTTP请求的常用方法有两种,一种是使用Java自带的标准化类HttpURLConnection,另一种是通过Apache的httpclient库来实现。以下是使用httpclient实现HTTP请求的示例代码,需要导入httpclient和httpcore两个jar包:
javaimport org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;
public class HttpTest { public static void main(String[] args) { CloseableHttpClient httpClient=HttpClients.createDefault(); HttpGet httpGet=new HttpGet(http://www.example.com);
try { CloseableHttpResponse response=httpClient.execute(httpGet); String result=EntityUtils.toString(response.getEntity()); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } finally { try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } }}
java实现www.hangge.com/blog/cache/detail_2020.html String uri = "jsonplaceholder.typicode.com"; //get方式请求数据 //请求地址:jsonplaceholder.typicode.com/posts @Ignore("暂时忽略") @Test public void test1() { System.out.print("\n" + "test1---------------------------"+ "\n"); HttpParamers paramers = HttpParamers.jsonplaceholder.typicode.com/posts?userId=5 @Ignore("暂时忽略") @Test public void test2() { System.out.print("\n" + "test2---------------------------"+ "\n"); HttpParamers paramers = HttpParamers.jsonplaceholder.typicode.com/posts @Test public void test3() { System.out.print("\n" + "test3---------------------------"+ "\n"); HttpParamers paramers = HttpParamers.httpPostParamers(); paramers.addParam("time", String.valueOf(System.currentTimeMillis())); String response = ""; try { HttpService httpService = new HttpService(uri); response = httpService.service("/posts", paramers); } catch (Exception e) { e.printStackTrace(); } System.out.print(response); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计278个文字,预计阅读时间需要2分钟。
Java实现HTTP请求的常用方法有两种,一种是使用Java自带的标准化类HttpURLConnection,另一种是通过Apache的httpclient库来实现。以下是使用httpclient实现HTTP请求的示例代码,需要导入httpclient和httpcore两个jar包:
javaimport org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;
public class HttpTest { public static void main(String[] args) { CloseableHttpClient httpClient=HttpClients.createDefault(); HttpGet httpGet=new HttpGet(http://www.example.com);
try { CloseableHttpResponse response=httpClient.execute(httpGet); String result=EntityUtils.toString(response.getEntity()); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } finally { try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } }}
java实现www.hangge.com/blog/cache/detail_2020.html String uri = "jsonplaceholder.typicode.com"; //get方式请求数据 //请求地址:jsonplaceholder.typicode.com/posts @Ignore("暂时忽略") @Test public void test1() { System.out.print("\n" + "test1---------------------------"+ "\n"); HttpParamers paramers = HttpParamers.jsonplaceholder.typicode.com/posts?userId=5 @Ignore("暂时忽略") @Test public void test2() { System.out.print("\n" + "test2---------------------------"+ "\n"); HttpParamers paramers = HttpParamers.jsonplaceholder.typicode.com/posts @Test public void test3() { System.out.print("\n" + "test3---------------------------"+ "\n"); HttpParamers paramers = HttpParamers.httpPostParamers(); paramers.addParam("time", String.valueOf(System.currentTimeMillis())); String response = ""; try { HttpService httpService = new HttpService(uri); response = httpService.service("/posts", paramers); } catch (Exception e) { e.printStackTrace(); } System.out.print(response); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

