excel - 带有空主体的 HTTP GET 响应,在 IE 中下载了 excel 文档

标签 excel scala http get httpurlconnection

我想弄清楚 this 上发生了什么下载 Excel 报告时出现的页面。它仅适用于 Internet Explorer,当我尝试使用 ScalaJ 在 Scala 中下载报告时,我的响应主体的输入流为空。有人可以帮助我了解这里发生了什么吗?

明文请求:

GET /oca_ReportViewer.aspx?ReportName=District_and_Statutory_County_Court/DSC_Civil_Family_Activity_Detail_N.rpt&ddlFromMonth=9&ddlFromYear=2010&txtFromMonthField=@FromMonth&txtFromYearField=@FromYear&ddlToMonth=10&ddlToYear=2010&txtToMonthField=@ToMonth&txtToYearField=@ToYear&ddlCountyPostBack=0&txtCountyPostBackField=@CountyID&chkAggregateMonthlyReport=0&export=1625 HTTP/1.1
Accept: text/html, application/xhtml+xml, image/jxr, */*
Referer: http://card.txcourts.gov/ReportSelection.aspx
Accept-Language: en-US
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: card.txcourts.gov
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: ASP.NET_SessionId=j0tgci45qj3t1uqygrvkqz55

到目前为止的代码:

import java.io.{BufferedOutputStream, FileOutputStream, FileWriter, InputStream}
import scalaj.http._

object Downloader extends App {

    val url = "http://card.txcourts.gov/oca_ReportViewer.aspx"
    val keys: Map[String, String] = Map (
        ("ReportName", "District_and_Statutory_County_Court/DSC_Civil_Family_Activity_Detail_N.rpt"),
        ("ddlFromMonth", "9"),
        ("ddlFromYear", "2010"),
        ("txtFromMonthField", "@FromMonth"),
        ("txtFromYearField", "@FromYear"),
        ("ddlToMonth", "10"),
        ("ddlToYear", "2010"),
        ("txtToMonthField", "@ToMonth"),
        ("txtToYearField", "@ToYear"),
        ("ddlCountyPostBack", "0"),
        ("txtCountyPostBackField", "@CountyID"),
        ("chkAggregateMonthlyReport", "0"),
        ("export", "1625")
    )

    //println(keys)

    val heads: Map[String, String] = Map (
        ("Accept", "text/html, application/xhtml+xml, image/jxr, */*"),
        ("Accept-Encoding", "gzip, deflate"),
        ("Accept-Language", "en-US"),
        ("Cache-Control", "no-cache"),
        ("Connection", "Keep-Alive"),
        ("Cookie", "ASP.NET_SessionId=j0tgci45qj3t1uqygrvkqz55"),
        ("Host", "card.txcourts.gov"),
        ("Referer", "http://card.txcourts.gov/ReportSelection.aspx"),
        ("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko")
    )

    def parse(status: Int, headers: Map[String, IndexedSeq[String]], inputStream: InputStream): Unit = {
        val output = new BufferedOutputStream(new FileOutputStream("test.xls"))
        val bytes = new Array[Byte](1024) //1024 bytes - Buffer size
        //println(status)
        //println(headers)
        Stream
            .continually (inputStream.read(bytes))
            .takeWhile (-1 !=)
            .foreach (read=>output.write(bytes,0,read))

        output.close()
    }

    val response: HttpResponse[Unit] = Http(url).params(keys).headers(heads).charset("US-ASCII").timeout(1000, 60000).exec[Unit](parse)
    println(response.isSuccess)
}

如果有人能帮助我,我将不胜感激!我只需要将响应保存到 .xls 文件中。如果您有时间,我似乎也无法找到浏览器在何处获取 session ID,所以这对找出答案也很有帮助。

编辑:

我可以看到 excel 数据实际上正在发送,请使用 wireshark 查看此流:

stream

谢谢!

最佳答案

好吧,不太确定为什么我的流变为空,但我更改了代码以在遇到异常时关闭输出流。我知道这很愚蠢,但它确实有效并且文件已完全下载。如果有人知道更好的解释,我将不胜感激!

这是新的解析函数:

def parse(status: Int, headers: Map[String, IndexedSeq[String]], inputStream: InputStream): Unit = {
    val output = new BufferedOutputStream(new FileOutputStream(filename))
    try {
      Iterator
        .continually (inputStream.read)
        .takeWhile (-1 !=)
        .foreach (output.write)
    }
    catch {
      case _: Throwable => output.close()
    }
    finally{
      output.close()
    }
  }

关于excel - 带有空主体的 HTTP GET 响应,在 IE 中下载了 excel 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43991340/

相关文章:

excel - 复选框以选中和取消选中电子表格中的所有其他复选框

Ruby HTTP POST - 错误

excel - MsgBox 中的表格

excel - 最有效地根据组合框值评估列中的值列表

scala - Shapeless 的类型不等式有问题吗?

scala - 如何在 spark 3.0+ 中获得一年中的一周?

scala - 在 Scala 中使用选项

http - 对于哪些 3xx HTTP 代码,Location header 是强制性的?

R shiny - 如何将带有参数的请求 url 发送到 R shiny 并仅获得数据响应?

excel - 如何获取日期范围之间的天数