scala: How to write a simple HTTP GET request client in Scala (with a timeout)

Scala CookBook: http://scalacookbook.com/

@throws(classOf[java.io.IOException])
@throws(classOf[java.net.SocketTimeoutException])
def get(url: String,
        connectTimeout:Int =5000,
        readTimeout:Int =5000,
        requestMethod: String = "GET") = {
    import java.net.{URL, HttpURLConnection}
    val connection = (new URL(url)).openConnection.asInstanceOf[HttpURLConnection]
    connection.setConnectTimeout(connectTimeout)
    connection.setReadTimeout(readTimeout)
    connection.setRequestMethod(requestMethod)
    val inputStream = connection.getInputStream
    val content = io.Source.fromInputStream(inputStream).mkString
    if (inputStream != null) inputStream.close
    content
}

if want to set the request property, add:for (header <- headers) {   val headerDetails = header.split(":")   if (headerDetails.size<=2) {      connection.setRequestProperty(headerDetails(0), headerDetails(1));   }}
时间: 2024-10-07 04:28:33

scala: How to write a simple HTTP GET request client in Scala (with a timeout)的相关文章

Scala &amp; IntelliJ IDEA环境搭建升级版:在JAVA中调用Scala的helloworld

--------------------- 前言 --------------------- 项目关系,希望用Spark GraphX做数据分析及图像展示,但前提是得会spark:spark是基于scala的,scala是基于JAVA的--所以,要想用Spark GraphX,技术堆栈大体上应该是这样的:Java -> Scala -> Spark -> Spark GraphX. JAVA都这么多年了--就不说了,我们从学习Scala开始. -------------------- 说

Simple http post request demo

1 // httppostdemo.h 2 #ifndef HTTPPOSTDEMO_H 3 #define HTTPPOSTDEMO_H 4 5 #include <QMainWindow> 6 #include <QNetworkAccessManager> 7 #include <QNetworkReply> 8 9 namespace Ui { 10 class HttpPostDemo; 11 } 12 13 class HttpPostDemo : publ

Spark standalone集群安装

本文不会搞什么Yarn混搭Spark,只想建立一个纯粹的Spark环境,太多层东西搅和在一起,不靠谱. 创建spark服务运行帐号 # useradd smile smile帐号就是spark服务的运行帐号. 下载安装包并测试 在root帐号下,下载最新安装包,注意不是source,而是bin安装包,支持hadoop2.6以后的 wget http://mirrors.cnnic.cn/apache/spark/spark-1.5.1/spark-1.5.1-bin-hadoop2.6.tgz

spark取得lzo压缩文件报错 java.lang.ClassNotFoundException

恩,这个问题,反正是我从来没有注意的问题,但今天还是写出来吧 配置信息 hadoop core-site.xml配置 <property>    <name>io.compression.codecs</name>         <value>org.apache.hadoop.io.compress.GzipCodec,org.apache.hadoop.io.compress.DefaultCodec,com.hadoop.compression.lz

spark-shell启动spark报错

前言 离线安装好CDH.Coudera Manager之后,通过Coudera Manager安装所有自带的应用,包括hdfs.hive.yarn.spark.hbase等应用,过程很是波折,此处就不抱怨了,直接进入主题. 描述 在安装有spark的节点上,通过spark-shell启动spark,满怀期待的启动spark,but,来了个晴天霹雳,报错了,报错了!错误信息如下: 18/06/11 17:40:27 ERROR spark.SparkContext: Error initializ

How do I create a List in Scala?

Scala List class FAQ: How do I create a List in Scala? You can create a Scala List in several different ways, including these approaches: Lisp style Java style Using the List class rangemethod Using the List class fillmethod Using the List classtabul

Can you share some Scala List class examples?

Scala List FAQ: Can you share some Scala List class examples? The Scala List class may be the most commonly used data structure in Scala applications. Therefore, it's very helpful to know how create lists, merge lists, select items from lists, operat

Brief introduction to Scala and Breeze for statistical computing

Brief introduction to Scala and Breeze for statistical computing 时间 2013-12-31 03:17:19  Darren Wilkinson's research blog 原文  http://darrenjw.wordpress.com/2013/12/30/brief-introduction-to-scala-and-breeze-for-statistical-computing/ 主题 Scala Introduc

【转】Scala基础知识

原文地址.续 课程内容: 关于这节课 表达式 值 函数 类 继承 特质 类型 apply方法 单例对象 函数即对象 包 模式匹配 样本类 try-catch-finally 关于这节课 最初的几个星期将涵盖基本语法和概念,然后我们将通过更多的练习展开这些内容. 有一些例子是以解释器交互的形式给出的,另一些则是以源文件的形式给出的. 安装一个解释器,可以使探索问题空间变得更容易. 为什么选择 Scala? ·表达能力     ·函数是一等公民     ·闭包 ·简洁     ·类型推断     ·