C # use proxy IP Address

Source: Internet
Author: User

C # use proxy IP Address

Brief Introduction 1: WebProxy: HTTP proxy settings.

Official explanation: the WebProxy class contains the proxy settings that the WebRequest instance uses to determine whether to use the Web proxy to send requests. You can specify global Web proxy settings in the computer and application configuration files, and the application can use WebProxy instances to customize the purpose of Web Proxy.

Personal Understanding: encapsulate the proxy IP address and Port, set the proxy IP user name and password, and log on to the proxy host using the user name and password for relevant access.

Brief Introduction 2: HttpWebClientProtocol: The base class of all xm l Web services Client proxies using HTTP transmission protocol.

When an easy-to-use interface is called, the source code is dynamically compiled, and the created instance is forcibly converted to the HttpWebClientProtocol type. The proxy type is attached to the HttpWebClientProtocol to access the instance by using the proxy IP address.

Brief Introduction 3: proxy IP addresses can be used in HttpWebRequest, WebClien, and HttpWebClientProtocol.

I. HttpWebRequest: a webpage has been crawled in Http format. You only need to add the proxy attribute to the request before initiating http. For example, the following uses the proxy IP to capture the Baidu homepage:

HttpWebRequest httpRequest = (HttpWebRequest) HttpWebRequest. Create ("http://www.baidu.com ");

HttpRequest. Method = "GET ";

HttpRequest. Credentials = CredentialCache. DefaultCredentials;

// Set the proxy attribute WebProxy -------------------------------------------------

WebProxy proxy = new WebProxy ();

Proxy. Address = new Uri ("http: // 58.22.62.163: 888 /");

Proxy. Credentials = new NetworkCredential ("juese", "1029 ");

// Assign the proxy attribute to HttpWebRequest before initiating an HTTP request

HttpRequest. Proxy = proxy;

//-------------------------------------------------

HttpWebResponse res = (HttpWebResponse) httpRequest. GetResponse ();

StreamReader reader = new StreamReader (res. GetResponseStream (), System. Text. Encoding. UTF8 );

String content = reader. ReadToEnd ();

Reader. Close ();

Ii. WebClien: similar to the above,

WebClient wc = new WebClient ();

WebProxy proxy = new WebProxy ();

Proxy. Address = new Uri ("http: // 58.22.62.163: 888 /");

Proxy. Credentials = new NetworkCredential ("juese", "1029 ");

Wc. Proxy = proxy;

Stream PageHtml = wc. OpenRead ("http://www.baidu.com ");

StreamReader reader = new StreamReader (PageHtml, System. Text. Encoding. UTF8 );

String content = reader. ReadToEnd ();

Return content;

3. HttpWebClientProtocol: Used for the proxy IP address of the webService (for details, you can participate in the WebServiceHelper. cs of The TTS interactive service ):

// Obtain the WSDL

WebClient wc = new WebClient ();

Stream = wc. OpenRead (url );

ServiceDesc ription sd = ServiceDesc ription. Read (stream );

ServiceDesc riptionImporter sdi = new ServiceDesc riptionImporter ();

Sdi. AddServiceDesc ription (sd, string. Empty, string. Empty );

CodeNamespace cn = new CodeNamespace (@ namespace );

// Generate client proxy code

CodeCompileUnit ccu = new CodeCompileUnit ();

Ccu. Namespaces. Add (cn );

Sdi. Import (cn, ccu );

CSharpCodeProvider icc = new CSharpCodeProvider ();

// Set compilation Parameters

CompilerParameters cplist = new CompilerParameters ();

Cplist. GenerateExecutable = false;

Cplist. GenerateInMemory = true;

Cplist. ReferencedAssemblies. Add ("System. dll ");

Cplist. ReferencedAssemblies. Add ("System. xm l. dll ");

Cplist. ReferencedAssemblies. Add ("System. Web. Services. dll ");

Cplist. ReferencedAssemblies. Add ("System. Data. dll ");

//// The compilation will not stop, causing memory leakage

// Compile the proxy class

Cr = icc. CompileAssemblyFromDom (cplist, ccu );

// Generate a proxy instance and call the Method

System. Reflection. Assembly assembly = cr. CompiledAssembly;

Type t = assembly. GetType (@ namespace + "." + classname, true, true );

Ob ject obj = Activator. CreateInstance (t );

If (ConfigurationManager. etettings ["UseYeexingProxy"] = "true ")

{

ICredentials cred;

WebProxy p = null;

Var prox = obj as HttpWebClientProtocol;

String proxyAddressAndPort = ConfigurationManager. etettings ["ProxyIP"];

String proxyUserName = ConfigurationManager. receivettings ["ProxyName"];

String proxyPassword = ConfigurationManager. receivettings ["ProxyPwd"];

Cred = new NetworkCredential (proxyUserName, proxyPassword );

P = new WebProxy (proxyAddressAndPort, true, null, cred );

Prox. Proxy = p;

System. Reflection. MethodInfo mi = t. GetMethod (methodname );

Return mi. Invoke (prox, args );

}

Else

{

System. Reflection. MethodInfo mi = t. GetMethod (methodname );

Return mi. Invoke (obj, args );

}

Viewing relevant documents on the Internet makes it easy to add the Proxy attribute to WebClient. However, the Proxy is passed in directly and the interface is called asynchronously.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.