invoke HttpWebRequest from specific IP of card - how?

  • Thread starter Thread starter Andrew Kibler
  • Start date Start date
A

Andrew Kibler

I have a setup where multiple IPs can be configured for a network card
to allow multiple server programs binding on a port (80) each with their
own IPs.

I want to invoke a HttpWebRequest from a specific IP that corresponds
with a given server. How do I tell HttpWebRequest which IP to request
from?

here is a code snip of setting up a request:

HttpWebRequest httpWebRequest =
HttpWebRequest)WebRequest.Create(requestUri);
httpWebRequest.KeepAlive = false;
WebProxy webProxy = WebProxy.GetDefaultProxy();
webProxy.BypassProxyOnLocal = true;
httpWebRequest.Proxy = webProxy;

HttpWebResponse httpWebResponse;
try
{
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
}
catch (WebException webException)
{
httpWebResponse = (HttpWebResponse)webException.Response;
}
WebHeaderCollection responseHeaders = httpWebResponse.Headers;
byte[] responseHtml = new byte[0];

if(httpWebResponse.ContentLength > 0)
{
Stream httpWebResponseReader =
httpWebResponse.GetResponseStream();

etc
etc


thanks.
 
Hey Andrew,
I have a setup where multiple IPs can be configured for a network card
to allow multiple server programs binding on a port (80) each with their
own IPs.

I just posted exactly the same question... I guess I should have read the
threads beforehand.

Andrew, did you get a response or find a solution in the meantime?

Thanks, Christoph
 
Joerg said:
A new feature introduced in .NET 2.0 allows you to do that. See
http://msdn2.microsoft.com/library/ms144146(en-us,vs.80).aspx


Thank you, that does look like a possibility. However, how would I
assign a servicepoint to a httpwebrequest, if the
httpwebrequest.servicepoint property is read only? Could you give a
code snip outlining how to create a servicepoint with a delegated
endpoint and how to assign that to a httpwebrequest? thanks!
 
Andrew said:
Thank you, that does look like a possibility. However, how would I
assign a servicepoint to a httpwebrequest, if the
httpwebrequest.servicepoint property is read only? Could you give a
code snip outlining how to create a servicepoint with a delegated
endpoint and how to assign that to a httpwebrequest? thanks!

You don't and can't create ServicePoints -- that's being taken care of
by the framework. Use ServicePointManager to obtain a ServicePoint for
a given URI and attach your delegate to this object.

static void Get(Uri uri)
{
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri);
request.UserAgent =
"Mozilla/5.0 (Windows; U; Windows NT 5.1; de-DE; rv:1.7.8) " +
"Gecko/20050511 Firefox/1.0.4";
ServicePoint servicePoint =
ServicePointManager.FindServicePoint(uri);
servicePoint.BindIPEndPointDelegate = new BindIPEndPoint(Bind);

using (MemoryStream memoryStream = new MemoryStream(0x10000))
using (HttpWebResponse response =
(HttpWebResponse) request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
{
// Add a breakpoint on the next line. When the breakpoint is
// being hit, do a "netstat -p tcp" on a command line and
// verify that port 8888 is being used.
byte[] buffer = new byte[0x1000];
int bytes;

while ((bytes = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
memoryStream.Write(buffer, 0, bytes);
}

string text = Encoding.UTF8.GetString(memoryStream.ToArray());
Console.WriteLine(text);
}
}

static IPEndPoint Bind(ServicePoint servicePoint, IPEndPoint
remoteEndPoint, int retryCount)
{
IPAddress address = IPAddress.Parse("192.168.2.102");
return new IPEndPoint(address, 8888);
}

Of course you need to change the IP address in the sample to one that
exists in your machine.

Cheers,
 
Back
Top