Global.helma.Http.getUrl (url, opt)
executes a http request

Sourcecode in /home/hannes/helma/modules/helma/Http.js:
171:   this.getUrl = function(url, opt) {
172:           if (typeof url == "string") {
173:               if (!(url = helma.Http.evalUrl(url)))
174:                   throw new Error("'" + url + "' is not a valid URL.");
175:           } else if (!(url instanceof java.net.URL)) {
176:               throw new Error("'" + url + "' is not a valid URL.");
177:           }
178:           
179:           var conn = proxy ? url.openConnection(proxy) : url.openConnection();
180:          conn.setFollowRedirects(followRedirects);
181:          conn.setAllowUserInteraction(false);
182:          conn.setRequestMethod(method);
183:          conn.setRequestProperty("User-Agent", userAgent);
184:  
185:          if (opt) {
186:              if (opt instanceof Date)
187:                  conn.setIfModifiedSince(opt.getTime());
188:              else if ((typeof opt == "string") && (opt.length > 0))
189:                  conn.setRequestProperty("If-None-Match", opt);
190:          }
191:          if (credentials != null) {
192:              conn.setRequestProperty("Authorization", "Basic " + credentials);
193:          }
194:          if (parseFloat(java.lang.System.getProperty("java.specification.version"), 10) >= 1.5) {
195:              conn.setConnectTimeout(timeout.connect);
196:              conn.setReadTimeout(timeout.socket);
197:          } else {
198:              app.debug("WARNING: timeouts can only be set due to Java version < 1.5");
199:          }
200:  
201:          for (var i in headers) {
202:              if (typeof(headers[i]) != "function")
203:                  conn.setRequestProperty(i, headers[i]);
204:          }
205:  
206:          if (content) {
207:              conn.setRequestProperty("Content-Length", content.length);
208:              conn.setDoOutput(true);
209:              var out = new java.io.OutputStreamWriter(conn.getOutputStream());
210:              out.write(content);
211:              out.flush();
212:              out.close();
213:          }
214:  
215:          var result = {
216:              url: conn.getURL(),
217:              location: conn.getHeaderField("location"),
218:              code: conn.getResponseCode(),
219:              message: conn.getResponseMessage(),
220:              length: conn.getContentLength(),
221:              type: conn.getContentType(),
222:              encoding: conn.getContentEncoding(),
223:              lastModified: null,
224:              eTag: null,
225:              content: null
226:          }
227:          var lastmod = conn.getLastModified();
228:          if (lastmod)
229:              result.lastModified = new Date(lastmod);
230:          result.eTag = conn.getHeaderField("ETag");
231:      
232:          if (result.length != 0 && result.code == 200) {
233:              var body = new java.io.ByteArrayOutputStream();
234:              var input = new java.io.BufferedInputStream(conn.getInputStream());
235:              var buf = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024);
236:              var str;
237:              while ((str = input.read(buf)) > -1) {
238:                  body.write(buf, 0, str);
239:              }
240:              input.close();
241:              result.content = result.encoding ? body.toString(result.encoding) : body.toString();
242:              result.length = result.content.length;
243:          }
244:          conn.disconnect();
245:          return result;
246:      }