Example usage for java.net URLConnection setRequestProperty

List of usage examples for java.net URLConnection setRequestProperty

Introduction

In this page you can find the example usage for java.net URLConnection setRequestProperty.

Prototype

public void setRequestProperty(String key, String value) 

Source Link

Document

Sets the general request property.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    URL url = new URL("http://hostname:80");
    URLConnection conn = url.openConnection();

    conn.setRequestProperty("Cookie", "name1=value1; name2=value2");

    conn.connect();//from w w  w .j av a 2 s . c  o m
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.x.com");
    URLConnection urlc = url.openConnection();
    urlc.setRequestProperty("User-Agent", "Mozilla 5.0 (Windows; U; " + "Windows NT 5.1; en-US; rv:1.8.0.11) ");

    InputStream is = urlc.getInputStream();
    int c;//ww  w  . ja v a 2  s. com
    while ((c = is.read()) != -1)
        System.out.print((char) c);
}

From source file:com.manning.blogapps.chapter10.examples.AuthGetJava.java

public static void main(String[] args) throws Exception {
    if (args.length < 3) {
        System.out.println("USAGE: authget <username> <password> <url>");
        System.exit(-1);/*from   ww  w. j a  va 2 s  . com*/
    }
    String credentials = args[0] + ":" + args[1];

    URL url = new URL(args[2]);
    URLConnection conn = url.openConnection();

    conn.setRequestProperty("Authorization",
            "Basic " + new String(Base64.encodeBase64(credentials.getBytes())));

    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    String s = null;
    while ((s = in.readLine()) != null) {
        System.out.println(s);
    }
}

From source file:Main.java

License:asdf

public static void main(String[] argv) throws Exception {

    URLConnection conn = new URL("http://www.yourserver.com").openConnection();
    conn.setDoInput(true);/*from   ww  w  .j  a v  a 2 s  . c o  m*/
    conn.setRequestProperty("Authorization", "asdfasdf");
    conn.connect();

    InputStream in = conn.getInputStream();
}

From source file:com.tbs.devcorner.simple.App.java

public static void main(String[] args) {
    try {//ww w. ja v a 2  s.  co m
        // Build Connection
        URL api_url = new URL("http://www.earthquakescanada.nrcan.gc.ca/api/earthquakes/");
        URLConnection api = api_url.openConnection();

        // Set HTTP Headers
        api.setRequestProperty("Accept", "application/json");
        api.setRequestProperty("Accept-Language", "en");

        // Get Response
        JSONTokener tokener = new JSONTokener(api.getInputStream());
        JSONObject jsondata = new JSONObject(tokener);

        // Display API name
        System.out.println(jsondata.getJSONObject("metadata").getJSONObject("request").getJSONObject("name")
                .get("en").toString());

        // Iterate over latest links
        JSONObject latest = jsondata.getJSONObject("latest");
        for (Object item : latest.keySet()) {
            System.out.println(item.toString() + " -> " + latest.get(item.toString()));
        }

    } catch (MalformedURLException e) {
        System.out.println("Malformed URL");
    } catch (IOException e) {
        System.out.println("IO Error");
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String sessionCookie = null;//  w w  w . jav  a  2  s  .c o m
    URL url = new java.net.URL("http://127.0.0.1/yourServlet");
    URLConnection con = url.openConnection();
    if (sessionCookie != null) {
        con.setRequestProperty("cookie", sessionCookie);
    }
    con.setUseCaches(false);
    con.setDoOutput(true);
    con.setDoInput(true);
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(byteOut);
    out.flush();
    byte buf[] = byteOut.toByteArray();
    con.setRequestProperty("Content-type", "application/octet-stream");
    con.setRequestProperty("Content-length", "" + buf.length);
    DataOutputStream dataOut = new DataOutputStream(con.getOutputStream());
    dataOut.write(buf);
    dataOut.flush();
    dataOut.close();
    DataInputStream in = new DataInputStream(con.getInputStream());
    int count = in.readInt();
    in.close();
    if (sessionCookie == null) {
        String cookie = con.getHeaderField("set-cookie");
        if (cookie != null) {
            sessionCookie = parseCookie(cookie);
            System.out.println("Setting session ID=" + sessionCookie);
        }
    }

    System.out.println(count);
}

From source file:com.tbs.devcorner.simple.App.java

public static void main(String[] args) {
    try {// www. j  ava 2s . com
        // Crer la connexion
        URL api_url = new URL("http://www.earthquakescanada.nrcan.gc.ca/api/earthquakes/");
        URLConnection api = api_url.openConnection();

        // Dfinir les en-ttes HTTP
        api.setRequestProperty("Accept", "application/json");
        api.setRequestProperty("Accept-Language", "fr");

        // Obtenir la rponse
        JSONTokener tokener = new JSONTokener(api.getInputStream());
        JSONObject jsondata = new JSONObject(tokener);

        // Afficher le nom de lAPI
        System.out.println(jsondata.getJSONObject("metadata").getJSONObject("request").getJSONObject("name")
                .get("fr").toString());

        // Rpter lopration sur les liens les plus rcents
        JSONObject latest = jsondata.getJSONObject("latest");
        for (Object item : latest.keySet()) {
            System.out.println(item.toString() + " -> " + latest.get(item.toString()));
        }

    } catch (MalformedURLException e) {
        System.out.println("URL mal forme");
    } catch (IOException e) {
        System.out.println("Erreur dE/S");
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://localhost:8080/postedresults.jsp");
    URLConnection conn = url.openConnection();
    conn.setDoInput(true);//from www  .j a  va  2 s.  c o m
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
    String content = "CONTENT=HELLO JSP !&ONEMORECONTENT =HELLO POST !";

    out.writeBytes(content);
    out.flush();
    out.close();

    DataInputStream in = new DataInputStream(conn.getInputStream());
    String str;
    while (null != ((str = in.readUTF()))) {
        System.out.println(str);
    }
    in.close();
}

From source file:com.manning.blogapps.chapter10.examples.AuthPostJava.java

public static void main(String[] args) throws Exception {
    if (args.length < 4) {
        System.out.println("USAGE: authpost <username> <password> <filepath> <url>");
        System.exit(-1);//from w w w . j ava2 s.  co  m
    }
    String credentials = args[0] + ":" + args[1];
    String filepath = args[2];

    URL url = new URL(args[3]);
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    conn.setRequestProperty("Authorization",
            "Basic " + new String(Base64.encodeBase64(credentials.getBytes())));

    File upload = new File(filepath);
    conn.setRequestProperty("name", upload.getName());

    String contentType = "application/atom+xml; charset=utf8";
    if (filepath.endsWith(".gif"))
        contentType = "image/gif";
    else if (filepath.endsWith(".jpg"))
        contentType = "image/jpg";
    conn.setRequestProperty("Content-type", contentType);

    BufferedInputStream filein = new BufferedInputStream(new FileInputStream(upload));
    BufferedOutputStream out = new BufferedOutputStream(conn.getOutputStream());
    byte buffer[] = new byte[8192];
    for (int count = 0; count != -1;) {
        count = filein.read(buffer, 0, 8192);
        if (count != -1)
            out.write(buffer, 0, count);
    }
    filein.close();
    out.close();

    String s = null;
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    while ((s = in.readLine()) != null) {
        System.out.println(s);
    }
}

From source file:com.maverick.ssl.SSLSocket.java

public static void main(String[] args) {

    try {/*ww  w  . j a v  a 2  s .co m*/

        HttpsURLStreamHandlerFactory.addHTTPSSupport();

        URL url = new URL("https://localhost"); //$NON-NLS-1$

        URLConnection con = url.openConnection();
        con.setDoInput(true);
        con.setDoOutput(false);
        con.setAllowUserInteraction(false);

        con.setRequestProperty("Accept", //$NON-NLS-1$
                "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*"); //$NON-NLS-1$
        con.setRequestProperty("Accept-Encoding", "gzip, deflate"); //$NON-NLS-1$ //$NON-NLS-2$
        con.setRequestProperty("Accept-Language", "en-gb"); //$NON-NLS-1$ //$NON-NLS-2$
        con.setRequestProperty("Connection", "Keep-Alive"); //$NON-NLS-1$ //$NON-NLS-2$
        con.setRequestProperty("User-Agent", //$NON-NLS-1$
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)"); //$NON-NLS-1$

        con.connect();

        InputStream in = con.getInputStream();
        int read;

        while ((read = in.read()) > -1) {
            System.out.write(read);
        }

    } catch (SSLIOException ex) {
        ex.getRealException().printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}