• HttpUtils工具类


    1.http请求工具类

    package com.ins.mesrabbitmq.common.util;

    import org.springframework.data.annotation.Version;
    import org.springframework.data.repository.query.Param;

    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.net.URLConnection;

    public class HttpUtils {



    /**
    * 填设置5秒请求不到,就失败
    * @param requestMethod 请求方式 POST/GET
    * @param urlString 请求地址
    * @param param 参数
    * @param setContentTypeFlag 设置请求头 设置参数类型json格式
    * @return {{@link String}} 返回响应报文
    * @author zhaoJs
    * @date 2022/3/31 10:09
    **/
    public static String doClient(String requestMethod, String urlString, String param, boolean setContentTypeFlag) throws Exception {
    return doClient(requestMethod, urlString, "json", "utf-8", param, 3000, 5000,setContentTypeFlag);
    }

    /**
    *
    *
    * @param requestMethod POST
    * @param urlString 请求的地址
    * @param contentType json/text/html/xml...
    * @param charset utf-8/gbk/gb2312/iso-8859-1...
    * @param param 参数
    * @param timeOut 超时毫秒数
    * @return
    * @throws Exception
    */
    public static String doClient(String requestMethod, String urlString, String contentType, String charset, String param, int timeOut, int readtimeOut,boolean setContentFlag) throws Exception {

    /*
    1.创建HttpURLConnection
    */
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();
    HttpURLConnection conn = (HttpURLConnection)connection;


    /*
    2.设置HttpURLConection参数
    */
    //必须设置下面的这两行 允许写出 读入
    conn.setDoOutput(true);
    conn.setDoInput(true);

    //post方式必须不能有缓存
    conn.setUseCaches(false);

    //设置是post提交方式 POST/GET
    conn.setRequestMethod(requestMethod);

    //设置请求头 设置参数类型是json格式
    if (setContentFlag) {
    String ContentType = "application/" + contentType + ";charset=" + charset;
    conn.setRequestProperty("Content-type", ContentType);
    }

    /**
    * 设置连接超时时间
    * 暂设超时时间3秒
    * @author lib
    */
    conn.setConnectTimeout(timeOut);
    /**
    * 设置读取数据超时(setConnectTimeout和setReadTimeout必须同时使用才会生效)
    * 暂设读取超时5秒
    * @author lib
    */
    conn.setReadTimeout(readtimeOut);

    /*
    3.建立连接
    建立连接之前配置好所有的链接参数
    */
    conn.connect();


    /*
    4.HttpURLConnect 发起请求
    */
    //拿到输出字节流
    OutputStream outputStream = conn.getOutputStream();

    //以字节的方式向服务器发送数据
    outputStream.write(param.getBytes(charset));

    /*
    5.HttpURLConneciton获取响应
    */
    //从服务器拿到输入流用来接收数据,同时这句话又是发送数据的标志,没有这句话是不行的!!!!
    InputStream inputStream = conn.getInputStream();

    InputStreamReader inputStreamReader = new InputStreamReader(inputStream,charset);

    BufferedReader br = new BufferedReader(inputStreamReader);

    String str = null;
    StringBuffer sb = new StringBuffer();

    //从服务器端接受数据
    while((str = br.readLine()) != null){
    sb.append(str);
    }

    outputStream.close();
    inputStreamReader.close();
    inputStream.close();

    conn.disconnect();
    return sb.toString();
    }
    }

    2.0 详解HttpURLConnection

      2.1 请求响应流程

       

      2.2 设置请求头  HttpURLConnection.setRequestProperty的作用

      2.3  设置HttpURLConnection参数:是指 从url.openConnection() 到 url.connect();

    3. 输入流、输出流, 程序本来就有输入输出的功能;A 通过HttpURLConnection程序 和B 建立连接,此时程序是可以读取到A的数据的,要通过程序把参数 输出B ;然后再读取B的响应值 ;展示在程序中。

     4.流

      4.1 InputStreamReader和InputStream的区别:

        InputStreamReader用于读取字符,而InputStream用于读取字节;

        从输入流中读取数据的一个字符

     
     

     
  • 相关阅读:
    [日料探店] 食一料理
    工地英语
    C++20协程解糖
    C++20协程解糖
    C++20协程解糖
    单表操作
    数据库查询语句罗列
    数据库简易介绍
    mysql的常用操作
    css基础
  • 原文地址:https://www.cnblogs.com/nextgg/p/16080327.html
Copyright © 2020-2023  润新知