加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用Scalas“fromURL”的结果会抛出异常

发布时间:2020-12-16 18:05:34 所属栏目:安全 来源:网络整理
导读:我正在尝试使用 Scala的scala.io.Source对象获取一些网页.获取迭代器工作正常但我不能做任何事情而不会得到异常: scala scala.io.Source.fromURL("http://google.com")res0: scala.io.BufferedSource = non-empty iteratorscala scala.io.Source.fromURL("h
我正在尝试使用 Scala的scala.io.Source对象获取一些网页.获取迭代器工作正常但我不能做任何事情而不会得到异常:

scala> scala.io.Source.fromURL("http://google.com")
res0: scala.io.BufferedSource = non-empty iterator

scala> scala.io.Source.fromURL("http://google.com").length
java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:277)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:338)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.read(BufferedReader.java:175)
at scala.io.BufferedSource$$anonfun$iter$1$$anonfun$apply$mcI$sp$1.apply$mcI$sp(BufferedSource.scala:38)
at scala.io.Codec.wrap(Codec.scala:64)
at scala.io.BufferedSource$$anonfun$iter$1.apply$mcI$sp(BufferedSource.scala:38)
at scala.io.BufferedSource$$anonfun$iter$1.apply(BufferedSource.scala:38)
at scala.io.BufferedSource$$anonfun$iter$1.apply(BufferedSource.scala:38)
at scala.collection.Iterator$$anon$14.next(Iterator.scala:150)
at scala.collection.Iterator$$anon$25.hasNext(Iterator.scala:562)
at scala.collection.Iterator$$anon$19.hasNext(Iterator.scala:400)
at scala.io.Source.hasNext(Source.scala:238)
at scala.collection.Iterator$class.foreach(Iterator.scala:772)
at scala.io.Source.foreach(Source.scala:181)
at scala.collection.TraversableOnce$class.size(TraversableOnce.scala:104)
at scala.io.Source.size(Source.scala:181)
at scala.collection.Iterator$class.length(Iterator.scala:1071)
at scala.io.Source.length(Source.scala:181)
at .<init>(<console>:8)
at .<clinit>(<console>)
at .<init>(<console>:11)
at .<clinit>(<console>)
at $print(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)
at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920)
at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43)
at scala.tools.nsc.io.package$$anon$2.run(package.scala:25)
at java.lang.Thread.run(Thread.java:745)

因此,您可以看到获取缓冲区的工作原理,我可以用它做点什么

scala> scala.io.Source.fromURL("http://google.com").next
res7: Char = <

但似乎我无法重复它.

我正在使用scala v 2.9.2,但问题也在2.11.2中重复出现.我还在跑步

java version "1.7.0_75"
OpenJDK Runtime Environment (IcedTea 2.5.4) (7u75-2.5.4-2)
OpenJDK 64-Bit Server VM (build 24.75-b04,mixed mode)

任何有助于此工作的帮助将不胜感激

解决方法

你有一个编码问题.

解释响应所需的编码是latin1,也称为ISO-8859-1.

使用Source.fromURL(“url”)(“encoding”)来解决您的问题.

Source.fromURL("http://google.com")("ISO-8859-1").mkString
 res4: String =
 <!doctype html><html itemscop

一点背景:当http请求中没有给出编码时,标准行为是重新调整在Latin-1中编码的所有内容.
有关深度信息,请参阅http://www.ietf.org/rfc/rfc2045.txt

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读