current position:Home>Come on, today I'll teach you how - Java - initiates - HTTP - requests, Kafka tutorial Java

Come on, today I'll teach you how - Java - initiates - HTTP - requests, Kafka tutorial Java

2021-08-21 13:28:54 Code Silva

  • POST The request has no length limit

Implementation code

Next Java send out GET/POST The request is encapsulated as HttpRequest class , You can use it directly .HttpRequest The class code is as follows :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
public class HttpRequest {
    /**
     *  Assign to URL send out GET Method request 
     *
     * @param url
     *             Send requested URL
     * @param param
     *             Request parameters , Request parameter should be  name1=value1&name2=value2  In the form of .
     * @return URL  Response result of the remote resource represented 
     */
    public static String sendGet(String url, String param) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            //  Open and URL Connection between 
            URLConnection connection = realUrl.openConnection();
            //  Set common request properties 
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            //  Establish the actual connection 
            connection.connect();
            //  Get all response header fields 
            Map<String, List<String>> map = connection.getHeaderFields();
            //  Traverse all response header fields 
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }
            //  Definition  BufferedReader Input stream to read URL Response 
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println(" send out GET Exception in request !" + e);
            e.printStackTrace();
        }
        //  Use finally Block to close the input stream 
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
    /**
     *  Assign to  URL  send out POST Method request 
     *
     * @param url
     *             Send requested  URL
     * @param param
     *             Request parameters , Request parameter should be  name1=value1&name2=value2  In the form of .
     * @return  Response result of the remote resource represented 
     */
    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            //  Open and URL Connection between 
            URLConnection conn = realUrl.openConnection();
            //  Set common request properties 
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            //  send out POST The request must be set to the following two lines 
            conn.setDoOutput(true);
            conn.setDoInput(true);
            //  obtain URLConnection Object corresponding output stream 
            out = new PrintWriter(conn.getOutputStream());
            //  Send request parameters 
            out.print(param);
            // flush Buffering of output streams 
            out.flush();
            //  Definition BufferedReader Input stream to read URL Response 
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println(" send out  POST  Exception in request !"+e);
            e.printStackTrace();
        }
        // Use finally Block to close the output stream 、 Input stream 
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.

Examples demonstrate

#app The routing address of "/show" That is to say ajax As defined in url Address , use POST、GET Methods can be submitted 
@app.route("/show",methods=["GET", "POST"])
def show():
    # First, get the front-end input name data 
    if request.method == "POST":
        name = request.form.get("name")
    if request.method == "GET":
        name = request.args.get("name")
    # establish Database Class object sql,test Name the database you want to access   concrete Database Class constructor 
    sql = Database("test")
    try:
        # perform sql sentence   Many say ,f+ String form , You can use... In a string {} Add the variable name in the form of   The results are stored in result Array 
        result = sql.execute(f"SELECT type FROM type WHERE name='{name}'")
    except Exception as e:


# Kafka Practical notes 

> ** About this note , In order not to affect everyone's reading experience , I can only show some chapters and core screenshots in the article , If you need complete pdf edition ,[ You can get it free by poking here ](https://gitee.com/vip204888/java-p7).**

![image.png](https://s2.51cto.com/images/20210820/1629409803125651.jpg)


*   **Kafka introduction **
*   ** Why choose Kafka**
*   **Karka Installation 、 Management and configuration **

![image.png](https://s2.51cto.com/images/20210820/1629409803769674.jpg)


*   **Kafka The cluster of **
*   ** first Kafka Program **
*   ![image.png](https://s2.51cto.com/images/20210820/1629409804303681.jpg)


afka The producers of 

![image.png](https://s2.51cto.com/images/20210820/1629409805878990.jpg)

*   **Kafka The consumer **
*   ** In depth understanding of Kafka**
*   ** Reliable data transmission **

![image.png](https://s2.51cto.com/images/20210820/1629409805930576.jpg)


![image.png](https://s2.51cto.com/images/20210820/1629409806277963.jpg)


*   **Spring and Kalka Integration of **
*   **Sprinboot and Kafka Integration of **
*   **Kafka The practice of cutting peaks and filling valleys **
*   ** Data pipeline and streaming ( Understanding can )**

![image.png](https://s2.51cto.com/images/20210820/1629409807830192.jpg)


*   **Kafka The practice of cutting peaks and filling valleys **

![image.png](https://s2.51cto.com/images/20210820/1629409808250113.jpg)


     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.

copyright notice
author[Code Silva],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2021/08/20210821132852307Y.html

Random recommended