依赖
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.3</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.3.3</version> <scope>runtime</scope> </dependency>
HttpGet
public String getVersion() { String url = "http://***********"; HttpGet httpGet = new HttpGet( url ); HttpClient httpClient = HttpClients.createDefault(); try { HttpResponse resposne = httpClient.execute( httpGet ); int statusCode = resposne.getStatusLine().getStatusCode(); if ( statusCode == 200 ) { InputStream input = resposne.getEntity().getContent(); byte[] buf = new byte[ input.available() ]; input.read( buf ); String result = new String( buf ); url = url + " ---> ############## ---> " + result; } else { } } catch ( Exception e ) { e.printStackTrace(); } return url; }
HttpPost
public String doPost( String param1, String param2, String param3 ) { String url = "http://***********"; HttpPost httpPost = new HttpPost( url ); List< NameValuePair > values = new ArrayList< NameValuePair >(); { values.add( new BasicNameValuePair( "param1", param1 ) ); values.add( new BasicNameValuePair( "param2", param2 ) ); values.add( new BasicNameValuePair( "param3", param3 ) ); } try { httpPost.setEntity( new UrlEncodedFormEntity( values, "UTF-8" ) ); HttpClient httpClient = HttpClients.createDefault(); HttpResponse resposne = httpClient.execute( httpPost ); int statusCode = resposne.getStatusLine().getStatusCode(); if ( statusCode == 200 ) { InputStream input = resposne.getEntity().getContent(); byte[] buf = new byte[ input.available() ]; input.read( buf ); String result = new String( buf ); url = url + " ---> ############## ---> " + result; } else { } } catch ( Exception e ) { e.printStackTrace(); } return url; }
相关推荐
利用这两个jar包,很简单的用java实现httpget和httppost请求。
利用httpclient-4.3.1.jar、httpcore-4.3.jar包,很简单的用java实现httpget和httppost请求。
1. `httpclient.jar`:这是HttpClient的主要库,提供了HTTP请求和响应处理的类和接口,包括`HttpClient`、`HttpGet`和`HttpPost`等。 2. `httpcore.jar`:这是HttpClient的核心库,包含了网络通信的基本组件,如...
HttpPost httpPost = new HttpPost("https://example.com/api"); String jsonInputString = "{\"key\":\"value\"}"; StringEntity input = new StringEntity(jsonInputString); input.setContentType(...
java调用HTTP接口(Get请求和Post请求)
本篇文章将深入探讨“Java HttpGetServer HTTP服务器”的相关知识点,主要涉及如何利用Java实现一个简单的HTTP服务器,理解HTTP请求与响应的基本原理,并探讨源码和实用工具。 首先,我们需要了解HTTP(超文本传输...
java实现调用httpclient接口的类和方法,包括了get和post传参方式,简单易懂
javahttp的简单使用。javaHttp的get与post自带api简单实现。java get post的使用记录
在Java中,可以使用`HttpGet`类来创建一个GET请求。以下是一个简单的示例: ```java import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org....
HttpGet httpGet = new HttpGet("http://example.com"); // 执行请求并获取响应 HttpResponse response = httpClient.execute(httpGet); // 处理响应 int statusLine = response.getStatusLine()....
在Java编程中,GET和POST是HTTP协议中最常见的两种请求方法。它们用于向服务器发送数据,获取响应,是Web应用程序的基础。本篇文章将详细介绍如何在Java中实现GET和POST请求,以及相关的知识点。 首先,我们要了解...
总的来说,HttpClient是Java开发者进行HTTP通信的强大工具,而HttpPost和HttpGet是其主要的请求方式。确保正确引入并配置所需的jar包,可以让我们轻松地与Web服务进行数据交换。在实际项目中,根据需求,可能还需要...
HttpGet httpGet = new HttpGet(requestUrl); // 构建HttpGet对象并设置请求URL try { HttpResponse response = httpClient.execute(httpGet); // 执行GET请求 HttpEntity entity = response.getEntity(); // ...
JAVA 调用HTTP接口POST或GET实现方式,java通用
HttpPost httpPost = new HttpPost("https://example.com"); List<NameValuePair> params = new ArrayList(); params.add(new BasicNameValuePair("key", "value")); httpPost.setEntity(new UrlEncodedFormEntity...
HttpPost httpPost = new HttpPost("http://example.com"); List<NameValuePair> nameValuePairs = new ArrayList(); nameValuePairs.add(new BasicNameValuePair("param1", "value1")); nameValuePairs.add...
在 `doPost` 方法中,我们首先创建了一个 `CloseableHttpClient` 对象,然后使用 `HttpPost` 对象来发送 POST 请求。在这个示例中,我们使用 `UrlEncodedFormEntity` 对象来指定请求体。 使用 Apache HttpClient 库...
HttpPost httpPost = new HttpPost(url); httpPost.setEntity(httpEntity); CloseableHttpResponse response = httpClient.execute(httpPost); try { // 处理响应 // ... } finally { response.close(); }...
HttpPost httpPost = new HttpPost("http://example.com"); List<NameValuePair> params = new ArrayList(); params.add(new BasicNameValuePair("key1", "value1")); params.add(new BasicNameValuePair("key2", ...