Example usage for java.net HttpURLConnection setRequestMethod

List of usage examples for java.net HttpURLConnection setRequestMethod

Introduction

In this page you can find the example usage for java.net HttpURLConnection setRequestMethod.

Prototype

public void setRequestMethod(String method) throws ProtocolException 

Source Link

Document

Set the method for the URL request, one of:
  • GET
  • POST
  • HEAD
  • OPTIONS
  • PUT
  • DELETE
  • TRACE
are legal, subject to protocol restrictions.

Usage

From source file:io.fares.junit.soapui.outside.test.SoapUIMockRunnerTest.java

public static String testMockService(String endpoint) throws MalformedURLException, IOException {

    HttpURLConnection con = (HttpURLConnection) new URL(endpoint).openConnection();
    con.setRequestMethod("POST");
    con.addRequestProperty("Accept", "application/soap+xml");
    con.addRequestProperty("Content-Type", "application/soap+xml");
    con.setDoOutput(true);//from   www  . j a  v a2 s. c  o  m
    con.getOutputStream().write(
            "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"><soap:Header/><soap:Body><GetWeather xmlns=\"http://www.webserviceX.NET\"><CityName>Brisbane</CityName></GetWeather></soap:Body></soap:Envelope>"
                    .getBytes("UTF-8"));
    InputStream is = con.getInputStream();
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");
    String rs = writer.toString();
    LOG.fine(rs);
    return rs;

}

From source file:org.cytoscape.app.internal.net.server.ScreenOriginsBeforeResponseTest.java

private static HttpURLConnection connectToURL(String urlString, String method, String origin) throws Exception {
    final URL url = new URL(urlString);
    final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod(method);
    connection.setDoOutput(true);/*from w w  w .  j  a  v a2 s  .  com*/
    if (origin != null)
        connection.setRequestProperty("Origin", origin);
    connection.setConnectTimeout(1000);
    connection.connect();
    return connection;
}

From source file:com.microsoft.webapp.util.WebAppUtils.java

public static void sendGet(String sitePath) throws Exception {
    URL url = new URL(sitePath);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    con.setRequestProperty("User-Agent", "AzureToolkit for Eclipse");
    con.getResponseCode();//w  w  w  . ja v  a2s .  com
}

From source file:Main.java

/**
 * Uses http to post an XML String to a URL.
 *
 * @param url//from w w  w . ja  va  2  s . c  o  m
 * @param xmlString
 * @param return
 * @throws IOException
 * @throws UnknownHostException
 */
public static HttpURLConnection post(String url, String xmlString) throws IOException, UnknownHostException {

    // open connection
    URL urlObject = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) urlObject.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "text/xml; charset=\"utf-8\"");
    connection.setDoOutput(true);

    OutputStreamWriter outStream = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
    outStream.write(xmlString);
    outStream.close();

    return connection;
}

From source file:Main.java

static public String downloadFile(String downloadUrl, String fileName, String dir) throws IOException {
    URL url = new URL(downloadUrl);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoOutput(true);/*w  w  w  . j  a  v  a  2 s.  c o m*/
    urlConnection.connect();
    int code = urlConnection.getResponseCode();
    if (code > 300 || code == -1) {
        throw new IOException("Cannot read url: " + downloadUrl);
    }
    String filePath = prepareFilePath(fileName, dir);
    String tmpFilePath = prepareTmpFilePath(fileName, dir);
    FileOutputStream fileOutput = new FileOutputStream(tmpFilePath);
    BufferedInputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
    byte[] buffer = new byte[1024];
    int bufferLength;
    while ((bufferLength = inputStream.read(buffer)) > 0) {
        fileOutput.write(buffer, 0, bufferLength);
    }
    fileOutput.close();
    // move tmp to destination
    copyFile(new File(tmpFilePath), new File(filePath));
    return filePath;
}

From source file:org.cytoscape.app.internal.net.server.AddAccessControlAllowOriginHeaderAfterResponseTest.java

private static HttpURLConnection connectToURL(String urlString, String method) throws Exception {
    final URL url = new URL(urlString);
    final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod(method);
    connection.setDoOutput(true);/*from w  w w.j a v a2s . c o m*/
    connection.setConnectTimeout(1000);
    connection.connect();
    return connection;
}

From source file:Main.java

public static String getJsonContent(String url_path) {
    try {/*ww  w.j a  v  a2  s  .  c  o  m*/
        URL url = new URL(url_path);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(3000);
        connection.setRequestMethod("GET");
        connection.setDoInput(true);
        int code = connection.getResponseCode();
        if (code == 200) {
            return changeInputStream(connection.getInputStream());
        }
    } catch (Exception e) {

    }
    return "";
}

From source file:Main.java

public static boolean deleteRequest(String query) {
    HttpURLConnection connection = null;
    try {/*from w  w  w  .  j av a  2 s. c o  m*/
        connection = (HttpURLConnection) new URL(url + query).openConnection();
        connection.setRequestMethod("DELETE");
        connection.setRequestProperty("Accept-Charset", charset);

        statusCode = connection.getResponseCode();
        if (statusCode != 200) {
            return false;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return true;
}

From source file:com.pkrete.locationservice.admin.util.WebUtil.java

/**
 * Checks if the given URL exists./*from w w  w. j  ava  2s  .  c  om*/
 *
 * @param url the URL to be checked
 * @return if the URL is exists returns true, otherwise returns false
 */
public static boolean exists(String url) {
    try {
        HttpURLConnection.setFollowRedirects(true);
        HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
        con.setRequestMethod("HEAD");
        // HTTP statuses 200 and 302 are accepted
        return (con.getResponseCode() == HttpURLConnection.HTTP_OK
                || con.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return false;
    }
}

From source file:me.prokopyl.commandtools.migration.UUIDFetcher.java

private static HttpURLConnection createConnection() throws IOException {
    URL url = new URL(PROFILE_URL);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setUseCaches(false);/*  ww w . j a va2s .c  om*/
    connection.setDoInput(true);
    connection.setDoOutput(true);
    return connection;
}