String userId="Username"; String password="Password"; String encoding=null; OutputStream dstream=null; InputStream inputStreamServlet = null; String url = "http://URL"; try { if(userId!=null && userId.trim().length()>0 ){ String userPassword = userId + ":" + password; encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes()); } URL u = new URL(url); // Open the connection and prepare to POST URLConnection uc = u.openConnection(); uc.setDoOutput(true); uc.setDoInput(true); uc.setAllowUserInteraction(false); uc.setRequestProperty("Content-Type", "multipart/form-data"); if(encoding!=null) uc.setRequestProperty ("Authorization", "Basic " + encoding); // write data to URL connection dstream = uc.getOutputStream(); int i=0; byte[] b = new byte[1024]; while ((i=inputStream.read(b)) != -1) { dstream.write(b,0,i); } inputStreamServlet = uc.getInputStream(); dstream.flush(); }catch(Exception ex){ ex.printStackTrace(); // throw ex; }finally{ try { if(dstream!=null) dstream.close(); // inputStreamServlet.close(); } catch (IOException e) { //throw e; // TODO Auto-generated catch block } }