0

I'm using HttpUrlConnection and I would like to change the request method to put, this is the code:

uri = new URL(url);
        con = (HttpURLConnection) uri.openConnection();
        con.setConnectTimeout(60000); //60 secs
        con.setReadTimeout(60000); //60 secs
        //con.setRequestProperty("Accept-Encoding", "Your Encoding");
        con.setRequestProperty("Authorization", authCode);
        con.setRequestProperty("Content-Type", contentType);
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setRequestMethod(type);

but when i debug the project, the method is GET what must I do so i can set it to PUT

Fakher
  • 2,098
  • 3
  • 29
  • 45

1 Answers1

1
 URL url = null;
    try {
        url = new URL("http://localhost:8080/putservice");
    } catch (MalformedURLException exception) {
        exception.printStackTrace();
    }
    HttpURLConnection httpURLConnection = null;
    DataOutputStream dataOutputStream = null;
    try {
        httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        httpURLConnection.setRequestMethod("PUT");
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
        dataOutputStream.write("hello");
    } catch (IOException exception) {
        exception.printStackTrace();
    }  finally {
        if (dataOutputStream != null) {
            try {
                dataOutputStream.flush();
                dataOutputStream.close();
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        if (httpsURLConnection != null) {
            httpsURLConnection.disconnect();
        }
    }
kidyu
  • 56
  • 2
  • thank u for this answer, but as u see, ur code for setting request method is the same as mine ! that's my problem ! – Fakher Sep 06 '16 at 09:51