How to change the request IP in HttpWebRequest?

2020-06-23 05:24发布

问题:

I'm developing a website that will connect to a credit card processing gateway webservice. For security purposes this webservice accepts requests only from IP addresses that were previously informed to them.

Since I'm developing locally, my IP changes almost every day. Is there a way for me to change the IP address of a HttpWebRequest so that I can test the Webservice calls locally?

This webservice is accessed through a https address and the methods must be sent via POST.

回答1:

No, but if you managed to changes the source IP address of your requests, what you would be doing is called IP spoofing. The problem is that the source IP is used to route responses back to your machine, so since you somehow managed to change the IP address in the request packets, the response would never get back to you because that is not your IP address.



回答2:

I know this is a old post. But I was able to get this work for me, hope this will be useful for someone in need of similar issue

  ServicePointManager.Expect100Continue = true;
            if (System.Web.HttpContext.Current.Request.IsLocal)
            {
                webRequest.ServicePoint.BindIPEndPointDelegate = delegate(
                ServicePoint servicePoint,
                IPEndPoint remoteEndPoint,
                int retryCount)
                {
                    return new IPEndPoint(
                        IPAddress.Parse("192.168.1.1"),
                        0);
                };
            }


回答3:

You might want to check out JSONP if your data is in the JSON encoding as that is exactly for the purpose of requesting data from a webserver other than the one sending the original webpage.