java send accept request through http

When the project is developed, we use webservice to call other people's data, and then transmit it through netty. However, when netty is transmitting, there will be a reason why the data is too large to be transmitted. At this time, someone suggested to me to use http to accept data.

The idea is that when using netty transmission, it cannot be transmitted, so use http transmission instead. At this time, the data is already obtained and sent. The other end accepts it.

code above.

send data

public static String loginOfPost(String str) {
        HttpURLConnection conn = null;
        try {
            // create a URL object
            URL mURL = new URL("http://localhost:12001");
            // Call the openConnection() method of the URL to get the HttpURLConnection object
            conn = (HttpURLConnection) mURL.openConnection();

            conn.setRequestMethod("POST");// Set the request method to post
            conn.setReadTimeout(10000);// Set the read timeout to 5 seconds
            conn.setConnectTimeout(10000);// Set the network connection timeout to 10 seconds
            conn.setDoOutput(true);// Set this method to allow output to the server

            // Get an output stream and write data to the server. By default, the system does not allow output to the server
            OutputStream out = conn.getOutputStream();// Get an output stream and write data to the server
            out.write(str.getBytes());
            out.flush();
            out.close();
            int responseCode = conn.getResponseCode();// Calling this method eliminates the need to use the conn.connect() method
            if (responseCode == 200) {
                InputStream is = conn.getInputStream();
                String state = getStringFromInputStream(is);
                return state;
            } else {
// Log.i(TAG, "Access failed" + responseCode);
                System.out.print("Access failed");
            }
        } catch (Exception e) {
            e.printStackTrace ();
        } finally {
            if (conn != null) {
                conn.disconnect();//Close the connection
            }
        }
        return null;
    }

accept data

/**
     * Accept the request sent by the client
     * @param map
     */
    private String serverAcceptRequest(Map map, String json,int port,String methodName) {
        StringBuffer sb = new StringBuffer();
        try {
            ServerSocket serverSocket = new ServerSocket(port);
            while(true) {
                json = JSONArray.toJSONString(map);
                cwpService.sendMsgToIpa(UUID.randomUUID().toString(), methodName, json);
                Socket clientSocket = serverSocket.accept();
                BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                String temp;
                while((temp=in.readLine()) != null)
                    sb.append(temp);
                in.close();
                break;
            }
        } catch(Exception e) {
            System.out.println("ERROR: " + e.getMessage());
            System.exit(1);
        }
        return sb.toString();
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324918432&siteId=291194637