From 4c448f389881333804f0da6b01c86aa860563d82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=AE=87=E8=88=AA?= <19856072110@163.com> Date: Tue, 17 Dec 2024 15:28:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/wsk/tool/HttpUtils.java | 93 +++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/main/java/com/wsk/tool/HttpUtils.java diff --git a/src/main/java/com/wsk/tool/HttpUtils.java b/src/main/java/com/wsk/tool/HttpUtils.java new file mode 100644 index 0000000..18c3ab8 --- /dev/null +++ b/src/main/java/com/wsk/tool/HttpUtils.java @@ -0,0 +1,93 @@ +package com.wsk.tool; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; +import java.util.Map; + +/** + * Created by wsk1103 on 2017/1/2. + */ +public class HttpUtils { + /* + * Function : 发送Post请求到服务器 + * Param : params请求体内容,encode编码格式 + */ + static String submitPostData(String strUrlPath, Map params, String encode) { + + byte[] data = getRequestData(params, encode).toString().getBytes();//获得请求体 + try { + + //String urlPath = "http://192.168.1.9:80/JJKSms/RecSms.php"; + URL url = new URL(strUrlPath); + + HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); + httpURLConnection.setConnectTimeout(3000); //设置连接超时时间 + httpURLConnection.setDoInput(true); //打开输入流,以便从服务器获取数据 + httpURLConnection.setDoOutput(true); //打开输出流,以便向服务器提交数据 + httpURLConnection.setRequestMethod("POST"); //设置以Post方式提交数据 + httpURLConnection.setUseCaches(false); //使用Post方式不能使用缓存 + //设置请求体的类型是文本类型 + httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + //设置请求体的长度 + httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length)); + //获得输出流,向服务器写入数据 + OutputStream outputStream = httpURLConnection.getOutputStream(); + outputStream.write(data); + + int response = httpURLConnection.getResponseCode(); //获得服务器的响应码 + if(response == HttpURLConnection.HTTP_OK) { + InputStream inptStream = httpURLConnection.getInputStream(); + return dealResponseResult(inptStream); //处理服务器的响应结果 + } + } catch (IOException e) { + //e.printStackTrace(); + return "err: " + e.getMessage().toString(); + } + return "-1"; + } + + /* + * Function : 封装请求体信息 + * Param : params请求体内容,encode编码格式 + */ + private static StringBuffer getRequestData(Map params, String encode) { + StringBuffer stringBuffer = new StringBuffer(); //存储封装好的请求体信息 + try { + for(Map.Entry entry : params.entrySet()) { + stringBuffer.append(entry.getKey()) + .append("=") + .append(URLEncoder.encode(entry.getValue(), encode)) + .append("&"); + } + stringBuffer.deleteCharAt(stringBuffer.length() - 1); //删除最后的一个"&" + } catch (Exception e) { + e.printStackTrace(); + } + return stringBuffer; + } + + /* + * Function : 处理服务器的响应结果(将输入流转化成字符串) + * Param : inputStream服务器的响应输入流 + */ + private static String dealResponseResult(InputStream inputStream) { + String resultData = null; //存储处理结果 + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + byte[] data = new byte[1024]; + int len = 0; + try { + while((len = inputStream.read(data)) != -1) { + byteArrayOutputStream.write(data, 0, len); + } + } catch (IOException e) { + e.printStackTrace(); + } + resultData = new String(byteArrayOutputStream.toByteArray()); + return resultData; + } +}