2013-12-10 46 views
0

我的应用程序中有一个Web浏览器,我想更改即将加载页面的传入连接的IP。我将如何设置连接的IP地址?我以前在Java中完成过,但我不知道如何在C#中完成它。设置连接C的IP#

这里是我正在做Java中的新的IP和将其设置为打开使用IP在Java中的页面:

InetSocketAddress newIP = new InetSocketAddress(
    InetAddress.getByAddress(
     next, new byte[] {Byte.parseByte(bts[0]), 
     Byte.parseByte(bts[1]), Byte.parseByte(bts[2]), 
     Byte.parseByte(bts[3])} 
    ), 
    80); 

URL url = new URL("http://google.com"); 
url.openConnection(new Proxy(Proxy.Type.HTTP, newIP)); 

现在我只需要在C#中重新创建。

+0

你能告诉我们你在C#中开始了什么吗?你想用来建立连接的许多.net /第三方类是什么? – gunr2171

回答

0

如果要设置代理服务器的地址,你可以做到这一点的的WebRequest:

using System; 
using System.IO; 
using System.Net; 

class Program 
{ 
    static void Main(string[] args) 
    { 
     WebRequest wr = WebRequest.Create("http://www.google.com"); 
     WebProxy proxy = new WebProxy("http://localhost:8888"); 
     wr.Proxy = proxy; 
     StreamReader sr = new StreamReader(wr.GetResponse().GetResponseStream()); 

     Console.WriteLine(sr.ReadToEnd()); 
     Console.ReadLine(); 
    } 
} 

或者,如果你需要广泛的设置应用程序,你可以设置GlobalProxySelection.Select到您的代理以及所有后续HTTP请求将使用该代理:

using System; 
using System.IO; 
using System.Net; 

class Program 
{ 
    static void Main(string[] args) 
    { 
     WebProxy proxy = new WebProxy("http://localhost:8888"); 
     GlobalProxySelection.Select = proxy; 

     WebRequest wr = WebRequest.Create("http://www.google.com"); 

     StreamReader sr = new StreamReader(wr.GetResponse().GetResponseStream()); 

     Console.WriteLine(sr.ReadToEnd()); 
     Console.ReadLine(); 
    } 
}