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:roommateapp.info.net.FileDownloader.java

/**
 * Parallel execution// w w w .  j  av a  2s.  c  o m
 */
@SuppressLint("UseValueOf")
@Override
protected Object doInBackground(Object... params) {

    if (!this.isHolidayFile) {

        /* Progressbar einblenden */
        this.ac.runOnUiThread(new Runnable() {
            public void run() {
                toggleProgressbar();
            }
        });
    }

    boolean success = true;
    String eingabe = (String) params[0];

    if (eingabe != null) {

        try {
            URL url = new URL(eingabe);

            // Get filename
            String fileName = eingabe;
            StringTokenizer tokenizer = new StringTokenizer(fileName, "/", false);
            int tokens = tokenizer.countTokens();
            for (int i = 0; i < tokens; i++) {
                fileName = tokenizer.nextToken();
            }

            // Create file
            this.downloadedFile = new File(this.roommateDirectory, fileName);

            // Download and write file
            try {
                // Password and username if it's HTACCESS
                String authData = new String(Base64.encode((username + ":" + pw).getBytes(), Base64.NO_WRAP));

                URLConnection urlcon = url.openConnection();

                // Authorisation if it's a userfile
                if (eingabe.startsWith(RoommateConfig.URL)) {
                    urlcon.setRequestProperty("Authorization", "Basic " + authData);
                    urlcon.setDoInput(true);
                }
                InputStream inputstream = urlcon.getInputStream();
                ByteArrayBuffer baf = new ByteArrayBuffer(50);

                int current = 0;

                while ((current = inputstream.read()) != -1) {
                    baf.append((byte) current);
                }

                FileOutputStream fos = new FileOutputStream(this.downloadedFile);
                fos.write(baf.toByteArray());
                fos.close();

            } catch (IOException e) {
                success = false;
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            success = false;
            e.printStackTrace();
        }
    } else {
        success = false;
    }
    return new Boolean(success);
}

From source file:org.jab.docsearch.utils.NetUtils.java

/**
 * Get SpiderUrl status/*from   ww w  .j a  v  a2  s .c o m*/
 *
 * Content type, content length, last modified and md5 will be set in SpiderUrl if url is changed
 *
 * @param spiderUrl  URL to check
 * @param file       URL content downloads to to this file
 * @return           -1 if link is broken, 0 if the file is unchanged or 1 if the file
 *                   is different... part of caching algoritm.
 */
public int getURLStatus(final SpiderUrl spiderUrl, final String file) {
    // -1 means broken link
    //  0 means same file
    //  1 means changed

    int status;

    try {
        // attempt to obtain status from date
        URL url = new URL(spiderUrl.getUrl());
        URLConnection conn = url.openConnection();

        // set connection parameter
        conn.setDoInput(true);
        conn.setDoOutput(false);
        conn.setUseCaches(false);
        conn.setRequestProperty("User-Agent", USER_AGENT);

        // connect
        conn.connect();

        // content type
        spiderUrl.setContentType(getContentType(conn));

        // check date
        long spiDate = spiderUrl.getLastModified();
        long urlDate = conn.getLastModified();

        // same if date is equal and not zero
        if (spiDate == urlDate && urlDate != 0) {
            // the file is not changed
            status = 0;
        } else {
            // download the URL and compare hashes
            boolean downloaded = downloadURLToFile(conn, file);
            if (downloaded) {
                // download ok

                // compare file hashes
                String fileHash = FileUtils.getMD5Sum(file);

                if (fileHash.equals(spiderUrl.getMd5())) {
                    // same
                    status = 0;
                } else {
                    // changed
                    status = 1;

                    // set changed values
                    spiderUrl.setSize(FileUtils.getFileSize(file));
                    spiderUrl.setLastModified(urlDate);
                    spiderUrl.setMd5(fileHash);
                }
            } else {
                // download failed

                // broken link
                status = -1;
            }
        }
    } catch (IOException ioe) {
        logger.error("getURLStatus() failed for URL='" + spiderUrl.getUrl() + "'", ioe);
        status = -1;
    }

    return status;
}

From source file:org.t3.metamediamanager.TheMovieDBProvider.java

private HashMap<String, String[]> getAllImages(String id, String language) {
    HashMap<String, String[]> res = new HashMap<String, String[]>(); //Final hashmap to be returned

    String query = String.format("http://api.themoviedb.org/3/movie/" + id + "/images?api_key=" + apiKey);

    try {//from  www.  jav a2 s. c  om
        URLConnection connection = new URL(query).openConnection();
        connection.setRequestProperty("Accept-Charset", charset);
        InputStream response = connection.getInputStream();
        String rep = convertStreamToString(response);

        JSONObject json = convertStringToJSON(rep);

        String[] types = { "backdrops", "posters" };

        //We only select the 10 most voted images. We use this class to do the final sort
        class ImageInfo implements Comparable<ImageInfo> {
            String name;
            double priority;

            @Override
            public int compareTo(ImageInfo o) {
                return (int) (priority - o.priority);
            }
        }

        //Use of a temp hashmap with a list to be sorted
        HashMap<String, List<ImageInfo>> tmpRes = new HashMap<String, List<ImageInfo>>();

        //For each type of image (poster or backdrop)
        for (String type : types) {
            if (json.has(type)) {
                JSONArray typeJson = json.getJSONArray(type);

                List<ImageInfo> imgList = new ArrayList<ImageInfo>();

                tmpRes.put(type, imgList);

                int nbImages = typeJson.length();
                for (int i = 0; i < nbImages; i++) //For every image
                {
                    JSONObject imgObject = typeJson.getJSONObject(i);

                    if (imgObject.has("file_path") && imgObject.has("vote_average")
                            && imgObject.has("iso_639_1")) {
                        if (imgObject.isNull("iso_639_1")
                                || imgObject.getString("iso_639_1").equals(language)) {
                            ImageInfo ii = new ImageInfo();
                            ii.name = imgObject.getString("file_path");
                            ii.priority = imgObject.getDouble("vote_average");
                            imgList.add(ii);
                        }

                    }
                }

                //We sort by priority/vote
                Collections.sort(imgList, Collections.reverseOrder());

                int nbImagesFinal = (imgList.size() > 10) ? 10 : imgList.size();
                res.put(type, new String[nbImagesFinal]);
                for (int i = 0; i < nbImagesFinal; i++) {
                    res.get(type)[i] = createPicture(imgList.get(i).name); //Download the image, and keep the filename of the temp file
                }
            }

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

    return res;
}

From source file:screenieup.ImgurUpload.java

/**
 * Connect to image host./* w  w w .  ja  va  2 s . c om*/
 * @return the connection
 */
private URLConnection connect() {
    URLConnection conn = null;
    try {
        System.out.println("Connecting to imgur...");
        // opens connection and sends data
        URL url = new URL(IMGUR_POST_URI);
        conn = url.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestProperty("Authorization", "Client-ID " + IMGUR_API_KEY);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    } catch (MalformedURLException ex) {
        Logger.getLogger(ImgurUpload.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ImgurUpload.class.getName()).log(Level.SEVERE, null, ex);
    }
    return conn;
}

From source file:msearch.io.MSFilmlisteLesen.java

private boolean filmlisteDownload(String uurl, File nachDatei) {
    boolean ret = false;
    try {/*ww w . j a  v  a 2s .co  m*/
        URLConnection conn = new URL(uurl).openConnection();
        conn.setConnectTimeout(TIMEOUT);
        conn.setReadTimeout(TIMEOUT);
        conn.setRequestProperty("User-Agent", MSConfig.getUserAgent());
        BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
        FileOutputStream fOut;
        byte[] buffer = new byte[1024];
        int n = 0;
        int count = 0;
        int countMax;
        if (uurl.endsWith(MSConst.FORMAT_XZ) || uurl.endsWith(MSConst.FORMAT_BZ2)
                || uurl.endsWith(MSConst.FORMAT_ZIP)) {
            countMax = 44;
        } else {
            countMax = 250;
        }
        fOut = new FileOutputStream(nachDatei);
        this.notifyProgress(uurl);
        while (!MSConfig.getStop() && (n = in.read(buffer)) != -1) {
            fOut.write(buffer, 0, n);
            ++count;
            if (count > countMax) {
                this.notifyProgress(uurl);
                count = 0;
            }
        }
        if (MSConfig.getStop()) {
            ret = false;
        } else {
            ret = true;
        }
        try {
            fOut.close();
            in.close();
        } catch (Exception e) {
        }
    } catch (Exception ex) {
        MSLog.fehlerMeldung(952163678, MSLog.FEHLER_ART_PROG, "MSearchIoXmlFilmlisteLesen.filmlisteDownload",
                ex);
    }
    return ret;
}

From source file:com.apache.ivy.BasicURLHandler.java

public void download(URL src, File dest, CopyProgressListener l) throws IOException {
    // Install the IvyAuthenticator
    if ("http".equals(src.getProtocol()) || "https".equals(src.getProtocol())) {
        IvyAuthenticator.install();//from  w  w w .java2 s. c  om
    }

    URLConnection srcConn = null;
    try {
        src = normalizeToURL(src);
        srcConn = src.openConnection();
        srcConn.setRequestProperty("User-Agent", "Apache Ivy/1.0");// + Ivy.getIvyVersion());
        srcConn.setRequestProperty("Accept-Encoding", "gzip,deflate");
        if (srcConn instanceof HttpURLConnection) {
            HttpURLConnection httpCon = (HttpURLConnection) srcConn;
            if (!checkStatusCode(src, httpCon)) {
                throw new IOException("The HTTP response code for " + src + " did not indicate a success."
                        + " See log for more detail.");
            }
        }

        // do the download
        InputStream inStream = getDecodingInputStream(srcConn.getContentEncoding(), srcConn.getInputStream());
        FileUtil.copy(inStream, dest, l);

        // check content length only if content was not encoded
        if (srcConn.getContentEncoding() == null) {
            int contentLength = srcConn.getContentLength();
            if (contentLength != -1 && dest.length() != contentLength) {
                dest.delete();
                throw new IOException("Downloaded file size doesn't match expected Content Length for " + src
                        + ". Please retry.");
            }
        }

        // update modification date
        long lastModified = srcConn.getLastModified();
        if (lastModified > 0) {
            dest.setLastModified(lastModified);
        }
    } finally {
        disconnect(srcConn);
    }
}

From source file:org.spoutcraft.launcher.api.util.Download.java

@SuppressWarnings("unused")
public void run() {
    ReadableByteChannel rbc = null;
    FileOutputStream fos = null;//www .j a  va2 s .  c o  m
    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) {
        result = Result.PERMISSION_DENIED;
    } catch (DownloadException e) {
        result = Result.FAILURE;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(rbc);
    }
}

From source file:com.apache.ivy.BasicURLHandler.java

public InputStream openStreamPost(URL url, ArrayList<NameValuePair> postData) throws IOException {
    // Install the IvyAuthenticator
    if ("http".equals(url.getProtocol()) || "https".equals(url.getProtocol())) {
        IvyAuthenticator.install();//w  w  w .j ava  2 s .com
    }

    URLConnection conn = null;
    try {
        url = normalizeToURL(url);
        conn = url.openConnection();
        conn.setRequestProperty("User-Agent", "Apache Ivy/1.0");// + Ivy.getIvyVersion());
        conn.setRequestProperty("Accept-Encoding", "gzip,deflate");

        if (conn instanceof HttpURLConnection) {
            HttpURLConnection httpCon = (HttpURLConnection) conn;

            httpCon.setRequestMethod("POST");
            httpCon.setDoInput(true);
            httpCon.setDoOutput(true);

            byte[] postDataBytes = getQuery(postData).getBytes(Charset.forName("UTF-8"));
            String contentLength = String.valueOf(postDataBytes.length);
            httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpCon.setRequestProperty("Content-Length", contentLength);
            httpCon.setDoOutput(true);

            httpCon.connect();

            httpCon.getOutputStream().write(postDataBytes);

            conn.getOutputStream().flush();

            if (!checkStatusCode(url, httpCon)) {
                throw new IOException("The HTTP response code for " + url + " did not indicate a success."
                        + " See log for more detail.");
            }

        }

        // read the output
        InputStream inStream = getDecodingInputStream(conn.getContentEncoding(), conn.getInputStream());
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[BUFFER_SIZE];
        int len;
        while ((len = inStream.read(buffer)) > 0) {
            outStream.write(buffer, 0, len);
        }

        return new ByteArrayInputStream(outStream.toByteArray());
    } finally {
        disconnect(conn);
    }
}

From source file:com.determinato.feeddroid.parser.RssParser.java

/**
 * Persists RSS item to the database.// w w  w  .j  a  va2 s .co m
 * @param id item ID
 * @param folderId ID of containing folder
 * @param rssurl URL of RSS feed
 * @return long containing ID of inserted item
 * @throws Exception
 */
public long syncDb(long id, long folderId, String rssurl) throws Exception {
    mId = id;
    mFolderId = folderId;
    mRssUrl = rssurl;

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser parser = factory.newSAXParser();
    XMLReader reader = parser.getXMLReader();

    reader.setContentHandler(this);
    reader.setErrorHandler(this);

    URL url = new URL(mRssUrl);

    URLConnection c = url.openConnection();
    // TODO: Is this a known user agent, or do I need to come up with my own?
    c.setRequestProperty("User-Agent", "Android/m3-rc37a");

    try {
        BufferedReader bufReader = new BufferedReader(new InputStreamReader(c.getInputStream()), 65535);
        reader.parse(new InputSource(bufReader));
    } catch (NullPointerException e) {
        Log.e(TAG, Log.getStackTraceString(e));
        Log.e(TAG, "Failed to load URL" + url.toString());
    }

    return mId;
}

From source file:net.sourceforge.atunes.kernel.modules.network.NetworkHandler.java

/**
 * Returns a HttpURLConnection specified by a given URL
 * //from  w  ww.java 2 s .  c o  m
 * @param urlString
 *            A URL as String
 * 
 * @return A HttpURLConnection
 * 
 * @throws IOException
 *             If an IO exception occurs
 */
@Override
public URLConnection getConnection(final String urlString) throws IOException {
    Logger.debug("Opening Connection With: ", urlString);

    URL url = new URL(urlString);

    URLConnection connection;

    ExtendedProxy proxy = ExtendedProxy.getProxy(stateCore.getProxy());
    if (proxy == null) {
        connection = url.openConnection();
    } else {
        connection = url.openConnection(proxy);
    }
    connection.setRequestProperty("User-agent", Constants.APP_NAME);
    connection.setConnectTimeout(connectTimeoutInSeconds * 1000);
    connection.setReadTimeout(readTimeoutInSeconds * 1000);
    return connection;
}