Java URL Connection getDataFromServer(URL url)

Here you can find the source of getDataFromServer(URL url)

Description

get Data From Server

License

Open Source License

Parameter

Parameter Description
url a parameter

Exception

Parameter Description
IOException an exception

Return

Returns data from the server with a given url

Declaration

public static String getDataFromServer(URL url) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006, 2007 Bug Labs, Inc..
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.buglabs.net/legal/epl_license.html
 *******************************************************************************/

import java.io.BufferedReader;
import java.io.IOException;

import java.io.InputStreamReader;

import java.net.URL;

public class Main {
    /**/*from  w  w w  .ja  v  a2  s. c o  m*/
     * @param url
     * @return Returns data from the server with a given url
     * @throws IOException
     */
    public static String getDataFromServer(URL url) throws IOException {
        java.net.URLConnection conn = url.openConnection();
        StringBuffer sb = new StringBuffer();
        conn.setDoInput(true);
        conn.setDoOutput(false);

        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            if (line.trim().length() > 0) {
                sb.append(line);
            }
        }

        rd.close();

        return sb.toString();
    }
}

Related

  1. getChildren(URL url)
  2. getConnection(URL url)
  3. getConnectionResponseHeaders(URLConnection c)
  4. getContainerUrl(URL url, String resourceInThatDir)
  5. getCookies(URLConnection conn, Map> store)
  6. getDefaultUrlConnection(URL url)
  7. getFeedReader(URL feedUrl)
  8. getFromUrl(String url)
  9. getGlobalAddress(String url)