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 Bitmap downLoadBitmap(String httpUrl) {
    InputStream inputStream = null;
    try {/*w ww.  j av a  2 s .  co  m*/
        URL url = new URL(httpUrl);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.setReadTimeout(5000);
        conn.setConnectTimeout(5000);
        conn.connect();
        int responseCode = conn.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            inputStream = conn.getInputStream();

            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

            return bitmap;
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return null;
}

From source file:FainClasses.HttpBasicAuth.java

public static void auth() {

    String sHTML = "Can not load page";
    URL url;//from w  ww .j av  a 2  s  . c  o  m
    InputStream is;
    BufferedReader buff = null;

    try {
        url = new URL("http://vrgaz.ru/lk/index.php?page=ls&lss=1600013620");
        url.getAuthority();

        String encoding = Base64.encodeBase64String("md_dimka@mail.ru:ufpyflt;ls".getBytes());

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setRequestProperty("Cookie", "PHPSESSID=343266771f5de3a489ba82fe79809854");
        // connection.
        //connection.setRequestProperty("Authorization", "Basic " + encoding);

        /*connection.setRequestProperty("avl","md_dimka@mail.ru");
        connection.setRequestProperty("avp","ufpyflt;ls");
         */
        //connection.setRequestProperty("page", "ls");
        //connection.setRequestProperty("lss", "1600013620");

        //URL url = new URL("http://vrgaz.ru/lk/");  
        // url = new URL(sUrl);
        //is = url.openStream();
        is = connection.getInputStream();
        buff = new BufferedReader(new InputStreamReader(is, "windows-1251"));
        StringBuilder page = new StringBuilder();
        String tmp;
        while ((tmp = buff.readLine()) != null) {
            page.append(tmp).append("\n");
        }

        sHTML = page.toString();
        System.out.println(sHTML);
        //            InputStream content = (InputStream) connection.getInputStream();
        //            BufferedReader in
        //                    = new BufferedReader(new InputStreamReader(content));
        //            String line;
        //            while ((line = in.readLine()) != null) {
        //                System.out.println(line);
        //            }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.dianping.phoenix.dev.core.tools.generator.stable.GitRepositoryListGenerator.java

private static String curl(String url) throws Exception {
    URL reqUrl = new URL(url);
    HttpURLConnection conn = (HttpURLConnection) reqUrl.openConnection();
    conn.setRequestMethod("GET");
    conn.setConnectTimeout(3000);/*from w  w w .  j av  a 2  s  . com*/
    System.out.println(url);
    return IOUtils.toString(new InputStreamReader(conn.getInputStream(), "UTF-8"));
}

From source file:com.dianping.phoenix.dev.core.tools.generator.stable.GitRepositoryListGenerator.java

private static int curl_code(String url) throws Exception {
    URL reqUrl = new URL(url);
    HttpURLConnection conn = (HttpURLConnection) reqUrl.openConnection();
    conn.setRequestMethod("GET");
    conn.setConnectTimeout(3000);/*  w  w w .j  a v a2s . com*/

    conn.setRequestProperty("Cookie",
            "uid=code51c3bceea328e0.87696853; remember_user_token=BAhbB1sGaXFJIiIkMmEkMTAkQUR2YXNENGNqT2NSaDBvVWRSS2guTwY6BkVU--bd148a50388e51e524e89809afd6d98e69793711; request_method=GET; _gitlab_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJTMzY2U3YzM3YjlhZWQ1ODcwNDljNDA0MjFkOTdjZjA4BjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMTNqS1V3V2paUCtCWU0rbVZtTUVraFBpTDR3MGFpZ2FtaExXeWphYjJRSVE9BjsARkkiGXdhcmRlbi51c2VyLnVzZXIua2V5BjsAVFsHWwZpcUkiIiQyYSQxMCRBRHZhc0Q0Y2pPY1JoMG9VZFJLaC5PBjsAVA%3D%3D--af5d6859bcba9e86e72ca6c3b2430400db75dcad");
    return conn.getResponseCode();
}

From source file:Main.java

public static String get(String url) {
    try {/*from  w  w w  .  j ava2  s .com*/
        URL u = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) u.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Charset", "UTF-8");
        InputStream is = connection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String response = "";
        String readLine = "";
        while ((readLine = br.readLine()) != null) {
            response += readLine;
        }
        br.close();
        return response;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String getPosthtml(String posturl, String postData, String encode) {

    String html = "";
    URL url;//w  w  w  .  j av a  2  s  .  c  om
    try {
        url = new URL(posturl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestProperty("User-Agent", userAgent);

        String postDataStr = postData;
        byte[] bytes = postDataStr.getBytes("utf-8");
        connection.setRequestProperty("Content-Length", "" + bytes.length);
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Cache-Control", "no-cache");
        connection.setDoOutput(true);
        connection.setReadTimeout(timeout);
        connection.setFollowRedirects(true);
        connection.connect();
        OutputStream outStrm = connection.getOutputStream();
        outStrm.write(bytes);
        outStrm.flush();
        outStrm.close();
        InputStream inStrm = connection.getInputStream();

        BufferedReader br = new BufferedReader(new InputStreamReader(inStrm, encode));

        String temp = "";
        while ((temp = br.readLine()) != null) {
            html += (temp + '\n');
        }
        br.close();
        connection.disconnect();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return html;

}

From source file:Clima.Clima.java

public static String getHTML(String urlToRead) throws Exception {
    StringBuilder result = new StringBuilder();
    URL url = new URL(urlToRead);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;/*from   w ww  . jav  a 2  s  . com*/
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    rd.close();
    return result.toString();
}

From source file:com.blogspot.ryanfx.auth.GoogleUtil.java

/**
 * Sends the auth token to google and gets the json result.
 * @param token auth token//from   w  ww.j  av a 2s.  c om
 * @return json result if valid request, null if invalid.
 * @throws IOException
 * @throws LoginException
 */
private static String issueTokenGetRequest(String token) throws IOException, LoginException {
    int timeout = 2000;
    URL u = new URL("https://www.googleapis.com/oauth2/v2/userinfo");
    HttpURLConnection c = (HttpURLConnection) u.openConnection();
    c.setRequestMethod("GET");
    c.setRequestProperty("Content-length", "0");
    c.setRequestProperty("Authorization", "OAuth " + token);
    c.setUseCaches(false);
    c.setAllowUserInteraction(false);
    c.setConnectTimeout(timeout);
    c.setReadTimeout(timeout);
    c.connect();
    int status = c.getResponseCode();
    if (status == HttpServletResponse.SC_OK) {
        BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }
        br.close();
        return sb.toString();
    } else if (status == HttpServletResponse.SC_UNAUTHORIZED) {
        Logger.getLogger(GoogleUtil.class.getName()).severe("Invalid token request: " + token);
    }
    return null;
}

From source file:net.sf.taverna.t2.renderers.RendererUtils.java

public static long getSizeInBytes(Path path) throws IOException {
    if (isValue(path))
        return Files.size(path);
    if (!isReference(path))
        throw new IllegalArgumentException("Path is not a value or reference");

    URL url = getReference(path).toURL();
    switch (url.getProtocol().toLowerCase()) {
    case "http":
    case "https":
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("HEAD");
        conn.connect();//from   ww w .  j ava 2 s  .  c  om
        String contentLength = conn.getHeaderField("Content-Length");
        conn.disconnect();
        if (contentLength != null && !contentLength.isEmpty())
            return Long.parseLong(contentLength);
        return -1;
    case "file":
        return FileUtils.toFile(url).length();
    default:
        return -1;
    }
}

From source file:Main.java

public static String jsonGetRequest(String urlQueryString) {
    String json = null;//from   w  w w .j  a  v a 2s  . co m
    try {
        URL url = new URL(urlQueryString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("charset", "utf-8");
        connection.connect();
        InputStream inStream = connection.getInputStream();
        json = streamToString(inStream); // input stream to string
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return json;
}