I am trying to write to an http server, but nothing happens. I can read from the file, I just cannot write to it and no errors are thrown. I do not know how to proceed.
Specifically, I have a text file on my server, and I can read from it (the text file already has content), but I cannot write to it. And, no exceptions are thrown.
url = new URL("http://kajl-ig.com/txt.txt"); urlConn = url.openConnection(); urlConn.setDoInput(true); urlConn.setDoOutput(true); urlConn.setUseCaches(false); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(urlConn.getOutputStream())); String s = "TEST Successfull!"; bw.write(s); // doesn't work bw.close(); BufferedReader br = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); System.out.println(br.readLine()); // works } catch (MalformedURLException mue) { System.out.println("MUE"); } catch (IOException ioe) { System.out.println("IOE"); }
That was my initial code, here is another API I tried:
URL url; URLConnection urlConn; DataOutputStream dos; DataInputStream dis; url = new URL("http://kajl-ig.com/txt.txt"); urlConn = url.openConnection(); urlConn.setDoInput(true); urlConn.setDoOutput(true); urlConn.setUseCaches(false); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(urlConn.getOutputStream())); String message = "TEST Successfull!"; dos = new DataOutputStream (urlConn.getOutputStream()); dos.writeBytes(message); dos.flush(); dos.close(); dis = new DataInputStream(urlConn.getInputStream()); String s = dis.readLine(); dis.close(); System.out.println(s+" "+dos.size()); } // end of "try" catch (MalformedURLException mue) { System.out.println("MUE"); } catch (IOException ioe) { System.out.println("IOE"); }
The second code, I got from some random website, and I was desperate so I tried it. I am not sure what the
method does, so I removed it and it reads fine it just cannot write.urlConn.setRequestProperty (String, String);