Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8525225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:54:55+00:00 2026-06-11T07:54:55+00:00

Here is my code to making a post request to servlet on an http

  • 0

Here is my code to making a post request to servlet on an http server

private static void post(String endpoint, Map<String, String> params)
        throws IOException {
    URL url;
    try {
        url = new URL(endpoint);
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("invalid url: " + endpoint);
    }
    StringBuilder bodyBuilder = new StringBuilder();
    Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
    // constructs the POST body using the parameters
    while (iterator.hasNext()) {
        Entry<String, String> param = iterator.next();
        bodyBuilder.append(param.getKey()).append('=')
                .append(param.getValue());
        if (iterator.hasNext()) {
            bodyBuilder.append('&');
        }
    }
    String body = bodyBuilder.toString();
    Log.v(TAG, "Posting '" + body + "' to " + url);
    byte[] bytes = body.getBytes();
    HttpURLConnection conn = null;


    try {
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setFixedLengthStreamingMode(bytes.length);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded;charset=UTF-8");
        // post the request
        OutputStream out = conn.getOutputStream();
        out.write(bytes);
        out.close();
        // handle the response
        int status = conn.getResponseCode();
        if (status != 200) {
          throw new IOException("Post failed with error code " + status);
        }
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
  }

When my end point is like an http://myipaddress:myport/ it is working well but when i have changed it in to an https connection its not working i have read about HttpsURLConnection too but i am not getting how to implement it there they have written this this code :

KeyStore keyStore = ...;
   TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
   tmf.init(keyStore);

   SSLContext context = SSLContext.getInstance("TLS");
   context.init(null, tmf.getTrustManagers(), null);

   URL url = new URL("https://www.example.com/");
   HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
   urlConnection.setSSLSocketFactory(context.getSocketFactory());
   InputStream in = urlConnection.getInputStream();

But I am not able to get what should be in Keystore i have the file file only and if I am using Keystore.getInstance(String type) how to get the type of certificate.

Please Help me out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:54:57+00:00Added an answer on June 11, 2026 at 7:54 am

    please check this for making post request to secure server

    // request

        String Verify_Mobile_URL ="https://www.sample.php";
                            try 
                            {
    
                                StringBuilder postDataBuilder = new StringBuilder();
                                postDataBuilder.append("param1").append("=").append("paramvalue");
                                postDataBuilder.append("&").append("param2").append("=").append("paramvalue");
    
    
                                byte[] postData = postDataBuilder.toString().getBytes();
    
                                // Hit the dm URL.
    
                                URL url = new URL(Verify_Mobile_URL);
                                HttpsURLConnection.setDefaultHostnameVerifier(new AllVerifier());
                                SSLContext sslContext = SSLContext.getInstance("TLS");
                                sslContext.init(null, new TrustManager[] { new AllTrustManager() }, null);
                                HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
                                HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();        
                                conn.setReadTimeout(60000);
                                conn.setConnectTimeout(35000);
                                conn.setDoOutput(true);
                                conn.setUseCaches(false);
                                conn.setRequestMethod("POST");
                                conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                                conn.setRequestProperty("Content-Length",Integer.toString(postData.length));
    
                                OutputStream out = conn.getOutputStream();
                                out.write(postData);
                                out.close();
    
                                int responseCode = conn.getResponseCode();
                                if(responseCode==200)
                                {
                                    InputStream inputstream=conn.getInputStream();  
                                    String result=streamToString(inputstream);   // here you will will get result from
    
                                }
                                catch(Exception e)
                                {
                                }
    
    
    
    
    
    
    
    /**
         * This method convert inputstream to string
         * @param is - inputtream to be converted
         * @return String - converted string 
         */
        public static String streamToString(InputStream is)
        {
            DataInputStream din = new DataInputStream(is);
            StringBuffer sb = new StringBuffer();
            try {
                String line = null;
                while ((line = din.readLine()) != null) 
                {
                    sb.append(line + "\n");
                }
    
            } 
            catch (Exception ex) 
            {}      
    
            finally 
            {
                try 
                {  if(is!=null)
                    {
                        din.close();
                        is.close();
                    }
                } 
                catch (Exception ex) 
                {}
    
            }
            return sb.toString();
    
        }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    public class AllTrustManager implements X509TrustManager {
    
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            // TODO Auto-generated method stub
            return new X509Certificate[0];
        }
    
    }
    
    
    
    
    
    public class AllVerifier implements HostnameVerifier {
    
        @Override
        public boolean verify(String hostname, SSLSession session) {
            // TODO Auto-generated method stub
            return true;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a pure css dropdown menu (code here: http://jsfiddle.net/SeXyv/7/ ) and I would
I am making a GUI of Stack using Java. Here is my code private
Started making a game. Here's some of my code. package games.tribe.screens; import games.tribe.model.World; import
here is code for regular expression matching #include<iostream> #include<stdio.h> #include<string.h> using namespace std; int
I am making a jquery post request to obtain a part of the html
I want to invoke an ASP.NET web service via an http POST request using
I'm making a cross domain POST request. I added Access-Control-* headers to the web
I log in users from a Http page via ajax. I'm making the request
I'm making a HTTP Post and I would like to know how to convert
First post and first iPhone app in the making here, so please excuse the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.