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

4
  • Which error do you have? Commented Jan 18, 2018 at 9:23
  • You have to use httpPost to send json Commented Jan 18, 2018 at 9:23
  • org.apache.http.entity.StringEntity cannot be cast to org.apache.http.params.HttpParams I can't use httpPost Commented Jan 18, 2018 at 9:25
  • stackoverflow.com/questions/7181534/… Commented Jan 18, 2018 at 9:31

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

ok.. but.. i can use get to do authorization and post to send json?
U can use it as a header parameter , which can be used for both post and get request

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.