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:org.spoutcraft.launcher.util.Download.java

@SuppressWarnings("unused")
public void run() {
    ReadableByteChannel rbc = null;
    FileOutputStream fos = null;//from   w  ww. j  a  va  2s.c  om
    try {
        URLConnection conn = url.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(false);
        System.setProperty("http.agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");
        conn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");
        HttpURLConnection.setFollowRedirects(true);
        conn.setUseCaches(false);
        ((HttpURLConnection) conn).setInstanceFollowRedirects(true);
        int response = ((HttpURLConnection) conn).getResponseCode();
        InputStream in = getConnectionInputStream(conn);

        size = conn.getContentLength();
        outFile = new File(outPath);
        outFile.delete();

        rbc = Channels.newChannel(in);
        fos = new FileOutputStream(outFile);

        stateChanged();

        Thread progress = new MonitorThread(Thread.currentThread(), rbc);
        progress.start();

        fos.getChannel().transferFrom(rbc, 0, size > 0 ? size : Integer.MAX_VALUE);
        in.close();
        rbc.close();
        progress.interrupt();
        if (size > 0) {
            if (size == outFile.length()) {
                result = Result.SUCCESS;
            }
        } else {
            result = Result.SUCCESS;
        }
    } catch (PermissionDeniedException e) {
        exception = e;
        result = Result.PERMISSION_DENIED;
    } catch (DownloadException e) {
        exception = e;
        result = Result.FAILURE;
    } catch (Exception e) {
        exception = e;
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(rbc);
    }
}

From source file:io.s4.example.twittertopiccount.TwitterFeedListener.java

public void connectAndRead() throws Exception {
    URL url = new URL(urlString);

    URLConnection connection = url.openConnection();
    String userPassword = userid + ":" + password;
    String encoded = EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getAsciiBytes(userPassword)));
    connection.setRequestProperty("Authorization", "Basic " + encoded);
    connection.connect();/*from www .  j  a v  a 2 s.c  om*/

    InputStream is = connection.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    String inputLine = null;
    while ((inputLine = br.readLine()) != null) {
        if (inputLine.trim().length() == 0) {
            blankCount++;
            continue;
        }
        messageCount++;
        messageQueue.add(inputLine);
    }
}

From source file:com.romeikat.datamessie.core.base.service.download.AbstractDownloader.java

protected URLConnection getConnection(final String url) throws IOException {
    final URLConnection urlConnection = new URL(url).openConnection();
    if (urlConnection instanceof HttpURLConnection) {
        final HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection;
        httpUrlConnection.setInstanceFollowRedirects(true);
    }/*from   ww  w .j a  v  a  2 s.c o  m*/
    urlConnection.setConnectTimeout(timeout);
    urlConnection.setReadTimeout(timeout);
    urlConnection.setRequestProperty("User-Agent", userAgent);
    return urlConnection;
}

From source file:it.isti.cnr.hpc.europeana.hackthon.domain.GoogleQuery2Entity.java

public List<Result> getResults(String query) throws InterruptedException {
    URL mapWikipedia;//www  .j a v a 2  s.  c  om
    try {
        mapWikipedia = new URL(query);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        logger.error(e.toString());
        return null;
    }
    URLConnection urlc;
    try {
        urlc = mapWikipedia.openConnection();
    } catch (IOException e) {
        logger.error(e.toString());
        return null;
    }
    urlc.setRequestProperty("User-Agent",
            "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11");

    InputStream is;
    try {
        is = urlc.getInputStream();
    } catch (IOException e) {
        logger.error(e.toString());
        if (e.toString().contains("sorry")) {
            logger.error("SLEEP!");
            try {
                Thread.sleep(60000 * 30);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        return null;
    }
    try {
        Thread.sleep(500);
    } catch (InterruptedException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    BufferedReader in = new BufferedReader(new InputStreamReader(is));
    String inputLine;
    boolean bodyFound = false;
    StringBuilder sb = new StringBuilder();

    try {
        while ((inputLine = in.readLine()) != null) {
            // Process each line.
            if (inputLine.contains("<body"))
                bodyFound = true;
            if (bodyFound)
                sb.append(inputLine).append(" ");
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        logger.error(e.toString());
        if (e.toString().contains("sorry")) {
            logger.error("SLEEP!");
            try {
                Thread.sleep(60000 * 30);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        return null;
    }
    try {
        in.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        logger.error(e.toString());

        return null;
    }

    return parseResults(sb.toString());
}

From source file:de.mprengemann.hwr.timetabel.data.Parser.java

protected Void doInBackground(Void... params) {
    this.exception = null;

    if (Utils.connectionChecker(context)) {
        try {//  w  w  w  . j  av  a  2 s.c o m
            final String matrikelnr = preferences.getString(context.getString(R.string.prefs_matrikelNrKey),
                    "");

            DefaultHttpClient httpclient = new DefaultHttpClient();
            if (preferences.getBoolean(context.getString(R.string.prefs_proxyFlagKey), false)) {
                HttpHost proxy = new HttpHost(
                        preferences.getString(context.getString(R.string.prefs_proxyKey), "localhost"),
                        Integer.parseInt(
                                preferences.getString(context.getString(R.string.prefs_proxyPortKey), "8080")));
                httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
            }

            HttpGet httpget = new HttpGet(Utils.buildURL(context, preferences));

            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                entity.consumeContent();
            }

            List<Cookie> cookies = httpclient.getCookieStore().getCookies();

            HttpPost httpost = new HttpPost("http://ipool.ba-berlin.de/main.php?action=login");

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("FORM_LOGIN_NAME", "student"));
            nvps.add(new BasicNameValuePair("FORM_LOGIN_PASS", matrikelnr));
            nvps.add(new BasicNameValuePair("FORM_LOGIN_PAGE", "home"));
            nvps.add(new BasicNameValuePair("FORM_LOGIN_REDIRECTION", Utils.buildURL(context, preferences)));
            nvps.add(new BasicNameValuePair("FORM_ACCEPT", "1"));
            nvps.add(new BasicNameValuePair("LOGIN", "login"));

            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

            response = httpclient.execute(httpost);
            entity = response.getEntity();

            if (entity != null) {
                entity.consumeContent();
            }

            cookies = httpclient.getCookieStore().getCookies();

            final URL url;
            InputStream is = null;
            try {
                url = new URL(Utils.buildURL(context, preferences));
                URLConnection c = url.openConnection();

                try {
                    c.setRequestProperty("Cookie", cookies.get(0).getName() + "=" + cookies.get(0).getValue());
                    c.connect();
                    is = c.getInputStream();
                } catch (NullPointerException e) {
                    throw new ConnectionAuthentificationException(
                            context.getString(R.string.dialog_error_message_auth));
                } catch (IndexOutOfBoundsException e) {
                    throw new ConnectionAuthentificationException(
                            context.getString(R.string.dialog_error_message_auth));
                }

                if (is != null) {
                    try {
                        new IcsParser(is, new OnCalendarParsingListener() {

                            @Override
                            public void onNewItem(Component c) {
                                if (c.getName().equals(Component.VEVENT)) {
                                    PropertyList p = c.getProperties();

                                    Subjects s = new Subjects();
                                    s.setTitle(p.getProperty("SUMMARY").getValue());
                                    s.setShortTitle(s.getTitle().substring(s.getTitle().indexOf("-") + 1));
                                    s.setShow(true);

                                    SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd HHmmss");

                                    Events evt = new Events();
                                    try {
                                        evt.setStart(df
                                                .parse(p.getProperty("DTSTART").getValue().replace("T", " ")));
                                        evt.setEnd(
                                                df.parse(p.getProperty("DTEND").getValue().replace("T", " ")));
                                    } catch (ParseException e) {
                                        exception = new IPoolFormatException(
                                                context.getString(R.string.dialog_error_message_ipool));
                                        sendBugReport(e, url.toString());
                                    }

                                    evt.setRoom(p.getProperty("LOCATION").getValue());
                                    evt.setUid(p.getProperty("UID").getValue());

                                    String description = p.getProperty("DESCRIPTION").getValue();
                                    evt.setFullDescription(description);
                                    for (String desc : description.split("\\n")) {
                                        if (desc.startsWith("Dozent: ")) {
                                            evt.setLecturer(desc.replace("Dozent: ", ""));
                                            break;
                                        } else if (desc.startsWith("Art: ")) {
                                            evt.setType(desc.replace("Art: ", ""));
                                        }
                                    }
                                    try {
                                        if (listener != null) {
                                            listener.onNewItem(s, evt);
                                        }
                                    } catch (SQLiteConstraintException e) {
                                        exception = new StorageException(
                                                context.getString(R.string.dialog_error_message_storage));
                                        sendBugReport(e, url.toString(), s.toString(), evt.toString());
                                    }

                                }
                            }
                        });
                    } catch (ParserException e) {
                        exception = new UnknownTimetableException(
                                context.getString(R.string.dialog_error_message_auth));
                        sendNewTimetable(url);
                    } catch (IOException e) {
                        if (e instanceof HttpHostConnectException) {
                            exception = new ConnectionTimeoutException(
                                    context.getString(R.string.dialog_error_message_timeout));
                        } else {
                            exception = new ConnectionException(
                                    context.getString(R.string.dialog_error_message));
                        }
                    }
                } else {
                    throw new IOException();
                }
            } catch (ConnectionAuthentificationException e) {
                exception = e;
            } catch (IOException e) {
                if (e instanceof ConnectTimeoutException) {
                    exception = new ConnectionTimeoutException(
                            context.getString(R.string.dialog_error_message_timeout));
                } else if (e instanceof MalformedURLException) {
                    exception = new ConnectionException(context.getString(R.string.dialog_error_message));
                    sendBugReport(e);
                } else if (e instanceof HttpHostConnectException) {
                    exception = new ConnectionTimeoutException(
                            context.getString(R.string.dialog_error_message_timeout));
                } else {
                    exception = new ConnectionException(context.getString(R.string.dialog_error_message));
                    sendBugReport(e);
                }
            } finally {
                try {
                    if (is != null) {
                        is.close();
                    }
                } catch (IOException e) {
                    if (e instanceof ConnectTimeoutException) {
                        exception = new ConnectionTimeoutException(
                                context.getString(R.string.dialog_error_message_timeout));
                    } else {
                        exception = new ConnectionException(context.getString(R.string.dialog_error_message));
                        sendBugReport(e);
                    }
                }
            }

            httpclient.getConnectionManager().shutdown();
        } catch (Exception e) {
            if (e instanceof ConnectTimeoutException) {
                exception = new ConnectionTimeoutException(
                        context.getString(R.string.dialog_error_message_timeout));
            } else {
                exception = new ConnectionException(context.getString(R.string.dialog_error_message));
                sendBugReport(e);
            }

        }
    } else {
        exception = new ConnectionException(context.getString(R.string.dialog_error_message));
    }

    return null;
}

From source file:org.y20k.transistor.helpers.MetadataHelper.java

private void shoutcastProxyReaderLoop(Socket proxy, URLConnection connection) throws IOException {

    connection.setConnectTimeout(5000);/*from  w  w  w .  j a  v a  2 s .c  om*/
    connection.setReadTimeout(5000);
    connection.setRequestProperty("Icy-MetaData", "1");
    connection.connect();

    InputStream in = connection.getInputStream();

    OutputStream out = proxy.getOutputStream();
    out.write(("HTTP/1.0 200 OK\r\n" + "Pragma: no-cache\r\n" + "Content-Type: " + connection.getContentType()
            + "\r\n\r\n").getBytes(StandardCharsets.UTF_8));

    byte buf[] = new byte[16384]; // one second of 128kbit stream
    int count = 0;
    int total = 0;
    int metadataSize = 0;
    final int metadataOffset = connection.getHeaderFieldInt("icy-metaint", 0);
    int bitRate = Math.max(connection.getHeaderFieldInt("icy-br", 128), 32);
    LogHelper.v(LOG_TAG,
            "createProxyConnection: connected, icy-metaint " + metadataOffset + " icy-br " + bitRate);
    while (true) {
        count = Math.min(in.available(), buf.length);
        if (count <= 0) {
            count = Math.min(bitRate * 64, buf.length); // buffer half-second of stream data
        }
        if (metadataOffset > 0) {
            count = Math.min(count, metadataOffset - total);
        }

        count = in.read(buf, 0, count);
        if (count == 0) {
            continue;
        }
        if (count < 0) {
            break;
        }

        out.write(buf, 0, count);

        total += count;
        if (metadataOffset > 0 && total >= metadataOffset) {
            // read metadata
            total = 0;
            count = in.read();
            if (count < 0) {
                break;
            }
            count *= 16;
            metadataSize = count;
            if (metadataSize == 0) {
                continue;
            }
            // maximum metadata length is 4080 bytes
            total = 0;
            while (total < metadataSize) {
                count = in.read(buf, total, count);
                if (count < 0) {
                    break;
                }
                if (count == 0) {
                    continue;
                }
                total += count;
                count = metadataSize - total;
            }
            total = 0;
            String[] metadata = new String(buf, 0, metadataSize, StandardCharsets.UTF_8).split(";");
            for (String s : metadata) {
                if (s.indexOf(TransistorKeys.SHOUTCAST_STREAM_TITLE_HEADER) == 0
                        && s.length() >= TransistorKeys.SHOUTCAST_STREAM_TITLE_HEADER.length() + 1) {
                    handleMetadataString(
                            s.substring(TransistorKeys.SHOUTCAST_STREAM_TITLE_HEADER.length(), s.length() - 1));
                    // break;
                }
            }
        }
    }
}

From source file:it.cicolella.ethrly.DevantechEthRly.java

private Document getXMLStatusFile(Board board) {
    //get the xml file from the socket connection
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = null;
    try {/*  w  w w. ja v a 2 s  .co  m*/
        dBuilder = dbFactory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        Logger.getLogger(DevantechEthRly.class.getName()).log(Level.SEVERE, null, ex);
    }
    Document doc = null;
    URL url = null;
    try {
        String authString = board.getUsername() + ":" + board.getPassword();
        byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
        String authStringEnc = new String(authEncBytes);
        //Create a URL for the desired  page 
        url = new URL("http://" + board.getIpAddress() + ":" + Integer.toString(board.getPort()) + "/"
                + GET_STATUS_URL);
        URLConnection urlConnection = url.openConnection();
        // if required set the authentication
        if (board.getHttpAuthentication().equalsIgnoreCase("true")) {
            urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
        }
        LOG.info("Devantech Eth-Rly gets relay status from file " + url);
        doc = dBuilder.parse(urlConnection.getInputStream());
        doc.getDocumentElement().normalize();
    } catch (ConnectException connEx) {
        disconnect();
        this.stop();
        this.setDescription("Connection timed out, no reply from the board at " + url);
    } catch (SAXException ex) {
        disconnect();
        this.stop();
        LOG.severe(Freedomotic.getStackTraceInfo(ex));
    } catch (Exception ex) {
        disconnect();
        this.stop();
        setDescription("Unable to connect to " + url);
        LOG.severe(Freedomotic.getStackTraceInfo(ex));
    }
    return doc;
}

From source file:fr.free.divde.webcam.WebcamApplet.java

public void sendImage(JSObject parameters) {
    final String url = (String) parameters.getMember("url");
    final String format = (String) getJSProperty(parameters, "format", "png");
    final Map<String, Object> headers = getJSMapProperty(parameters, "headers");
    final JSObject callback = (JSObject) getJSProperty(parameters, "callback", null);
    imageCapture.captureImage(new ImageListener() {
        @Override/*from   w w  w . j ava 2s  . c o  m*/
        public void nextFrame(BufferedImage image) {
            try {
                URL urlObject = new URL(getDocumentBase(), url);
                URLConnection connection = urlObject.openConnection();
                connection.setDoOutput(true);
                setHeaders(connection, headers);
                if (connection.getRequestProperty("content-type") == null) {
                    // default content type
                    connection.setRequestProperty("content-type", "image/" + format);
                }
                OutputStream out = connection.getOutputStream();
                ImageIO.write(image, format, out);
                out.flush();
                out.close();
                if (callback != null) {
                    StringWriter response = new StringWriter();
                    IOUtils.copy(connection.getInputStream(), response, "UTF-8");
                    callJS(callback, response.toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

From source file:com.oakesville.mythling.util.HttpHelper.java

private void prepareConnection(URLConnection conn, Map<String, String> headers) throws IOException {
    conn.setConnectTimeout(getConnectTimeout());
    conn.setReadTimeout(getReadTimeout());
    for (String key : headers.keySet())
        conn.setRequestProperty(key, headers.get(key));

    if (method == Method.Post) {
        ((HttpURLConnection) conn).setRequestMethod("POST");
        if (postContent != null)
            conn.setDoOutput(true);/*from   ww w  . ja v a  2s .co m*/
    }
}

From source file:com.freedomotic.plugins.devices.ethrly.DevantechEthRly.java

private Document getXMLStatusFile(Board board) {
    //get the xml file from the socket connection
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = null;
    try {/*from w  ww  . j  a  v  a  2s .c  om*/
        dBuilder = dbFactory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        LOG.error(ex.getLocalizedMessage());
    }
    Document doc = null;
    URL url = null;
    try {
        String authString = board.getUsername() + ":" + board.getPassword();
        byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
        String authStringEnc = new String(authEncBytes);
        //Create a URL for the desired  page 
        url = new URL("http://" + board.getIpAddress() + ":" + Integer.toString(board.getPort()) + "/"
                + GET_STATUS_URL);
        URLConnection urlConnection = url.openConnection();
        // if required set the authentication
        if (board.getHttpAuthentication().equalsIgnoreCase("true")) {
            urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
        }
        LOG.info("Devantech Eth-Rly gets relay status from file {}", url);
        doc = dBuilder.parse(urlConnection.getInputStream());
        doc.getDocumentElement().normalize();
    } catch (ConnectException connEx) {
        disconnect();
        this.stop();
        this.setDescription("Connection timed out, no reply from the board at " + url);
    } catch (SAXException ex) {
        disconnect();
        this.stop();
        LOG.error(Freedomotic.getStackTraceInfo(ex));
    } catch (Exception ex) {
        disconnect();
        this.stop();
        setDescription("Unable to connect to " + url);
        LOG.error(Freedomotic.getStackTraceInfo(ex));
    }
    return doc;
}