Class HttpFile

java.lang.Object
de.elo.ix.jscript.FileBase
de.elo.ix.jscript.HttpFile

public class HttpFile extends FileBase
This class provides convenient methods to send HTTP requests.

Download from an URL

This examples downloads a file from an URL. The file is created in the temporary directory with a generated file name.

  
 function download() {
   var file = new Packages.de.elo.ix.jscript.HttpFile(url);
   var fileName = file.download();
   log.info("fileName=" + fileName);
 } 
 
 

Upload to an URL

The upload function can be used to send a file via a HTTP POST request to a web application.

  
 function upload() {
    var file = new Packages.de.elo.ix.jscript.HttpFile(url);
    var ret = file.upload(fileName);
    log.info("ret=" + ret);
 } 
 
 

Read and Write Functions

The read functions can download contents into a byte array or into a String. The write functions can post the contents of a byte array or a String to the server.

  
 function receiveText() {
    var url = "http://server:port/...";
    var file = new Packages.de.elo.ix.jscript.HttpFile(url);
    var content = file.readAllText();
    log.info("content=" + content);
 }      
 function sendText() {
    var url = "http://server:port/...";
    var file = new Packages.de.elo.ix.jscript.HttpFile(url, "text/plain; charset=UTF-8");
    var content = "abcde";
    var response = file.writeAllText(content);
    log.info("response=" + response);
 }      
 function receiveBytes() {
    var url = "http://server:port/...";
    var file = new Packages.de.elo.ix.jscript.HttpFile(url);
    var content = file.readAllBytes();
    log.info("content=" + content);
 }      
 function sendBytes() {
    var url = "http://server:port/...";
    var file = new Packages.de.elo.ix.jscript.HttpFile(url);
    var content = ...;
    var response = file.writeAllBytes(content);
    log.info("response=" + response);
 }      
 
 
  • Constructor Details

  • Method Details

    • upload

      public Object upload(String fileName) throws IOException
      Upload a file to the resource.
      Parameters:
      fileName - File name
      Returns:
      Server response
      Throws:
      IOException - An exception is thrown, if the server response is not HTTP-OK (200).
    • download

      public String download(String fileName) throws IOException
      Download a file from the resource.
      Parameters:
      fileName - Optional. File name. If null, a temporary file is created.
      Throws:
      IOException - An exception is thrown, if the server response is not HTTP-OK (200).
    • download

      public String download() throws IOException
      Download the resource into a temporary file.
      Returns:
      Name of the temporary file.
      Throws:
      IOException - An exception is thrown, if the server response is not HTTP-OK (200).
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getContentType

      public String getContentType()
    • setContentType

      public void setContentType(String contentType)
    • getUrl

      public String getUrl()
    • setUrl

      public void setUrl(String url)