這是我的代理代碼。使用不同代理的多個控制檯應用程序
[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);
}
此代碼可以正常使用來自應用程序的窗口。我試圖在控制檯應用程序中使用它,並檢查了我的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();
}
HTTPGET是一類,我從有:Get public/external IP address?
我希望代理與控制檯應用程序工作。我不知道是什麼問題 我也將運行多個程序實例,所以我希望每個控制檯使用一個代理,它應該隻影響控制檯的瀏覽而不是整個計算機。 代理將沒有身份驗證。
這是我自己的HTTPBase類處理所有整個應用程序的http請求,
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Web;
using System.Runtime.InteropServices;
public class HTTPBase
{
private CookieContainer _cookies = new CookieContainer();
private string _lasturl;
private int _retries = 3;
private string _useragent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; FunWebProducts; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; InfoPath.1)";
public HTTPBase()
{
ServicePointManager.UseNagleAlgorithm = false;
ServicePointManager.Expect100Continue = false;
}
public void ClearCookies()
{
this._cookies = new CookieContainer();
}
public static string encode(string str)
{
// return System.Net.WebUtility.HtmlEncode(str);
return HttpUtility.UrlEncode(str);
//return str;
}
public string get(string url)
{
for (int i = 0; i < this._retries; i++)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = this._useragent;
request.CookieContainer = this._cookies;
if (this._lasturl != null)
{
request.Referer = this._lasturl;
}
else
{
request.Referer = url;
}
this._lasturl = url;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
catch (Exception exception)
{
if ((i + 1) == this._retries)
{
throw exception;
}
}
}
throw new Exception("Failed");
}
public string post(string url, string postdata)
{
for (int i = 0; i < this._retries; i++)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
if (this._lasturl != null)
{
request.Referer = this._lasturl;
}
else
{
request.Referer = url;
}
this._lasturl = url;
request.Method = "POST";
request.UserAgent = this._useragent;
request.CookieContainer = this._cookies;
request.ContentLength = postdata.Length;
using (Stream stream = request.GetRequestStream())
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write(postdata);
}
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream stream2 = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream2))
{
return reader.ReadToEnd();
}
}
}
catch (Exception exception)
{
if (this._lasturl.Contains("youtuberender.php"))
{
return "bypassing youtube error";
}
if ((i + 1) == this._retries)
{
throw exception;
}
}
}
throw new Exception("Failed");
}
public string post(string url, string postdata, string referer)
{
for (int i = 0; i < this._retries; i++)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = referer;
this._lasturl = url;
request.Method = "POST";
request.UserAgent = this._useragent;
request.CookieContainer = this._cookies;
request.ContentLength = postdata.Length;
using (Stream stream = request.GetRequestStream())
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write(postdata);
}
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream stream2 = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream2))
{
return reader.ReadToEnd();
}
}
}
catch (Exception exception)
{
if (this._lasturl.Contains("youtube.com"))
{
return "bypassing youtube error";
}
if ((i + 1) == this._retries)
{
throw exception;
}
}
}
throw new Exception("Failed");
}
public string XmlHttpRequest(string urlString, string xmlContent)
{
string str = null;
HttpWebRequest request = null;
HttpWebResponse response = null;
request = (HttpWebRequest)WebRequest.Create(urlString);
try
{
byte[] bytes = Encoding.ASCII.GetBytes(xmlContent);
request.Method = "POST";
request.UserAgent = this._useragent;
request.CookieContainer = this._cookies;
request.ContentLength = bytes.Length;
request.Headers.Add("X-Requested-With", "XMLHttpRequest");
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
using (Stream stream = request.GetRequestStream())
{
stream.Write(bytes, 0, bytes.Length);
stream.Close();
}
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
using (Stream stream2 = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream2))
{
str = reader.ReadToEnd();
}
}
}
response.Close();
}
catch (WebException exception)
{
throw new Exception(exception.Message);
}
catch (Exception exception2)
{
throw new Exception(exception2.Message);
}
finally
{
response.Close();
response = null;
request = null;
}
return str;
}
public string UserAgent
{
get
{
return this._useragent;
}
set
{
this._useragent = value;
}
}
}
}
所以我希望能夠添加代理在此代碼。我將從主窗體傳遞一個變量,它將使用代理:端口格式,並且我希望此代碼使用代理。我對這些代理事物很陌生,並且有些混亂。 我使用.NET框架4.0 VS 12
只有一個問題:爲什麼不使用[WebRequest.DefaultWebProxy] (http://msdn.microsoft.com/en-us/library/system.net.webrequest.defaultwebproxy.aspx)'WebRequest.DefaultWebProxy = new WebProxy(「219.93.183.106」,8080);'? –