Package anvil.util

Source Code of anvil.util.HttpUtils

/*
* $Id: HttpUtils.java,v 1.6 2002/09/16 08:05:07 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.util;

import anvil.core.Any;
import anvil.core.Serialization;
import anvil.core.UnserializationException;
import anvil.script.Context;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.URL;

/**
* HttpUtils
*
* @author: Jaripekka Salminen
*/
public class HttpUtils
{
  /**
   *
   */
  public static Any httpGet(Context context, String url, int returnType)
      throws IOException, UnserializationException
  {

    BufferedReader reader;

    switch (returnType) {

    case StreamUtils.TYPE_STRING:
      reader = new BufferedReader(new InputStreamReader(
        (new URL(url)).openStream()));
      return StreamUtils.readerToAnyString(reader);

    case StreamUtils.TYPE_LINES:
      reader = new BufferedReader(new InputStreamReader(
        (new URL(url)).openStream()));
      return StreamUtils.readerToAnyStringArray(reader);

    case StreamUtils.TYPE_BINARY:
      return StreamUtils.streamToAnyByteArray((new URL(url)).openStream());

    case StreamUtils.TYPE_DATA:
      InputStream inputStream = (new URL(url)).openStream();
      Any data = Serialization.unserialize(context, inputStream);
      inputStream.close();
      return data;

    default:
      return Any.NULL;
    }
  }
}
TOP

Related Classes of anvil.util.HttpUtils

TOP
Copyright © 2018 www.massapi.com. 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.