What are the ways to send HTTP requests in JAVA?
1. HttpURLConnection
Use the net provided by the JDK natively, without the need for other jar packages. The code is as follows:
import com.alibaba.fastjson.JSON; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; public class HttpTest1 { public static void main(String[] args) { HttpURLConnection con = null; BufferedReader buffer = null; StringBuffer resultBuffer = null; try { URL url = new URL("http://10.30.10.151:8012/gateway.do"); //得到连接对象 con = (HttpURLConnection) url.openConnection(); //设置请求类型 con.setRequestMethod("POST"); //设置Content-Type,此处根据实际情况确定 con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //允许写出 con.setDoOutput(true); //允许读入 con.setDoInput(true); //不使用缓存 con.setUseCaches(false); OutputStream os = con.getOutputStream(); Map paraMap = new HashMap(); paraMap.put("type", "wx"); paraMap.put("mchid", "10101"); //组装入参 os.write(("consumerAppId=test&serviceName=queryMerchantService¶ms=" + JSON.toJSONString(paraMap)).getBytes()); //得到响应码 int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { //得到响应流 InputStream inputStream = con.getInputStream(); //将响应流转换成字符串 resultBuffer = new StringBuffer(); String line; buffer = new BufferedReader(new InputStreamReader(inputStream, "GBK")); while ((line = buffer.readLine()) != null) { resultBuffer.append(line); } System.out.println("result:" + resultBuffer.toString()); } } catch (Exception e) { e.printStackTrace(); } } }
2. HttpClient
You need to use commons-httpclient- 3.1.jar, the maven dependencies are as follows:
<dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency>
The code is as follows:
import com.alibaba.fastjson.JSON; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class HttpTest2 { public static void main(String[] args) { HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod("http://10.30.10.151:8012/gateway.do"); postMethod.addRequestHeader("accept", "*/*"); //设置Content-Type,此处根据实际情况确定 postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //必须设置下面这个Header //添加请求参数 Map paraMap = new HashMap(); paraMap.put("type", "wx"); paraMap.put("mchid", "10101"); postMethod.addParameter("consumerAppId", "test"); postMethod.addParameter("serviceName", "queryMerchantService"); postMethod.addParameter("params", JSON.toJSONString(paraMap)); String result = ""; try { int code = httpClient.executeMethod(postMethod); if (code == 200){ result = postMethod.getResponseBodyAsString(); System.out.println("result:" + result); } } catch (IOException e) { e.printStackTrace(); } } }
3. CloseableHttpClient
needs to use httpclient-4.5.6.jar, the maven dependencies are as follows:
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.6</version> </dependency>
The code is as follows:
import com.alibaba.fastjson.JSON; import org.apache.http.Consts; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class HttpTest3 { public static void main(String[] args) { int timeout = 120000; CloseableHttpClient httpClient = HttpClients.createDefault(); RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(timeout) .setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build(); HttpPost httpPost = null; List<NameValuePair> nvps = null; CloseableHttpResponse responses = null;// 命名冲突,换一个名字,response HttpEntity resEntity = null; String result; try { httpPost = new HttpPost("http://10.30.10.151:8012/gateway.do"); httpPost.setConfig(defaultRequestConfig); Map paraMap = new HashMap(); paraMap.put("type", "wx"); paraMap.put("mchid", "10101"); nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("consumerAppId", "test")); nvps.add(new BasicNameValuePair("serviceName", "queryMerchantService")); nvps.add(new BasicNameValuePair("params", JSON.toJSONString(paraMap))); httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); responses = httpClient.execute(httpPost); resEntity = responses.getEntity(); result = EntityUtils.toString(resEntity, Consts.UTF_8); EntityUtils.consume(resEntity); System.out.println("result:" + result); } catch (Exception e) { e.printStackTrace(); } finally { try { responses.close(); httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } } }
4. okhttp
needs to use okhttp-3.10.0.jar, and the maven dependency is as follows:
<dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.10.0</version> </dependency>
The code is as follows:
import com.alibaba.fastjson.JSON; import okhttp3.*; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class HttpTest4 { public static void main(String[] args) throws IOException { String url = "http://10.30.10.151:8012/gateway.do"; OkHttpClient client = new OkHttpClient(); Map paraMap = new HashMap(); paraMap.put("yybh", "1231231"); RequestBody requestBody = new MultipartBody.Builder() .addFormDataPart("consumerAppId", "tst") .addFormDataPart("serviceName", "queryCipher") .addFormDataPart("params", JSON.toJSONString(paraMap)) .build(); Request request = new Request.Builder() .url(url) .post(requestBody) .addHeader("Content-Type", "application/x-www-form-urlencoded") .build(); Response response = client .newCall(request) .execute(); if (response.isSuccessful()) { System.out.println("result:" + response.body().string()); } else { throw new IOException("Unexpected code " + response); } } }
5. Socket
Use the net provided by the JDK natively, no other jar package required
The code is as follows:
import com.alibaba.fastjson.JSON; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.net.Socket; import java.net.URLEncoder; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; public class HttpTest6 { private static String encoding = "utf-8"; public static void main(String[] args) { try { Map paraMap = new HashMap(); paraMap.put("yybh", "12312311"); String data = URLEncoder.encode("consumerAppId", "utf-8") + "=" + URLEncoder.encode("test", "utf-8") + "&" + URLEncoder.encode("serviceName", "utf-8") + "=" + URLEncoder.encode("queryCipher", "utf-8") + "&" + URLEncoder.encode("params", "utf-8") + "=" + URLEncoder.encode(JSON.toJSONString(paraMap), "utf-8"); Socket s = new Socket("10.30.10.151", 8012); OutputStreamWriter osw = new OutputStreamWriter(s.getOutputStream()); StringBuffer sb = new StringBuffer(); sb.append("POST /gateway.do HTTP/1.1\r\n"); sb.append("Host: 10.30.10.151:8012\r\n"); sb.append("Content-Length: " + data.length() + "\r\n"); sb.append("Content-Type: application/x-www-form-urlencoded\r\n"); //注,这里很关键。这里一定要一个回车换行,表示消息头完,不然服务器会等待 sb.append("\r\n"); osw.write(sb.toString()); osw.write(data); osw.write("\r\n"); osw.flush(); //--输出服务器传回的消息的头信息 InputStream is = s.getInputStream(); String line = null; int contentLength = 0;//服务器发送回来的消息长度 // 读取所有服务器发送过来的请求参数头部信息 do { line = readLine(is, 0); //如果有Content-Length消息头时取出 if (line.startsWith("Content-Length")) { contentLength = Integer.parseInt(line.split(":")[1].trim()); } //打印请求部信息 System.out.print(line); //如果遇到了一个单独的回车换行,则表示请求头结束 } while (!line.equals("\r\n")); //--输消息的体 System.out.print(readLine(is, contentLength)); //关闭流 is.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /* * 这里我们自己模拟读取一行,因为如果使用API中的BufferedReader时,它是读取到一个回车换行后 * 才返回,否则如果没有读取,则一直阻塞,直接服务器超时自动关闭为止,如果此时还使用BufferedReader * 来读时,因为读到最后一行时,最后一行后不会有回车换行符,所以就会等待。如果使用服务器发送回来的 * 消息头里的Content-Length来截取消息体,这样就不会阻塞 * * contentLe 参数 如果为0时,表示读头,读时我们还是一行一行的返回;如果不为0,表示读消息体, * 时我们根据消息体的长度来读完消息体后,客户端自动关闭流,这样不用先到服务器超时来关闭。 */ private static String readLine(InputStream is, int contentLe) throws IOException { ArrayList lineByteList = new ArrayList(); byte readByte; int total = 0; if (contentLe != 0) { do { readByte = (byte) is.read(); lineByteList.add(Byte.valueOf(readByte)); total++; } while (total < contentLe);//消息体读还未读完 } else { do { readByte = (byte) is.read(); lineByteList.add(Byte.valueOf(readByte)); } while (readByte != 10); } byte[] tmpByteArr = new byte[lineByteList.size()]; for (int i = 0; i < lineByteList.size(); i++) { tmpByteArr[i] = ((Byte) lineByteList.get(i)).byteValue(); } lineByteList.clear(); return new String(tmpByteArr, encoding); } }
6. RestTemplate
RestTemplate is an HTTP request tool provided by Spring. It is much more convenient than traditional Apache and HttpCLient, and can greatly improve the writing efficiency of the client. The code is as follows:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate(ClientHttpRequestFactory factory){ return new RestTemplate(factory); } @Bean public ClientHttpRequestFactory simpleClientHttpRequestFactory(){ SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setConnectTimeout(15000); factory.setReadTimeout(5000); return factory; } } @Autowired RestTemplate restTemplate; @Test public void postTest() throws Exception { MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>(); Map paraMap = new HashMap(); paraMap.put("type", "wx"); paraMap.put("mchid", "10101"); requestEntity.add("consumerAppId", "test"); requestEntity.add("serviceName", "queryMerchant"); requestEntity.add("params", JSON.toJSONString(paraMap)); RestTemplate restTemplate = new RestTemplate(); System.out.println(restTemplate.postForObject("http://10.30.10.151:8012/gateway.do", requestEntity, String.class)); }
The above is the detailed content of What are the ways to send HTTP requests in JAVA?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
