HTTPCLIENT, CloseableHttpClient request interface

tags: Java  http  restful  

HttpClient

HTTPCLIENT GET method request interface

    public JSONObject getProducts(String token) {
        JSONObject jsonObject = null;
        // Create an HTTPCLIENT object
        HttpClient objClient = new HttpClient();
		
		/ / Set the delivery encoding format to prevent garbled errors
        objClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
		
		// Create a getMethod object, parameter is the address requested
        GetMethod method = new GetMethod("https://XXXXX.com/");
        
        // Set the request head
        method.setRequestHeader("token", token);

        try {
        	/ / Execute method request interface
            objClient.executeMethod(method);
            // Print the status of the server returns
            System.out.println(method.getStatusLine());
            // Get return value information
            String strResponseBody = new String(method.getResponseBody(),"UTF-8");
            System.out.println(strResponseBody);
            jsonObject = JSONObject.parseObject(strResponseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Release connection
        method.releaseConnection();
        return jsonObject;
    }

HTTPCLIENT POST request interface

    public JSONObject getProducts(String token) {
        JSONObject jsonObject = null;
        // Create an HTTPCLIENT object
        HttpClient objClient = new HttpClient();
		
		/ / Set the delivery encoding format to prevent garbled errors
        objClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
		
		// Create a PostMethod object, the parameter is the address requested
        PostMethod method = new PostMethod("https://XXXXX.com/");
        
        // Set the request head
        method.setRequestHeader("token", token);

        try {
        	/ / Execute method request interface
            objClient.executeMethod(method);
            // Print the status of the server returns
            System.out.println(method.getStatusLine());
            // Get return value information
            String strResponseBody = new String(method.getResponseBody(),"UTF-8");
            System.out.println(strResponseBody);
            jsonObject = JSONObject.parseObject(strResponseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Release connection
        method.releaseConnection();
        return jsonObject;
    }

CloseableHttpClient

CloseablehttpClient Get request interface

    public JSONObject getProducts(String token) {
        JSONObject jsonObject = null;
        // Get the CloseAblehttpClient object by httpclients.createdefault ()
        CloseableHttpClient httpclient = HttpClients.createDefault();
		
		// Create an HTTPGET object, the parameter is the requested address
      	HttpGet method = new HttpGet("https://XXXXX.com/");
        
        // Set the request head
       method.setHeader("token", token);

        try {
        	// Execute method request interface get a response
        	HttpResponse response = httpclient.execute(method);
            // Analytical response data
            String content = EntityUtils.toString(response.getEntity(), "UTF-8");
            / / Go to JSON format
            jsonObject = JSON.parseObject(content);
            System.out.println(jsonObject.toString());
            EntityUtils.consume(response.getEntity());// completely consume
        } finally {
            try {
                // Release connection
                method.releaseConnection();
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return jsonObject;
    }

CloseablehttpClient POST request interface

    public JSONObject saveOrder(String token) {
        JSONObject jsonObject = null;
        // Get the CloseAblehttpClient object by httpclients.createdefault ()
        CloseableHttpClient httpclient = HttpClients.createDefault();
        
        / / Set the delivery encoding format to prevent garbled errors
        httpclient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
		
		// Create an HTTPPOST object, the parameter is the address requested
        HttpPost method = new HttpPost("https://XXXXX.com/");
        // Set the request head
        method.setHeader("token", token);

        JSONObject requestJson = new JSONObject();
        //date
        requestJson.put("times", new Date());
        // single number, unique
        requestJson.put("orderCode", "123");
        // long (cm)
        requestJson.put("longs", "1");
        // Width (cm)
        requestJson.put("width", "1");
        // High (cm)
        requestJson.put("high", "1");
        // to Stringentity 
        StringEntity entity = new StringEntity(requestJson.toString(), Charset.forName("UTF-8"));
        // Set the encoding
        entity.setContentEncoding("UTF-8");
        // Send data requests in JSON format
        entity.setContentType("application/json");
        //Setting parameters
        method.setEntity(entity);

        try {
        	// Execute method request interface get a response
            HttpResponse response = httpclient.execute(method);
            // Get return value information
            String content = EntityUtils.toString(response.getEntity(), "UTF-8");
            // Go to JSON
            jsonObject = JSON.parseObject(content);
            System.out.println(jsonObject.toString());
            EntityUtils.consume(response.getEntity());// completely consume
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // Release connection
                method.releaseConnection();
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return jsonObject;
    }

Intelligent Recommendation

Two ways to send http requests in java: HTTPClient and CloseableHttpClient and understanding of some annotations of swagger interface

Two ways to send http requests in java: HTTPClient and CloseableHttpClient and understanding of some annotations of swagger interface There are three ways to send http requests in java. In addition to...

httpClient to request the interface (in the form of java)

Not in the code to appear mysql join statement (efficient is too slow) Automatically generate mapper.xml file without using mybatis-generator Do not use frames like tkmapper and pageHelper Get the out...

Common httpclient request interface demo

Initialize port Client that initializes the request Add agent Send an interface with pictures Get request, the parameter is List Post request, the parameter is List Post interface parameter is Map...

Angular HttpClient request interface data

1. Introduce the module in app.module.ts 2. Introduce HttpClient in the place of use 3. Can be called on the page through the service  ...

More Recommendation

httpclient request interface timeout problem

Recently, there was a problem online. After the external request came, there was no response to the caller. There was no error reported in the log, and it can be reproduced. may think of five reasons:...

HTTPCLIENT ways request external interface

HTTPCLIENT ways request external interface POST request, Map POST request, JSONSTRING Get request method  ...

Httpclient request data from the interface

Project scene: Request data from the public interface Problem Description Httpclient method Convenient for yourself CTR +C and CTR +V Welcome to correct me wrong, I did not find an error in the proces...

HttpClient uses CloseableHttpClient to send post requests

General steps for sending requests using HttpClient (1) Create an HttpClient object. (2) Create an instance of the request method and specify the request URL. If you need to send a GET request, create...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top