Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用特定IP';s发送HTTP Web请求_C#_.net_Network Programming - Fatal编程技术网

C# 使用特定IP';s发送HTTP Web请求

C# 使用特定IP';s发送HTTP Web请求,c#,.net,network-programming,C#,.net,Network Programming,我做了一个程序,它发送HTTP webrequests 为了确保发送这些请求的安全,我希望能够定义一个IP,通过该IP发送请求。 因此,如果默认出口不起作用,则应通过另一个出口发送请求 我尝试过使用IPEndPoint和Sockets来实现这一点,但不知怎么的,它不起作用 这是到目前为止我的代码 public Response ExecuteRequest(RequestData requestData, NetworkAddress exit) { Tracer.Inf

我做了一个程序,它发送HTTP webrequests

为了确保发送这些请求的安全,我希望能够定义一个IP,通过该IP发送请求。 因此,如果默认出口不起作用,则应通过另一个出口发送请求

我尝试过使用IPEndPoint和Sockets来实现这一点,但不知怎么的,它不起作用

这是到目前为止我的代码

   public Response ExecuteRequest(RequestData requestData, NetworkAddress exit) {
         Tracer.Info("Versuche Request über HTTP abzusetzen.");
         if (!PrepareIpEndPoint(Address, exit)) {
            return null;
         }
         Address = new Uri(PlaceholderReplacer.ReplacePlaceholders(Address.ToString(), requestData));
         return RequestExecutor.ExecuteRequest(Address);
      }

      private bool PrepareIpEndPoint(Uri url, NetworkAddress exit) {
         Tracer.Debug(string.Format("Setze den IP-Endpoint auf{0}", exit));
         var ipEndPoint = new IPEndPoint(IPAddress.Parse(exit.Address), 0);
         var tempSocket = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
         tempSocket.Bind(ipEndPoint);
         tempSocket.Connect(ipEndPoint);
         return tempSocket.Connected;
      }
上面的代码正在抛出SocketException

非常感谢您的任何想法

谢谢,Josh删除这一行:(这是为了打开一个端口来接受客户端)

您希望通过以下方式连接到服务器以充当客户端:

   tempSocket.Connect(ipEndPoint);
你真的确定要连接到零端口吗?我很确定这不是一个有效的选择。(标准HTTP=80,HTTPS=443)


绑定用于打开可以接受客户端连接的服务器套接字。Connect用于连接到服务器套接字(作为客户端)。不能在同一个插座上同时使用这两个插座。(您是服务器或客户端)
上述代码正在抛出SocketExceptions
将完整异常发布到字符串。由于缺少错误信息,很难诊断错误。
   tempSocket.Connect(ipEndPoint);
   var ipEndPoint = new IPEndPoint(IPAddress.Parse(exit.Address), 80);