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 String requestData(String address) throws IOException {
    URL url = new URL(address);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setConnectTimeout(5000);//from  ww w .j  av  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;
}

From source file:dev.meng.wikidata.util.http.HttpUtils.java

public static JSONObject queryForJSONResponse(URL url, String encoding)
        throws ProtocolException, IOException, StringConvertionException {
    JSONObject response = null;/*from   ww  w . j  av  a 2 s  . c o m*/

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.connect();

    if (connection.getResponseCode() == 200) {
        response = new JSONObject(StringUtils.inputStreamToString(connection.getInputStream(), encoding));
    } else {
        throw new IOException("Error in opening: " + url + ", " + connection.getResponseCode() + " "
                + connection.getResponseMessage());
    }

    return response;
}

From source file:Main.java

public static HttpURLConnection buildConnection(String url, String requestMethod) throws IOException {
    HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
    /*set request*/
    con.setRequestMethod(requestMethod);
    con.setRequestProperty("Accept-Language", "UTF-8");
    con.setDoOutput(true);/*from w  w w.j  a v  a2 s .  c  om*/
    return con;
}

From source file:Main.java

public static byte[] getBytes(String url) {
    try {/*from ww w . j a v  a2 s  .  c  o m*/
        URL u = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) u.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Charset", "UTF-8");
        InputStream is = connection.getInputStream();
        int contentLength = connection.getContentLength();
        byte[] data = new byte[contentLength];
        int alreadyRead = 0;
        int len = 0;
        while (alreadyRead < contentLength) {
            len = is.read(data, alreadyRead, contentLength - alreadyRead);
            alreadyRead += len;
        }
        is.close();
        return data;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}

From source file:com.mycompany.test.Jaroop.java

/**
 * @throws MalformedURLException/* ww w. j  a va 2 s.  c o m*/
 * @throws ProtocolException
 * @throws IOException
 * This method obtains the HTTP connection required in order to make a GET request.
 */
private static HttpURLConnection getConnection(String url)
        throws MalformedURLException, ProtocolException, IOException {
    URL urlObject = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) urlObject.openConnection();
    connection.setRequestMethod("GET");
    return connection;
}

From source file:bakuposter.gcm.server.POST2GCMessage.java

public static void post(String apiKey, Content content) {

    try {/*from  ww w  .j  a v  a  2 s .c o m*/
        URL url = new URL("https://android.googleapis.com/gcm/send");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Authorization", "key=" + apiKey);
        conn.setDoOutput(true);
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(content.getRegistration_ids().get(0));
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        mapper.writeValue(wr, content);
        wr.flush();
        wr.close();
        int responseCode = conn.getResponseCode();
        System.out.println("responseCode = " + responseCode);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String request(String httpUrl, String httpArg) {
    BufferedReader reader = null;
    String result = null;//w w w .java 2 s.  co m
    StringBuffer sbf = new StringBuffer();
    httpUrl = httpUrl + "?" + httpArg;

    try {
        URL url = new URL(httpUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("apikey", "2ffbcae4d025b6c109af30d7de2d7c09");
        connection.connect();
        InputStream is = connection.getInputStream();
        reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        String strRead = null;
        while ((strRead = reader.readLine()) != null) {
            sbf.append(strRead);
            sbf.append("\r\n");
        }
        reader.close();
        result = sbf.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

/** 
 * Get image from newwork /*from w w w.j  a  v a2s. c  o m*/
 * @param path The path of image 
 * @return InputStream 
 * @throws Exception 
 */
public static InputStream getImageStream(String path) throws Exception {
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setConnectTimeout(5 * 1000);
    conn.setRequestMethod("GET");
    if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return conn.getInputStream();
    }
    return null;
}

From source file:com.uksf.mf.core.utility.ClassNames.java

/**
 * Checks if connection to URL can be established
 * @param url url to check/*from   w  w  w . j a  v  a2 s .com*/
 * @return connection state
 * @throws IOException error
 */
private static boolean checkConnection(URL url) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setConnectTimeout(1500);
    connection.setReadTimeout(1500);
    connection.setRequestProperty("User-Agent",
            "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
    connection.connect();
    return connection.getResponseCode() == 404;
}

From source file:Main.java

public static void connectAndSendHttp(ByteArrayOutputStream baos) {
    try {//  ww  w  . j a  v  a2  s.co m

        URL url;
        url = new URL("http://10.0.2.2:8080");

        String charset = "UTF-8";

        HttpURLConnection conn;

        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestProperty("Accept-Charset", charset);
        conn.setRequestProperty("ENCTYPE", "multipart/form-data");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
        OutputStream output = conn.getOutputStream();
        output.write(baos.toByteArray());
        output.close();
        conn.getInputStream();

    } catch (IOException e) {
        e.printStackTrace();
    }
}