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. USACO Section 3.1: Contact

    算法简单,写起来遇到些小问题 /* ID: yingzho1 LANG: C++ TASK: contact */ #include <iostream> #include <fst ...

  2. jQuery练习一好友列表变色

    多选 选中变色 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  3. postgreSQLG关闭活动的connection、删除活动的数据库

    First, find the activities that are taken place against the target database, you can query thepg_sta ...

  4. poj 1325 Machine Schedule 二分匹配,可以用最大流来做

    题目大意:机器调度问题,同一个任务可以在A,B两台不同的机器上以不同的模式完成.机器的初始模式是mode_0,但从任何模式改变成另一个模式需要重启机器.求完成所有工作所需最少重启次数. ======= ...

  5. 标准类型内建函数 str()和 repr() (及 `` 运算符) 简单介绍

    内建函数 str() 和 repr() 或反引号运算符(``) 可以方便的以字符串的方式获取对象的内容.类型.数值属性等信息.str()函数得到的字符串可读性好, 而repr()函数得到的字符串通常可 ...

  6. SPOJ 2916 Can you answer these queries V(线段树-分类讨论)

    题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...

  7. 配置spring上下文

    <context-param> <param-name>contextConfigLocation</param-name> <param-value> ...

  8. C#处理文件流的转换

    //----引入必要的命名空间 using System.IO; using System.Drawing.Imaging; //----代码部分----// private byte[] photo ...

  9. css默认被后代inherite的属性列表

    css中的属性大部分都可以被继承,但是也有众多不能被继承,比如display, position,,left,right,top,bottom,float,width,border-color,bor ...

  10. UVa (BFS) The Monocycle

    题目不光要求要到达终点而且要求所走的步数为5的倍数,每个时刻有三个选择,前进,左转弯,右转弯. 所以在vis数组中新增加两个维度即可,vis[x][y][dir][color]表示在(x, y)格子方 ...