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 8188129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:48:13+00:00 2026-06-07T02:48:13+00:00

I am trying to post data to a server with java to this url:

  • 0

I am trying to post data to a server with java to this url:

https:www.stackoverflow.com

It’s not updating the data.

But when I tried the same with curl it’s updating the data with this url:

E:\curl ssl>curl -k -X POST -u"user:pass" "www.stackoverflow.com" 

Edit:

public void authenticatePostUrl() {

        HostnameVerifier hv = new HostnameVerifier() {

            @Override
            public boolean verify(String urlHostName, SSLSession session) {
                System.out.println("Warning: URL Host: " + urlHostName
                        + " vs. " + session.getPeerHost());
                return true;
            }
        };
        // Now you are telling the JRE to trust any https server.
        // If you know the URL that you are connecting to then this should
        // not be a problem
        try {
            trustAllHttpsCertificates();
        } catch (Exception e) {
            System.out.println("Trustall" + e.getStackTrace());
        }
        HttpsURLConnection.setDefaultHostnameVerifier(hv);
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try {
            URL url = new URL("www.stackoverflow.com");

            String credentials = "user" + ":" + "password";
            String encoding = Base64Converter.encode(credentials.getBytes("UTF-8"));
            HttpsURLConnection  uc = (HttpsURLConnection) url.openConnection();
            uc.setDoInput(true); 
            uc.setDoOutput(true);
            uc.setRequestProperty("Authorization", String.format("Basic %s", encoding));
            uc.setRequestMethod("POST");
            uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
            uc.setRequestProperty("Accept", "application/x-www-form-urlencoded; charset=utf-8");
            uc.getInputStream();
            System.out.println(uc.getContentType());
            InputStream content = (InputStream) uc.getInputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    content));
            String line;
            while ((line = in.readLine()) != null) {
                pw.println(line);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            pw.println("Invalid URL");
        } catch (IOException e) {
            e.printStackTrace();
            pw.println("Error reading URL");
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(sw.toString());
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CurlAuthentication au = new CurlAuthentication();
        au.authenticatePostUrl();
        au.authenticateUrl();
    }

    // Just add these two functions in your program

    public static class TempTrustedManager implements
            javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public boolean isServerTrusted(
                java.security.cert.X509Certificate[] certs) {
            return true;
        }

        public boolean isClientTrusted(
                java.security.cert.X509Certificate[] certs) {
            return true;
        }

        public void checkServerTrusted(
                java.security.cert.X509Certificate[] certs, String authType)
                throws java.security.cert.CertificateException {
            return;
        }

        public void checkClientTrusted(
                java.security.cert.X509Certificate[] certs, String authType)
                throws java.security.cert.CertificateException {
            return;
        }
    }

    private static void trustAllHttpsCertificates() throws Exception {

        // Create a trust manager that does not validate certificate chains:

        javax.net.ssl.TrustManager[] trustAllCerts =

        new javax.net.ssl.TrustManager[1];

        javax.net.ssl.TrustManager tm = new TempTrustedManager();

        trustAllCerts[0] = tm;

        javax.net.ssl.SSLContext sc =

        javax.net.ssl.SSLContext.getInstance("SSL");

        sc.init(null, trustAllCerts, null);

        javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(

        sc.getSocketFactory());

    }

Why is it not working in Java?

(For security reasons I changed the URLs above.)

  • 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-07T02:48:15+00:00Added an answer on June 7, 2026 at 2:48 am

    So you are trying to POST do_not_disturb=no to the server? That’s why I asked you in the comments to your previous question…

    By appending ?do_not_disturb=No to the URL these parameters are automatically send as a GET request to the server, to send them as POST you have to put them in the request body with something like this:

    String postData = "do_not_disturb=No";
    
    OutputStreamWriter outputWriter = new OutputStreamWriter(uc.getOutputStream());
    outputWriter.write(postData);
    outputWriter.flush();
    outputWriter.close();
    

    Then your Accept-Header is probably wrong, as this tells the server in which format you are expecting to get some response data (the content). If you expect to get some XML from the server, this should read uc.setRequestProperty("Accept", "application/xml");.

    UPDATE

    By adding the verbose-flag (-v) to your curl command, i got the header it is sending:

    POST /api/domains/amj.nms.mixnetworks.net/subscribers/9001/ HTTP/1.1
    Authorization: Basic {URL_ENCODED_AUTHENTICATION_STRING}
    User-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
    Host: 8.7.177.4
    Accept: */*
    Content-Length: 17
    Content-Type: application/x-www-form-urlencoded
    

    so please try changing your code like this:

    uc.setRequestProperty("Authorization", String.format("Basic %s", encoding));
    uc.setRequestMethod("POST");
    uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
    uc.setRequestProperty("Accept", "*/*");
    uc.setRequestProperty("Content-Length", Integer.toString(postData.getBytes().length));
    

    The user agent string should be of no interest, unless your server is doing really strange things.

    If it’s still not working, see if your variable encoding has the same value as the part after Basic in a verbose curl run.

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

Sidebar

Related Questions

I'm trying to post data on a server using java HttpUrlConnection class. It seems
I am trying to post data from PHP to Server which is built in
I am trying to setup a simple proxy server that we post the data
Trying to post JSON data to Spring controller.. Not working at all JSP Code:
I am trying to post some data with jQuery Ajax, but the parameters in
I'm trying to send post data between pages with Post. Not a form -
I'm currently trying to send data via POST to a server, and the server
I'm trying to pass post data from one page to the next but PreviousPage
I'm trying to fetch data of some https website from google appengine (java). There
I am trying to POST JSON data to my asp.net MVC server from an

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.