Phil
Phil

Reputation: 14661

Scala: Retrieving contents of a URL with specified headers and interface [ip] address

I would like to know how to get the source of a URL (like Source.fromURL) with specified headers and specified IP address (the machine where the code is executed has not one but several ip addresses bind).

How can I achieve this?

Thank you.

Upvotes: 0

Views: 3398

Answers (2)

Leif Wickland
Leif Wickland

Reputation: 3724

Based on this other answer, it looks to me like in current versions of java (6 /7) there's no nice way to make an HTTP request that is bound to a particular local address. (The direct proxy approach that used to work appears to be explicitly blocked now.) That answer makes it look like one relatively easy way is to use the Apache commons HttpClient, which lets you specify the local address to bind to.

Upvotes: 1

Odomontois
Odomontois

Reputation: 16308

I think you could do that the same way:

import io.Source
import java.net.URL

val stackOverflowURL = "http://69.170.135.92:80"
val requestProperties = Map(
  "User-Agent" -> "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
)
val connection = new URL(stackOverflowURL).openConnection
requestProperties.foreach({
  case (name, value) => connection.setRequestProperty(name, value)
})

print(Source.fromInputStream(connection.getInputStream).getLines.mkString("\n"))

Upvotes: 6

Related Questions