Update delegate everytime some function run
OK. I think me title is very bad. I don't know that much of delegates, only that they are like function pointers in C... I have some code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace lol
{
class FetchHttpSite
{
public static string Download(string url, string ip)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.AllowAutoRedirect = true;
req.UserAgent = UserAgent.Random();
req.ServicePoint.BindIPEndPointDelegate = delegate(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount) {
return new IPEndPoint(IPAddress.Parse(ip), 0);
};
HttpWebResponse webResponse;
string content;
webResponse = (HttpWebResponse)req.GetResponse();
using (StreamReader stream = new StreamReader(webResponse.GetResponseStream()))
content = stream.ReadToEnd();
return content;
}
}
}
I want to fetch sites with different IPs. But when I tried this i realized that this delegate is like... static xD. It just takes the first IP I give him. So if I feed him a new IP he will still use the first he got. I found that BindIPEndPointDelegate code thingy here on Stackoverflow btw.
So what I want that delegate to always take that string ip. Like, update itself all the time. Is this possible. How do I do this? Is it possible? Could I do this another way?
Thank you!
EDIT:
I want to fetch the site using that IP I feed the method. So when the webserver get my request my address is string ip argument. :)
The method works! You see I've tried before: return new IPEndPoint(IPAddress.Parse(ip), 0);, e.g. Console.Writeline("hello!");, then it will just run once! E开发者_如何学Goven thou I ask 100x. It only takes the IP from the first one to use the method. Thank you!
EDIT 2:
Pasted the whole code.
精彩评论