0

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.

Phil
  • 13,875
  • 21
  • 81
  • 126
  • Phil, are you trying to set the IP address that the request will be sent from or determine address it was sent from after it was sent? – Leif Wickland Feb 27 '12 at 19:08
  • The first one. Set the IP address (network interface) that the request will be sent from. – Phil Feb 27 '12 at 21:24

2 Answers2

6

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"))
Odomontois
  • 15,918
  • 2
  • 36
  • 71
  • Dear Odomontois, I sincerely thank you very much for taking your time to reply. However, the most important bit is missing, probably due to my bad self-explanation. I want to set the IP from which this get request is dispatched. As in, setting the interface that this connection will use. The server has multiple IP addresses attached to it and I want to set the IP from which the connection will be fired. How do you think that can be done? I would REALLY appreciate your help. THANK YOU!!! – Phil Feb 26 '12 at 15:28
1

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.

Community
  • 1
  • 1
Leif Wickland
  • 3,693
  • 26
  • 43
  • 1
    This is quite a tragedy and unfortunate. In a world where PHP can do this extremely easily, Python needing complex modules and add-ons and Java failing to do this, is really a tragedy. Thank you very much for your input on the matter. – Phil Feb 28 '12 at 13:59