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:Main.java

public static boolean delete(String murl) throws Exception {
    URL url = new URL(murl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("DELETE");
    conn.setConnectTimeout(5000);/*from   w ww.  j a va2  s. c o m*/
    if (conn.getResponseCode() == 204) {
        return true;
    }
    return false;
}

From source file:Main.java

public static InputStream getRequest(String path) throws Exception {
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setConnectTimeout(5000);//from   w  w w  .  ja v a2 s  .co m
    if (conn.getResponseCode() == 200) {
        return conn.getInputStream();
    }
    return null;
}

From source file:Main.java

static void getHTTPXml(URL url) throws Exception {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("ACCEPT", "application/xml");
    InputStream xml = conn.getInputStream();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    org.w3c.dom.Document document = builder.parse(xml);

    System.out.println(document);
    String doctype = conn.getContentType();
    System.out.println(doctype);/*from   w  ww . ja  v  a2 s . com*/

    XPathFactory pathFactory = XPathFactory.newInstance();
    XPath path = pathFactory.newXPath();
    XPathExpression expression;
    expression = path.compile("/result/checkid");
    NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
    String checkids[] = getNodeValue(nodeList);
    for (String checkid : checkids) {
        System.out.print(checkid + ", ");
    }
    conn.disconnect();
}

From source file:Main.java

private static InputStream OpenHttpConnection(String strURL, Boolean cache) throws IOException {
    InputStream inputStream = null;

    try {//from  www  .ja  v a 2 s.  c o m
        URL url = new URL(strURL);
        URLConnection conn = url.openConnection();
        conn.setUseCaches(cache);
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK)
            inputStream = httpConn.getInputStream();

    } catch (Exception ex) {
    }
    return inputStream;
}

From source file:Main.java

public static Bitmap getImage(String urlpath) throws Exception {
    Bitmap bitmap = null;//from   ww  w .j  ava  2 s.co m
    URL url = new URL(urlpath);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setConnectTimeout(5 * 1000);
    if (conn.getResponseCode() == 200) {
        InputStream inputStream = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(inputStream);
    }
    return bitmap;
}

From source file:Main.java

private static InputStream getHttpConnection(String urlString) throws IOException {
    InputStream stream = null;//from w  w  w  .  j a  v a2  s.co m
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();

    try {
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        httpConnection.setRequestMethod("GET");
        httpConnection.connect();

        if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            stream = httpConnection.getInputStream();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return stream;
}

From source file:Main.java

/**
 * Makes HttpURLConnection and returns InputStream
 *///ww w  . j  a va2  s  .c  om
private static InputStream getHttpConnection(String urlString) throws IOException {
    InputStream stream = null;
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();

    try {
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        httpConnection.setRequestMethod("GET");
        httpConnection.connect();

        if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            stream = httpConnection.getInputStream();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return stream;
}

From source file:Main.java

public static boolean isExists(String urlString) {
    try {/*w  ww. ja v  a 2 s.  co m*/
        URL u = new URL(urlString);
        HttpURLConnection huc = (HttpURLConnection) u.openConnection();
        huc.setRequestMethod("GET");
        huc.connect();
        int rc = huc.getResponseCode();
        return (rc == HttpURLConnection.HTTP_OK);
        // Handle response code here...
    } catch (UnknownHostException uhe) {
        // Handle exceptions as necessary
        Log.w("EPub", uhe.getMessage());
    } catch (FileNotFoundException fnfe) {
        // Handle exceptions as necessary
        Log.w("EPub", fnfe.getMessage());
    } catch (Exception e) {
        // Handle exceptions as necessary
        Log.w("EPub", e.getMessage());
    }
    return false;
}

From source file:Main.java

public static String stringFromHttpGet(String urlString) {
    try {//from   w w w.  ja  v a2  s. co  m
        URL url = new URL(urlString);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        return response.toString();
    } catch (Exception e) {
        e.printStackTrace();
        logError(e.getMessage());
        return null;
    }
}

From source file:Main.java

public static String requestData(String address) throws Exception {
    URL url = new URL(address);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setConnectTimeout(5000);//  w w  w.j a v a 2  s .c  o m
    connection.setReadTimeout(5000);
    String data = null;
    InputStream is = null;
    if (connection.getResponseCode() == 200) {
        is = connection.getInputStream();
        data = readFromStream(is);
    }
    if (is != null) {
        is.close();
    }
    return data;
}