Package java.net

Examples of java.net.URLConnection


        aURL = new URL(feedUrl);
      } catch (java.net.MalformedURLException e) {
        logger.error("Could not create URL for " + feedUrl);
      }

      URLConnection conn = null;
      try {
        conn = aURL.openConnection();

        if (conn instanceof HttpURLConnection) {

          HttpURLConnection httpConn = (HttpURLConnection) conn;

          httpConn.setInstanceFollowRedirects(true);
          //   Hack for User-Agent : problem for
          // http://www.diveintomark.org/xml/rss.xml
          HttpHeaderUtils.setUserAgent(httpConn, "Informa Java API");
          HttpHeaderUtils.setETagValue(httpConn, this.httpHeaders.getETag());
          HttpHeaderUtils.setIfModifiedSince(httpConn, this.httpHeaders
              .getIfModifiedSince());
          httpConn.connect();
          if (httpConn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {

            logger.info("cond. GET for feed at url " + feedUrl + ": no change");
            this.feed.setLastUpdated(new Date());
            // TODO : add a property in FeedIF interface for lastGet ?
            this.lastUpdate = System.currentTimeMillis();
            return;
          }
          logger.info("cond. GET for feed at url " + feedUrl + ": changed");
          logger.debug("feed at url " + feedUrl + " new values : ETag"
              + HttpHeaderUtils.getETagValue(httpConn) + " if-modified :"
              + HttpHeaderUtils.getLastModified(httpConn));

          this.httpHeaders.setETag(HttpHeaderUtils.getETagValue(httpConn));
          this.httpHeaders.setIfModifiedSince(HttpHeaderUtils
              .getLastModified(httpConn));
        }

      } catch (java.lang.ClassCastException e) {
        logger.warn("problem cast to HttpURLConnection (reading from a file?) "
            + feedUrl, e);
      }

      ChannelIF channel = null;
      if (conn == null) {
        channel = FeedParser.parse(getChannelBuilder(), feedUrl);
      } else {
        channel = FeedParser.parse(getChannelBuilder(), conn.getInputStream());
      }

      this.feed.setChannel(channel);
      this.feed.setLastUpdated(new Date());
      this.lastUpdate = System.currentTimeMillis();
View Full Code Here


   * http://username:password@www.domain.org/index.html
   */
  public static String readString(String url, int timeout) {
    Reader reader = null;
    try {
      URLConnection uc = new URL(url).openConnection();
      if (uc instanceof HttpURLConnection) {
        HttpURLConnection httpConnection = (HttpURLConnection) uc;
        httpConnection.setConnectTimeout(timeout * 1000);
        httpConnection.setReadTimeout(timeout * 1000);
      }
      Matcher matcher = Pattern.compile("://(\\w+:\\w+)@").matcher(url);
      if (matcher.find()) {
        String auth = matcher.group(1);
        String encoding = Base64.encodeBytes(auth.getBytes());
        uc.setRequestProperty("Authorization", "Basic " + encoding);
      }
      String charset = (uc.getContentType() != null && uc
          .getContentType().contains("charset=")) ? uc
          .getContentType().split("charset=")[1] : "utf-8";
      reader = new BufferedReader(new InputStreamReader(
          uc.getInputStream(), charset));
      StringBuilder sb = new StringBuilder();
      for (int chr; (chr = reader.read()) != -1;) {
        sb.append((char) chr);
      }
      return sb.toString();
View Full Code Here

   * Returns true if given url can be connected via HTTP within given timeout
   * (specified in seconds). Otherwise the url might be broken.
   */
  public static boolean isConnectable(String url, int timeout) {
    try {
      URLConnection connection = new URL(url).openConnection();
      if (connection instanceof HttpURLConnection) {
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        httpConnection.setConnectTimeout(timeout * 1000);
        httpConnection.setReadTimeout(timeout * 1000);
        httpConnection.connect();
 
View Full Code Here

   * Returns the status code for connecting given url with given timeout.
   * Returns 0 if an IOException occurs.
   */
  public static int getStatus(String url, int timeout) {
    try {
      URLConnection connection = new URL(url).openConnection();
      if (connection instanceof HttpURLConnection) {
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        httpConnection.setConnectTimeout(timeout * 1000);
        httpConnection.setReadTimeout(timeout * 1000);
        httpConnection.connect();
 
View Full Code Here

   */
  private InputStream getInputStream(URL location) throws IOException {
   
    if (location == null) return null;
   
    URLConnection connection = location.openConnection();
    if (userAgent != null && connection instanceof HttpURLConnection) {
      HttpHeaderUtils.setUserAgent((HttpURLConnection)connection, userAgent);
    }
    return new BufferedInputStream(connection.getInputStream());
  }
View Full Code Here

          if (jarURL.getProtocol().equals("file")) {
            // Return the last modified time of the underlying file - saves some opening and closing
            return new File(jarURL.getFile()).lastModified();
          } else {
            // Use the URL mechanism
            URLConnection jarConn=null;
            try {
              jarConn=jarURL.openConnection();
              return jarConn.getLastModified();
            } catch (IOException e) {
              return -1;
            } finally {
              try {
                if (jarConn!=null) jarConn.getInputStream().close();
              } catch (IOException e) { }
            }
          }
        } else {
          long lastModified = conn.getLastModified();
View Full Code Here

     * @param redirectCount The number of previous redirects. We will follow a maximum of 5 redirects.
     * @return the final URL, or the initial one in case there is no redirect.
     * @throws IOException for connection error
     */
    private URL resolveRedirects(URL url, int redirectCount) throws IOException {
        URLConnection uc = url.openConnection();
        if (uc instanceof HttpURLConnection) {
            HttpURLConnection huc = (HttpURLConnection)uc;
            huc.setInstanceFollowRedirects(false);
            huc.connect();
            int responseCode = huc.getResponseCode();
View Full Code Here

        else return url;
    }

    public static String loadPage(URL url) throws IOException {
        Reader in = null;
        URLConnection uc;
        HttpURLConnection huc = null;
        try {
            uc = url.openConnection();
            if (uc instanceof HttpURLConnection) {
                huc = (HttpURLConnection)uc;
View Full Code Here

    private BibtexDatabase parseBibtexDatabase(List<String> id, boolean abs) throws IOException {
      if (id.isEmpty())
        return null;
        URL url;
        URLConnection conn;
        try {
            url = new URL(importUrl);
            conn = url.openConnection();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        conn.setRequestProperty("Referer", searchUrl);
        PrintWriter out = new PrintWriter(
                conn.getOutputStream());

    String recordIds = "";
    Iterator<String> iter = id.iterator();
    while (iter.hasNext()) {
        recordIds += iter.next() + " ";
    }
    recordIds = recordIds.trim();
    String citation = abs ? "citation-abstract" : "citation-only";
   
    String content = "recordIds=" + recordIds.replaceAll(" ", "%20") + "&fromPageName=&citations-format=" + citation + "&download-format=download-bibtex";
    System.out.println(content);
        out.write(content);
        out.flush();
        out.close();

        BufferedReader bufr = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuffer sb = new StringBuffer();
        char[] buffer = new char[256];
        while(true) {
            int bytesRead = bufr.read(buffer);
            if(bytesRead == -1) break;
View Full Code Here

    OutputStream out = selectOutputStream(request, response);

    try {
      for (URL resource : resources) {
        URLConnection resourceConn = resource.openConnection();
        InputStream in = resourceConn.getInputStream();
        try {
          byte[] buffer = new byte[1024];
          while (in.available() > 0) {
            int len = in.read(buffer);
            out.write(buffer, 0, len);
          }
        } finally {
          in.close();
          try {
            resourceConn.getOutputStream().close();
          } catch (Throwable t) { //NOPMD
            /*ignore, just trying to free resources*/
          }
        }
      }
 
View Full Code Here

TOP

Related Classes of java.net.URLConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.