Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
rubikzube* - 26 Feb 2007 08:11 GMT
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.
Michael Rauscher - 26 Feb 2007 08:56 GMT
> 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
Gordon Beaton - 26 Feb 2007 09:14 GMT
> 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

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

rubikzube* - 26 Feb 2007 09:52 GMT
> 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.
Gordon Beaton - 26 Feb 2007 10:14 GMT
>> 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 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

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.