HTTP GET replies with empty body, loads Excel document in IE
I am trying to figure out what is going on in this page when I download an Excel report. It only works in Internet Explorer, and when I try to load a report in Scala using ScalaJ , I create an input stream for the response body to be null. Can someone help me understand what's going on here?
Plaintext Request:
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
The code so far:
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)
}
If anyone can help me I would really appreciate it! I just need to save the answer in a .xls file. And if you have time, I also can't seem to find where the browser is picking up the session id, so it would be very helpful to know about that.
EDIT:
I can see that excel data is actually being sent, check this flow by following using wireshark:
Thank!
source to share
Ok, not really sure why my stream goes null, but I changed the code to just close the output stream when it got caught in an exception. I know this is stupid, but it worked and the file is fully downloaded. If anyone else knows a better explanation, I would appreciate it!
Here's the new parsing function:
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()
}
}
source to share