Java HTTP Header doDelete(URL endpoint, Map headers)

Here you can find the source of doDelete(URL endpoint, Map headers)

Description

Send

License

Open Source License

Parameter

Parameter Description
endpoint - The URL of the server. (Example: " http://www.yahoo.com/search")
headers -

Exception

Parameter Description
Exception - ava.io.IOException If an error occurs while sending the DELETE request

Return

- The response code from the endpoint

Declaration


public static int doDelete(URL endpoint, Map<String, String> headers) throws Exception 

Method Source Code

//package com.java2s;
/*//from  w w  w  . j a  v a2 s  . c  o m
 *
 *   Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *   WSO2 Inc. licenses this file to you under the Apache License,
 *   Version 2.0 (the "License"); you may not use this file except
 *   in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing,
 *   software distributed under the License is distributed on an
 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *   KIND, either express or implied.  See the License for the
 *   specific language governing permissions and limitations
 *   under the License.
 *
 */

import java.io.IOException;

import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;

import java.util.Iterator;
import java.util.Map;

public class Main {
    /**
     * Send
      * @param endpoint - The URL of the server. (Example: " http://www.yahoo.com/search")
     * @param headers -
     * @return - The response code from the endpoint
     * @throws Exception - ava.io.IOException If an error occurs while sending the DELETE request
     */

    public static int doDelete(URL endpoint, Map<String, String> headers) throws Exception {
        HttpURLConnection urlConnection = null;
        int responseCode;
        try {
            urlConnection = (HttpURLConnection) endpoint.openConnection();
            try {
                urlConnection.setRequestMethod("DELETE");
            } catch (ProtocolException var33) {
                throw new Exception(
                        "Shouldn\'t happen: HttpURLConnection doesn\'t support DELETE?? " + var33.getMessage(),
                        var33);
            }
            urlConnection.setDoOutput(true);
            //setting headers
            if (headers != null && headers.size() > 0) {
                Iterator<String> itr = headers.keySet().iterator();
                while (itr.hasNext()) {
                    String key = itr.next();
                    if (key != null) {
                        urlConnection.setRequestProperty(key, headers.get(key));
                    }
                }
                for (String key : headers.keySet()) {
                    urlConnection.setRequestProperty(key, headers.get(key));
                }
            }
            responseCode = urlConnection.getResponseCode();
        } catch (IOException var36) {
            throw new Exception("Connection error (is server running at " + endpoint + " ?): " + var36.getMessage(),
                    var36);
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
        return responseCode;

    }
}

Related

  1. addHeader(HttpURLConnection conn, String headerName, String value)
  2. addHeadersToRequest(HttpURLConnection request, Map headers)
  3. copyHttpHeaders(@Nonnull final HttpURLConnection aConn, @Nonnull final InternetHeaders aHeaders)
  4. createHttpConnection(URL endpointUrl, String httpMethod, Map headers)
  5. delete(String url, Map headers)
  6. dumpHeaders(HttpURLConnection conn)
  7. dumpHeaders(HttpURLConnection conn, PrintStream out)
  8. dumpHttpHeaders(HttpURLConnection conn, java.io.PrintStream out)
  9. fillHeaders(HttpURLConnection urlConnection, Map headers)