java.net.ProtocolException: cannot write to a URLConnection
910067Jan 10 2012 — edited Jan 16 2012I want to upload a file to a computer in the network. There is proxy so i had to set proxy settings but ı've got this exception:
java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)
I couldn't solve this exception.
Here is my code :
StringBuffer filePath = new StringBuffer("file:\\\\10.253.8.77\\Folder");
Properties sysProperties = System.getProperties();
sysProperties.put("http.proxyHost", "proxy.XXX.intra");
sysProperties.put("http.proxyPort", "80");
sysProperties.put("http.proxySet", "true");
URL url = new URL( filePath.toString() );
URLConnection urlc = url.openConnection();
urlc.setDoInput (true);
urlc.setDoOutput (true);
urlc.setUseCaches(false);
urlc.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");
urlc.setAllowUserInteraction(true);
DataOutputStream dataOutputStream = (DataOutputStream)urlc.getOutputStream();
String content = "USERNAME=" + URLEncoder.encode ("XXXX") + "&PASSWORD=" + URLEncoder.encode ("987654321");
dataOutputStream.writeBytes (content);
//dataOutputStream.write(file.getFileData());
dataOutputStream.flush();
dataOutputStream.close();