Is there a way to specify a local address and port on a URLConnection.

R

rubikzube*

Hi. I'm trying to specify the local address and port (i.e. network
interface) to use for a URLConnection.

I didn't see any public methods in the documentation for the java.net
package that would allow me to do this.

I tried to debug into the URLConnection to look at how the native
implementation sets the local port and address, but there didn't seem
to be any source available for the concrete classes in
sun.net.www.protocol.http.

I resorted to instantiating my own socket and binding it to the local
address and port that I want, but it's not an ideal proposition
because I have to write my own HTTPRequests.

If anyone knows how to do this without using sockets, I would be
obliged.

I'm using a Mac running 10.4.8 with the 1.5.0 JVM and Eclipse 3.2.2.
 
M

Michael Rauscher

rubikzube* said:
Hi. I'm trying to specify the local address and port (i.e. network
interface) to use for a URLConnection.

I didn't see any public methods in the documentation for the java.net
package that would allow me to do this.

From the API documentation of URLConnection:

The connection object is created by invoking the openConnection method
on a URL.

So:

URL url = new URL("http://127.0.0.1:80");
URLConnection connection = url.openConnection();

Bye
Michael
 
G

Gordon Beaton

Hi. I'm trying to specify the local address and port (i.e. network
interface) to use for a URLConnection.

I didn't see any public methods in the documentation for the
java.net package that would allow me to do this.

Normally the OS should be allowed to choose the appropriate outgoing
interface based on information in the routing table, and you should
therefore make sure your routing table is configured appropriately
(i.e. add an entry for the given address or network that specifies the
interface you want).

If that doesn't address your needs, then URLStreamHandler might hold
some clues (I've never done this, just browsed the documentation).
Otherwise I believe that rolling your own with a Socket is the only
way forward.

Why do you need to specify the local port?

/gordon
 
R

rubikzube*

Normally the OS should be allowed to choose the appropriate outgoing
interface based on information in the routing table

Normally, yes, but not for my sinister plans :)
Why do you need to specify the local port?

I'm working on framework that will allow users to add attributes to
network interfaces, and users will be able to set acceptable levels on
these attributes that the framework will consult in order to select
which network interface to use as the primary interface.

I tried checking out what's involved in subclassing URLStreamHandler,
but it's abstract and I can't debug into it because the concrete class
is in sun.net.www.protocol.http. I'll give it another pass, tho, if
there's no easy way out.

Tks.
 
G

Gordon Beaton

I'm working on framework that will allow users to add attributes to
network interfaces, and users will be able to set acceptable levels
on these attributes that the framework will consult in order to
select which network interface to use as the primary interface.

I understand why one might want to specify the local address or
outgoing interface, but in the subject line you write "and port",
which I can't see any need to specify at the client.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top