0

HY, i would send json to gateway

i can do Login gateway with this code :

private static final String SPN_IP = "192.168.0.2";
        private static final String SPN_LOGIN = "myadmin";
        private static final String SPN_PASSWORD = "mypasswor";
        private static String jsonSicuro = "[\"txmsgid\":,\"trycount\": 5,\"port\": 2,\"payload\": \"01\",\"ack\": true,\"mote\": \"17A7F79\"  }]";


        public static void main(String[] args) throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException, AuthenticationException, URISyntaxException {
            // SSL management
            SSLContextBuilder builder = new SSLContextBuilder();
            builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); // trust self-signed cert
            SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(builder.build(), new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession sslSession) {
                    return SPN_IP.equals(hostname); // validate the host name
                }
            });

            // Create client
            CloseableHttpClient httpclient = HttpClientBuilder.create()
                    .setSSLSocketFactory(sslSocketFactory)
                    .build();

            // Create HTTP GET request
            HttpGet httpGet = new HttpGet("https://" + SPN_IP);

            // HTTP digest
            DigestScheme digestAuth = new DigestScheme();
            digestAuth.overrideParamter("algorithm", "MD5");
            digestAuth.overrideParamter("realm", "https://" + SPN_IP);
            digestAuth.overrideParamter("nonce", Long.toString(new Random().nextLong(), 36));
            digestAuth.overrideParamter("qop", "auth");
            digestAuth.overrideParamter("nc", "0");
            digestAuth.overrideParamter("cnonce", DigestScheme.createCnonce());

            // Send HTTP digest header
            Header auth = (Header) digestAuth.authenticate(new UsernamePasswordCredentials(SPN_LOGIN, SPN_PASSWORD), httpGet, null);
            System.out.println(((org.apache.http.Header) auth).getName());
            System.out.println(((org.apache.http.Header) auth).getValue());
            httpGet.setHeader((org.apache.http.Header) auth);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            System.out.println("Executing request: " + httpGet.getRequestLine());
            String response = httpclient.execute(httpGet, responseHandler);
            System.out.println("Response: " + response);

Gateway response with "Authorization ok "..................... now, how can I send json? I try " httpGet.setParams((HttpParams) ((HttpEntity) new StringEntity(jsonSicuro)));" but i have error

1 Answers1

0

We cant send json to httpGet , You can only send request body to httpPost. Please check What is the difference between POST and GET?

Also check the below link to pass params commons httpclient - Adding query string parameters to GET/POST request

VinuBibin
  • 549
  • 5
  • 17