1

这是我用于 BindIPEndPointDelegate 的代码:

    ServicePoint servicePoint,
    IPEndPoint remoteEndPoint,
    int retryCount)
      {
          if (lastIpEndpoint != null)
          {
              return lastIpEndpoint;
          }
          var candidates =
              GetAddresses(remoteEndPoint.AddressFamily);
          if (candidates == null || candidates.Count() == 0)
          {
              throw new NotImplementedException();
          }

          return
              lastIpEndpoint = new IPEndPoint(candidates[rnd.Next(candidates.Count())], remoteEndPoint.Port);
      };

    static IPAddress[] GetAddresses(AddressFamily af)
    {
        System.Net.IPHostEntry _IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
        return (from i in _IPHostEntry.AddressList where i.AddressFamily == af select i).ToArray();
    }

无论我做什么,我都会收到以下错误:

System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: The requested address is not valid in its context 173.194.39.68:80
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetResponse()
   at Webs.Web.SimpleGetResponseString(String uri, String& userAgent, List`1 redirectpath) 
   atWebs.Web.Get(String uri) 
   at test.Program.Main(String[] args)
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

代码有什么问题?

4

1 回答 1

2

BindIPEndPointDelegate 用于选择一个本地端点来发出请求,而不是选择远程端点。

当您需要来自某个 IP 地址或某个端口范围的请求时,这很有用。

于 2012-08-10T11:27:28.093 回答