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));
}
}

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

  1. Server asks us to fall back to SIMPLE auth, but this client is configured to only allow secure connections.

    我是在flume向hdfs 写(sink)数据时遇到的这个错误. Server (是指hdfs) asks us to fall back to SIMPLE auth, but this clien ...

  2. Creating a Simple Web Service and Client with JAX-WS

    Creating a Simple Web Service and Client with JAX-WS 发布服务 package cn.zno.service.impl; import javax. ...

  3. idea中使用scala运行spark出现Exception in thread "main" java.lang.NoClassDefFoundError: scala/collection/GenTraversableOnce$class

    idea中使用scala运行spark出现: Exception in thread "main" java.lang.NoClassDefFoundError: scala/co ...

  4. scala工具sbt的安装和使用;idea如何创建scala项目

    scala的sbt类似于java的maven mac:brew install sbt linux:yum Install sbt 或者下载二机制包 使用sbt需要想mvn一样搭建公司私服,不然,下载 ...

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

    --------------------- 前言 --------------------- 项目关系,希望用Spark GraphX做数据分析及图像展示,但前提是得会spark:spark是基于sc ...

  6. spark安装配置(scala不是必须的,基于java虚拟机,因此scala可以不配,但是开发需要可以配)

    下载 http://spark.apache.org/downloads.html 下载2.3.1 https://blog.csdn.net/qq_15349687/article/details/ ...

  7. Scala实战高手****第4课:零基础彻底实战Scala控制结构及Spark源码解析

    1.环境搭建 基础环境配置 jdk+idea+maven+scala2.11.以上工具安装配置此处不再赘述. 2.源码导入 官网下载spark源码后解压到合适的项目目录下,打开idea,File-&g ...

  8. RestShrap Simple REST and HTTP Client for .NET 了解

    最近做一个项目,需要上传文件到文件服务器, 文件服务器是 内部的webapi形式的接口.经朋友推荐使用restshrap , 例子: //上传文件 var request=new RestClient ...

  9. spark 问题

    问题描述1 使用spark-shell ,sc.textFile("hdfs://test02.com:8020/tmp/w").count 出现如下异常: java.lang.R ...

随机推荐

  1. 用rbenv给整个系统安装ruby(所有用户都可用)

    rbenv非常好用,但有一个小问题,每个用户都需要单独安装和设置版本. 下面安装步骤可以安装在/usr/local/rbenv下,所有用户都可以共享rbenv.   #rbenv安装(/usr/loc ...

  2. ajax练习习题三搜索

    做一个汽车搜索页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  3. Android应用开发学习笔记之菜单

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android中的菜单分为选项菜单(OptionMenu)和上下文菜单(Context Menu).通常使用菜单资源 ...

  4. Docker仓库搭建(Registry + Portus)

    1.更新系统: yum update -y 2. 安装docker-compos yum -y install epel-release    #pip安装包在epel源中   yum -y inst ...

  5. Asterisk的配置详解

    Asterisk的配置文件都在/etc/asterisk目录下,重要的配置文件有: sip.conf                      sip电话基本配置 extensions.conf    ...

  6. poj1860Currency Exchange(bell_fordmoban)

    http://poj.org/problem?id=1860 模板提 #include <iostream> #include<cstdio> #include<cstr ...

  7. VS2008下使用 CMFCPropertyGridCtrl 转载

    http://blog.csdn.net/sunnyloves/article/details/5655575 在DLG中的基本应用 . 首先在Cxxdlg.h文件中加入 public: CMFCPr ...

  8. Mybatis foreach

    批量删除: <delete id= "deleteBatchByXXX" parameterType= "list"> delete from 表名 ...

  9. Tyvj 1085 派对

    这道题和HDU 1016的素数环那道题很相似. 虽然1A了,但写代码的过程中还是丢三落四的. 贴完代码闪人,嘿嘿 //#define LOCAL #include <iostream> # ...

  10. IOS中控制器的重要方法使用

    1.屏幕即将旋转的时候调用(控制器监控屏幕旋转) - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfac ...