C # using the Proxy IP usage method

Source: Internet
Author: User
Tags http request reflection web services

A Brief introduction: WebProxy: HTTP proxy settings.

Official explanation: The WebProxy class contains WebRequest instances to determine whether proxy settings are used to send requests using a WEB proxy. You can specify global Web proxy settings in your computer and application configuration files, and your application may use instances of the WebProxy class to customize the purpose of the Web Proxy.

Personal Understanding: The proxy IP, port for encapsulation, and set proxy IP username and password, through the user name and password landing proxy host and related visits.

HttpWebClientProtocol: The base class for all XM L WEB Services Client proxies that use the HTTP transport protocol.

When you invoke the ease interface, you dynamically compile the source code, cast the instance created after the compilation to the HttpWebClientProtocol type, and enclose the proxy type in the HttpWebClientProtocol to use proxy IP for access.

Brief introduction Three: In HttpWebRequest, Webclien, HttpWebClientProtocol All can use proxy IP.

One: HttpWebRequest: The HTTP Form Crawl Web page, only in the launch of HTTP before the request plus proxy attribute can, such as the use of proxy IP crawl Baidu home:

HttpWebRequest HttpRequest = (HttpWebRequest) httpwebrequest.create ("http://www.baidu.com");

Httprequest.method = "Get";

Httprequest.credentials = CredentialCache.DefaultCredentials;

Set agent Properties WebProxy-------------------------------------------------

WebProxy proxy = new WebProxy ();

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

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

To assign a proxy to the HttpWebRequest Proxy property 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 ();

Two: 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;

Three: httpwebclientprotocol: Proxy IP Usage for WebService (details can participate in WebServiceHelper.cs of TTS Interactive service):

Get 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 class 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");

Compile here, causing memory leaks

Compiling proxy classes

CR = Icc.compileassemblyfromdom (cplist, CCU);

Generates a proxy instance and calls the method

System.Reflection.Assembly Assembly = cr.compiledassembly;

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

OB ject obj = activator.createinstance (t);

if (configurationmanager.appsettings["useyeexingproxy"] = = "true")

{

ICredentials cred;

WebProxy p = null;

var ProX = obj as HttpWebClientProtocol;

String proxyaddressandport = configurationmanager.appsettings["Proxyip"];

String proxyusername = configurationmanager.appsettings["Proxyname"];

String Proxypassword = configurationmanager.appsettings["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);

}

It's easy to add a proxy attribute to the WebClient on the web, but here you can simply pass in the proxy and invoke the easy interface 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.