Solved

Code for Uploading a file over https

Posted on 2006-10-31
13
237 Views
Last Modified: 2012-08-14
Hey experts,

   I have currently a java closed source code that can be used to upload a file over HTTP; however, I need also the capability to upload over HTTPS. Any help on providing such available code, or on how to modify my existing code to make it support https ?(I can provide any needed sections of it to the experts).

0
Comment
Question by:mte01
  • 5
  • 5
  • 3
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17848108
0
 
LVL 3

Author Comment

by:mte01
ID: 17848146
>>CEHJ

Yes, using the classes of this package, I was able to post a file over an http server, but my problem now is that it doesn't work directly over https (which is normal); some capabilities need to be added, and I am unable to find them..any help??
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17848174
0
 
LVL 3

Author Comment

by:mte01
ID: 17848179
>>CEHJ

Yes, it gives the following error:

An IOException occurred attempting to POST the file: test.txt
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certificate found
      at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.a(DashoA12275)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(DashoA12275)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(DashoA12275)
      at com.sun.net.ssl.internal.ssl.SunJSSE_az.a(DashoA12275)
      at com.sun.net.ssl.internal.ssl.SunJSSE_az.a(DashoA12275)
      at com.sun.net.ssl.internal.ssl.SunJSSE_ax.a(DashoA12275)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(DashoA12275)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.j(DashoA12275)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(DashoA12275)
      at com.sun.net.ssl.internal.ssl.AppOutputStream.write(DashoA12275)
      at org.apache.commons.httpclient.HttpConnection$WrappedOutputStream.write(HttpConnection.java:1360)
      at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:66)
      at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:124)
      at org.apache.commons.httpclient.HttpConnection.flushRequestOutputStream(HttpConnection.java:790)
      at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2271)
      at org.apache.commons.httpclient.HttpMethodBase.processRequest(HttpMethodBase.java:2651)
      at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1087)
      at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:643)
      at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:497)
      at com.tangoe.cmp.tools.HttpFilePost.simplePost(HttpFilePost.java:510)
      at com.tangoe.cmp.tools.HttpFilePost.performPost(HttpFilePost.java:421)
      at com.tangoe.cmp.tools.HttpFilePost.main(HttpFilePost.java:1000)
Caused by: sun.security.validator.ValidatorException: No trusted certificate found
      at sun.security.validator.SimpleValidator.buildTrustedChain(SimpleValidator.java:304)
      at sun.security.validator.SimpleValidator.engineValidate(SimpleValidator.java:107)
      at sun.security.validator.Validator.validate(Validator.java:202)
      at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(DashoA12275)
      at com.sun.net.ssl.internal.ssl.JsseX509TrustManager.checkServerTrusted(DashoA12275)
      ... 19 more

with regular http, the file uploads normally
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 285 total points
ID: 17848193
Perhaps try it in conjunction with the following


http://javaalmanac.com/egs/javax.net.ssl/TrustAll.html
0
 
LVL 92

Expert Comment

by:objects
ID: 17848210
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 92

Expert Comment

by:objects
ID: 17848216
>  I have currently a java closed source code that can be used to upload a file over HTTP

depending on the interface it provides you can most likely use it for https.
0
 
LVL 3

Author Comment

by:mte01
ID: 17848217
>>CEHJ

This TrustManager has to be at the server servelt...right?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17848228
>>This TrustManager has to be at the server servelt...right?

It should be at the box from which you're trying to upload
0
 
LVL 92

Assisted Solution

by:objects
objects earned 215 total points
ID: 17848232
details on using ssl with httpclient if you decide to switch libraries

http://jakarta.apache.org/commons/httpclient/sslguide.html
0
 
LVL 3

Author Comment

by:mte01
ID: 17848252
By the way...This is the method in which I am uploading the file:

  private int simplePost(File aFile) throws IOException
  {
    BufferedInputStream bufStream = null;
    int statusCode1               = -1;
    try
    {
      HttpClient client = new HttpClient();

      PostMethod postMethod;
      if (bKeepFilenames == false)
      {
        postMethod = new PostMethod(strUrl);
      }
      else
      {
        postMethod = new PostMethod(fullPostUrl);
      }

      if (authUrl != null)
      {
        logger.info("(authPost) Authentication to url" + authUrl);
        PostMethod authPost = new PostMethod(authUrl);
        StringBuffer strBuf = new StringBuffer(100);
        strBuf.append(contentType).append("; charset=").append(charSet);

        // set the content type and try to automatically follow redirects
        authPost.setRequestHeader("Content-type", strBuf.toString());
        authPost.setFollowRedirects(true);
        int authStatusCode = client.executeMethod(authPost);

        //put the response body in the variable so clients can retrieve it later on
        responseBody = postMethod.getResponseBodyAsString();
        if (verbose)
        {
          StatusLine statLine = authPost.getStatusLine();
          logger.info("(authPost) Status Line: " + statLine);
        }

        if ((authStatusCode < 200) || (authStatusCode > 399))
        {
          logger.info("(authPost) Authentication failed");
          return authStatusCode;
        }
      }

      client.setConnectionTimeout(8000);

      // Send any file as the body of the POST request
      if (verbose)
      {
        logger.info("(simplePost) File length: " + aFile.length());
      }

      bufStream = new BufferedInputStream(new FileInputStream(aFile));

      postMethod.setRequestBody(bufStream);
      postMethod.setRequestContentLength((int)aFile.length());

      StringBuffer strBuf = new StringBuffer(100);
      strBuf.append(contentType).append("; charset=").append(charSet);

      // set the content type and try to automatically follow redirects
      postMethod.setRequestHeader("Content-type", strBuf.toString());

      // add any custom headers
      addCustomRequestHeaders(postMethod);

      postMethod.setFollowRedirects(true);
      statusCode1 = client.executeMethod(postMethod);
      postMethod.releaseConnection();

      StatusLine statLine = postMethod.getStatusLine();
      if (verbose)
      {
        logger.info("(simplePost) Status Line: " + statLine);
      }

      logger.info(buildSummaryLine(statLine, aFile));
    }
    catch (IOException ioe)
    {
      if (bufStream != null)
      {
        bufStream.close();
      }

      throw ioe;
    }

    bufStream.close();
    return statusCode1;
  }
0
 
LVL 3

Author Comment

by:mte01
ID: 17959177
It turned out that the problem is that the cacerts file in which the certificate is added is inside the weblogic folders (since I am using WebLogic for my servlet); when I copied that file to the jdk cacerts folder of the one I am running the client application in, it worked...thanks for you help guys!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17959825
:-)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now