mte01
asked on
Code for Uploading a file over https
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).
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).
ASKER
>>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??
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??
Did you try the code using example at
http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/methods/multipart/MultipartRequestEntity.html
with an https url?
http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/methods/multipart/MultipartRequestEntity.html
with an https url?
ASKER
>>CEHJ
Yes, it gives the following error:
An IOException occurred attempting to POST the file: test.txt
javax.net.ssl.SSLHandshake Exception: sun.security.validator.Val idatorExce ption: No trusted certificate found
at com.sun.net.ssl.internal.s sl.BaseSSL SocketImpl .a(DashoA1 2275)
at com.sun.net.ssl.internal.s sl.SSLSock etImpl.a(D ashoA12275 )
at com.sun.net.ssl.internal.s sl.SSLSock etImpl.a(D ashoA12275 )
at com.sun.net.ssl.internal.s sl.SunJSSE _az.a(Dash oA12275)
at com.sun.net.ssl.internal.s sl.SunJSSE _az.a(Dash oA12275)
at com.sun.net.ssl.internal.s sl.SunJSSE _ax.a(Dash oA12275)
at com.sun.net.ssl.internal.s sl.SSLSock etImpl.a(D ashoA12275 )
at com.sun.net.ssl.internal.s sl.SSLSock etImpl.j(D ashoA12275 )
at com.sun.net.ssl.internal.s sl.SSLSock etImpl.a(D ashoA12275 )
at com.sun.net.ssl.internal.s sl.AppOutp utStream.w rite(Dasho A12275)
at org.apache.commons.httpcli ent.HttpCo nnection$W rappedOutp utStream.w rite(HttpC onnection. java:1360)
at java.io.BufferedOutputStre am.flushBu ffer(Buffe redOutputS tream.java :66)
at java.io.BufferedOutputStre am.flush(B ufferedOut putStream. java:124)
at org.apache.commons.httpcli ent.HttpCo nnection.f lushReques tOutputStr eam(HttpCo nnection.j ava:790)
at org.apache.commons.httpcli ent.HttpMe thodBase.w riteReques t(HttpMeth odBase.jav a:2271)
at org.apache.commons.httpcli ent.HttpMe thodBase.p rocessRequ est(HttpMe thodBase.j ava:2651)
at org.apache.commons.httpcli ent.HttpMe thodBase.e xecute(Htt pMethodBas e.java:108 7)
at org.apache.commons.httpcli ent.HttpCl ient.execu teMethod(H ttpClient. java:643)
at org.apache.commons.httpcli ent.HttpCl ient.execu teMethod(H ttpClient. java:497)
at com.tangoe.cmp.tools.HttpF ilePost.si mplePost(H ttpFilePos t.java:510 )
at com.tangoe.cmp.tools.HttpF ilePost.pe rformPost( HttpFilePo st.java:42 1)
at com.tangoe.cmp.tools.HttpF ilePost.ma in(HttpFil ePost.java :1000)
Caused by: sun.security.validator.Val idatorExce ption: No trusted certificate found
at sun.security.validator.Sim pleValidat or.buildTr ustedChain (SimpleVal idator.jav a:304)
at sun.security.validator.Sim pleValidat or.engineV alidate(Si mpleValida tor.java:1 07)
at sun.security.validator.Val idator.val idate(Vali dator.java :202)
at com.sun.net.ssl.internal.s sl.X509Tru stManagerI mpl.checkS erverTrust ed(DashoA1 2275)
at com.sun.net.ssl.internal.s sl.JsseX50 9TrustMana ger.checkS erverTrust ed(DashoA1 2275)
... 19 more
with regular http, the file uploads normally
Yes, it gives the following error:
An IOException occurred attempting to POST the file: test.txt
javax.net.ssl.SSLHandshake
at com.sun.net.ssl.internal.s
at com.sun.net.ssl.internal.s
at com.sun.net.ssl.internal.s
at com.sun.net.ssl.internal.s
at com.sun.net.ssl.internal.s
at com.sun.net.ssl.internal.s
at com.sun.net.ssl.internal.s
at com.sun.net.ssl.internal.s
at com.sun.net.ssl.internal.s
at com.sun.net.ssl.internal.s
at org.apache.commons.httpcli
at java.io.BufferedOutputStre
at java.io.BufferedOutputStre
at org.apache.commons.httpcli
at org.apache.commons.httpcli
at org.apache.commons.httpcli
at org.apache.commons.httpcli
at org.apache.commons.httpcli
at org.apache.commons.httpcli
at com.tangoe.cmp.tools.HttpF
at com.tangoe.cmp.tools.HttpF
at com.tangoe.cmp.tools.HttpF
Caused by: sun.security.validator.Val
at sun.security.validator.Sim
at sun.security.validator.Sim
at sun.security.validator.Val
at com.sun.net.ssl.internal.s
at com.sun.net.ssl.internal.s
... 19 more
with regular http, the file uploads normally
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if thats a test cert then try a custom factory along the lines of:
http://svn.apache.org/viewvc/jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/EasySSLProtocolSocketFactory.java?view=markup
http://svn.apache.org/viewvc/jakarta/commons/proper/httpclient/trunk/src/contrib/org/apache/commons/httpclient/contrib/ssl/EasySSLProtocolSocketFactory.java?view=markup
> 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.
depending on the interface it provides you can most likely use it for https.
ASKER
>>CEHJ
This TrustManager has to be at the server servelt...right?
This TrustManager has to be at the server servelt...right?
>>This TrustManager has to be at the server servelt...right?
It should be at the box from which you're trying to upload
It should be at the box from which you're trying to upload
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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-t ype", strBuf.toString());
authPost.setFollowRedirect s(true);
int authStatusCode = client.executeMethod(authP ost);
//put the response body in the variable so clients can retrieve it later on
responseBody = postMethod.getResponseBody AsString() ;
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.setConnectionTimeou t(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.setRequestConte ntLength(( 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.setRequestHeade r("Content -type", strBuf.toString());
// add any custom headers
addCustomRequestHeaders(po stMethod);
postMethod.setFollowRedire cts(true);
statusCode1 = client.executeMethod(postM ethod);
postMethod.releaseConnecti on();
StatusLine statLine = postMethod.getStatusLine() ;
if (verbose)
{
logger.info("(simplePost) Status Line: " + statLine);
}
logger.info(buildSummaryLi ne(statLin e, aFile));
}
catch (IOException ioe)
{
if (bufStream != null)
{
bufStream.close();
}
throw ioe;
}
bufStream.close();
return statusCode1;
}
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)
// set the content type and try to automatically follow redirects
authPost.setRequestHeader(
authPost.setFollowRedirect
int authStatusCode = client.executeMethod(authP
//put the response body in the variable so clients can retrieve it later on
responseBody = postMethod.getResponseBody
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.setConnectionTimeou
// 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(
postMethod.setRequestConte
StringBuffer strBuf = new StringBuffer(100);
strBuf.append(contentType)
// set the content type and try to automatically follow redirects
postMethod.setRequestHeade
// add any custom headers
addCustomRequestHeaders(po
postMethod.setFollowRedire
statusCode1 = client.executeMethod(postM
postMethod.releaseConnecti
StatusLine statLine = postMethod.getStatusLine()
if (verbose)
{
logger.info("(simplePost) Status Line: " + statLine);
}
logger.info(buildSummaryLi
}
catch (IOException ioe)
{
if (bufStream != null)
{
bufStream.close();
}
throw ioe;
}
bufStream.close();
return statusCode1;
}
ASKER
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!
:-)
http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/methods/multipart/package-frame.html