C# 使用不同代理的多个控制台应用程序
这是我的代理代码C# 使用不同代理的多个控制台应用程序,c#,C#,这是我的代理代码 [Runtime.InteropServices.DllImport("wininet.dll", SetLastError = true)] private static bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength); public struct Struct_INTERNET_PROXY_INFO { public int
[Runtime.InteropServices.DllImport("wininet.dll", SetLastError = true)]
private static bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
public struct Struct_INTERNET_PROXY_INFO
{
public int dwAccessType;
public IntPtr proxy;
public IntPtr proxyBypass;
}
private void UseProxy(string strProxy)
{
const int INTERNET_OPTION_PROXY = 38;
const int INTERNET_OPEN_TYPE_PROXY = 3;
Struct_INTERNET_PROXY_INFO struct_IPI = default(Struct_INTERNET_PROXY_INFO);
struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy);
struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local");
IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI));
Marshal.StructureToPtr(struct_IPI, intptrStruct, true);
bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, System.Runtime.InteropServices.Marshal.SizeOf(struct_IPI));
}
private void Button1_Click(System.Object sender, System.EventArgs e)
{
Label4.Text = (TextBox1.Text + ":" + TextBox2.Text);
}
此代码适用于windows from应用程序。我尝试在一个控制台应用程序中使用它,并检查了我的ip,但它不起作用。下面是我如何在控制台应用程序中使用它
static void Main()
{
Console.WriteLine("Ip before Proxy /r/n");
HTTPGet req = new HTTPGet();
req.Request("http://checkip.dyndns.org");
string[] a = req.ResponseBody.Split(':');
string a2 = a[1].Substring(1);
string[] a3 = a2.Split('<');
string a4 = a3[0];
Console.WriteLine(a4);
UseProxy("219.93.183.106:8080");
Console.WriteLine("Ip after Proxy /r/n");
HTTPGet req1 = new HTTPGet();
req1.Request("http://checkip.dyndns.org");
string[] a1 = req1.ResponseBody.Split(':');
string a21 = a1[1].Substring(1);
string[] a31 = a21.Split('<');
string a41 = a31[0];
Console.WriteLine(a41);
Console.ReadLine();
}
static void Main()
{
控制台写入线(“代理前Ip/r/n”);
HTTPGet req=新的HTTPGet();
请求http://checkip.dyndns.org");
字符串[]a=req.ResponseBody.Split(“:”);
字符串a2=a[1]。子字符串(1);
string[]a3=a2.Split('您可以使用
也可以显式设置代理:
using (WebClient wc = new WebClient { Proxy = null })
{
string html = wc.DownloadString("http://checkip.dyndns.org");
Console.WriteLine(XDocument.Parse(html).Root.Element("body").Value);
}
using (WebClient wc = new WebClient { Proxy = new WebProxy("219.93.183.106", 8080) })
{
string html = wc.DownloadString("http://checkip.dyndns.org");
Console.WriteLine(XDocument.Parse(html).Root.Element("body").Value);
}
只有一个问题:为什么不使用WebRequest.DefaultWebProxy=newwebproxy(“219.93.183.106”,8080);
?你能把这段代码和我的HTTPBase集成在一起吗?我不知道怎么做。如果你能把那段代码传给我,那就太难了helpful@user3502927只需重新声明UseProxy
为private void UseProxy(字符串strProxy){WebRequest.DefaultWebProxy=string.IsNullOrEmpty(strProxy)?null:新的WebProxy(strProxy);}
。我不知道为什么,但它不适用于我的类httpBase代码。我已尝试将代码添加到httpBase.get字符串中,并在请求ip之前以主格式添加了代码,但我无法使代理正常工作。我找到了它,但不确定修复方法是什么。它适用于特定ip,不适用于其他代理。我的windows窗体这个应用程序可以在我所有的ip上运行,但我的控制台juz可以在某些ip上运行。这个应用程序不能在这个ip上运行,111.161.126.85:80
WebRequest.DefaultWebProxy = null;
using (WebClient wc = new WebClient())
{
string html = wc.DownloadString("http://checkip.dyndns.org");
Console.WriteLine(XDocument.Parse(html).Root.Element("body").Value);
}
WebRequest.DefaultWebProxy = new WebProxy("219.93.183.106", 8080);
using (WebClient wc = new WebClient())
{
string html = wc.DownloadString("http://checkip.dyndns.org");
Console.WriteLine(XDocument.Parse(html).Root.Element("body").Value);
}
using (WebClient wc = new WebClient { Proxy = null })
{
string html = wc.DownloadString("http://checkip.dyndns.org");
Console.WriteLine(XDocument.Parse(html).Root.Element("body").Value);
}
using (WebClient wc = new WebClient { Proxy = new WebProxy("219.93.183.106", 8080) })
{
string html = wc.DownloadString("http://checkip.dyndns.org");
Console.WriteLine(XDocument.Parse(html).Root.Element("body").Value);
}