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

  • Home
  • SEARCH
  • 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 7726119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:08:05+00:00 2026-06-01T05:08:05+00:00

I have come up with the following code to call a webservice using client/server

  • 0

I have come up with the following code to call a webservice using client/server certificate authentication, but the server is never getting the client certificate, what is going wrong here?

package com.cs;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.util.Collection;
import java.util.Iterator;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;

import net.my.myHostnameVerifier;
import net.my.myX509TrustManager;
import android.app.Activity;
import android.os.Bundle;

public class ClientServerActivity extends Activity {
    protected static int debug = 1;
    String soapRequest="";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        soapRequest = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:nam=\"https://ind-etsdev/namespace\">"
                + "<soapenv:Header/>"
                + "<soapenv:Body>"
                + "<nam:CreateSession>"
                + "<DeviceID>something</DeviceID>"
                + "<UserName>something</UserName>"
                + "<UserPassword>something</UserPassword>"
                + "</nam:CreateSession>"
                + "</soapenv:Body>"
                + "</soapenv:Envelope>";
        runService();
    }
    private void runService(){
        try {
            debug = 1;
            String certPassword = "my1234";
            initializeURLConn(certPassword, "BKS", null);
            String urlStr = "https://webserviceurl";
            int requestStatus = -1;
            requestStatus = doHttpRequest(urlStr);
            System.out.println("Request Status: " + requestStatus);
        }
        catch(Throwable x) {
            x.printStackTrace();
        }
    }
    private int doHttpRequest(String urlStr) throws Exception {
        String responseBody = "";

        //Open the HTTP connection
        URL url = new URL(urlStr);
        HttpsURLConnection urlconnection = (HttpsURLConnection)url.openConnection();
        urlconnection.setHostnameVerifier(new myHostnameVerifier(true, debug));
        urlconnection.setDoOutput(true);
        urlconnection.setDoInput(true);
        urlconnection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8"); 

        urlconnection.setRequestProperty("Content-Length", Integer.toString(soapRequest.length())); 
        urlconnection.setRequestProperty("SOAPAction", "https://soapaction"); 

        urlconnection.setUseCaches(false);
        urlconnection.setConnectTimeout(30000); //30 seconds
        urlconnection.setReadTimeout(180000); //3 minutes
        urlconnection.connect();

        // Send Request to the server 
        try {
            DataOutputStream outStr = new DataOutputStream(urlconnection.getOutputStream ());   
            outStr.writeBytes(soapRequest); 
            outStr.flush(); 
            outStr.close();
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } 

        //Read the response
        int responseCode = urlconnection.getResponseCode();

        InputStreamReader responseISR = null;
        BufferedReader responseBR = null;

        try {
            responseISR = new InputStreamReader(urlconnection.getInputStream());
        }
        catch (IOException e) {
            System.out.println("IOException!!!!");
            e.printStackTrace();
            responseISR = new InputStreamReader(urlconnection.getErrorStream());
        }

        responseBR = new BufferedReader(responseISR);
        StringBuffer stringbuffer = new StringBuffer();
        String responseLine;
        while ((responseLine = responseBR.readLine()) != null) {
            stringbuffer.append(responseLine + "\n");
        }
        responseBR.close();
        urlconnection.disconnect();

        responseBody = stringbuffer.toString();

        System.out.println(responseBody);

        return responseCode;
    }


    private void initializeURLConn(String keystorePassword, String keystoreType, String trustFileName) throws Exception {
        // Create keystore
        KeyStore keystore = KeyStore.getInstance(keystoreType);
        InputStream keystoreFIS = getResources().openRawResource(R.raw.clientvik);
        keystore.load(keystoreFIS, keystorePassword != null ? keystorePassword.toCharArray(): null);

        // Create key manager
        KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmfactory.init(keystore, keystorePassword != null ? keystorePassword.toCharArray(): null);
        KeyManager[] keymanagers = kmfactory.getKeyManagers();

        TrustManager[] trustmanagers = null;
        if (trustFileName == null) {
            // Create a trust manager that does not validate certificate chains
            trustmanagers = new TrustManager[]{new myX509TrustManager()};
        }
        else {
            // Create trust keystore
            KeyStore ksCA = java.security.KeyStore.getInstance("JKS");
            ksCA.load(null, null);

            // Add MRTU cert to the keystore as a trusted certificate
            FileInputStream truststoreFIS = new FileInputStream(trustFileName);
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            Collection c = cf.generateCertificates(truststoreFIS);
            Iterator i = c.iterator();
            while (i.hasNext()) {
                Certificate cert = (Certificate)i.next();
                //System.out.println(cert);
                ksCA.setCertificateEntry("trustedCA", cert);
            }

            // Create trust manager
            TrustManagerFactory tmf = TrustManagerFactory.getInstance("X.509");
            tmf.init(ksCA);
            trustmanagers = (TrustManager[])tmf.getTrustManagers();
        }

        // Install the key manager and trust manager
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(keymanagers, trustmanagers, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    }

}
  • 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-01T05:08:07+00:00Added an answer on June 1, 2026 at 5:08 am

    i found out what the problem was. i didn’t had the certificate with full certificate chain in my client certificate. i created the bks keystore with full certificate chain and it worked.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have come across the following jQuery code but could not understand it. What
I have come across the following type of code many a times, and I
I have come across the following link: http://code.google.com/p/a-star/source/browse/trunk/java/PathFinder.java?r=8 I have got the code working
I need to do the following .. have come across various examples but i
While learning signal() system call, I supposed to come across the following code, #include
I am writing an API and have come across the following pattern: My API
I am trying to configure TFS 2010 reporting and have come accross the following
When writing GUIs, I've frequently come over the following problem: Assume you have a
With the help from good samaritans from stackoverflow, I have come till the following
I've used AsyncTask quite a bit - but I have come across a seemingly

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.