How to send request from different static IP address to a any website?

2.8k Views Asked by At

I am sending HTTP request using C#. (http://codesamplez.com/programming/http-request-c-sharp)

I have dedicated server on.I have purchased more static IPs.

How can I send request using these different IPs.

1

There are 1 best solutions below

0
On

What you want to do is bind to a specific network adapter. By default the LocalEndpoint is null so your connection will be to assigned an adapter. You can specify what to bind to using HttpWebRequest.ServicePoint.BindIPEndPointDelegate.

var req = (HttpWebRequest)WebRequest.Create("http://google.com/");
req.ServicePoint.BindIPEndPointDelegate = BindTo;
using (req.GetResponse());

static IPEndPoint BindTo(ServicePoint servicepoint, IPEndPoint remoteendpoint, int retrycount)
{
    IPAddress ip = IPAddress.Any; //This is where you specify the network adapter's address
    int port = 0; //This in most cases should stay 0. This when 0 will bind to any port available.
    return new IPEndPoint(ip, port);
}

Here is some more information on binding from msdn.

Use the Bind method if you need to use a specific local endpoint. You must call Bind before you can call the Listen method. You do not need to call Bind before using the Connect method unless you need to use a specific local endpoint. You can use the Bind method on both connectionless and connection-oriented protocols.

Before calling Bind, you must first create the local IPEndPoint from which you intend to communicate data. If you do not care which local address is assigned, you can create an IPEndPoint using IPAddress.Any as the address parameter, and the underlying service provider will assign the most appropriate network address. This might help simplify your application if you have multiple network interfaces. If you do not care which local port is used, you can create an IPEndPoint using 0 for the port number. In this case, the service provider will assign an available port number between 1024 and 5000.

If you use the above approach, you can discover what local network address and port number has been assigned by calling the LocalEndPoint. If you are using a connection-oriented protocol, LocalEndPoint will not return the locally assigned network address until after you have made a call to the Connect or EndConnect method. If you are using a connectionless protocol, you will not have access to this information until you have completed a send or receive.