:
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Net; usingSystem.Text; usingSystem.Windows.Forms; namespaceTestproxy { Public Partial classMainform:form { PublicMainForm () {InitializeComponent (); } Private voidBtndownload_click (Objectsender, EventArgs e) { Try{Start (); } Catch(Exception ex) {MessageBox.Show (ex). Message); } } Private voidStart () {txtresult.clear (); varClient =NewWebClient (); varURI =NewUri (TxtUrl.Text.Trim ()); Client. Downloadstringcompleted+=client_downloadstringcompleted; Client. Encoding=Encoding.UTF8; if(checkbox.checked) {varProxy =Createproxy (); if(Proxy = =NULL)return; Client. Proxy=proxy; } client. DownloadStringAsync (URI); } Private voidclient_DownloadStringCompleted (Objectsender, DownloadStringCompletedEventArgs e) { Try{Txtresult.text=E.result; } Catch(Exception ex) {varmsg =Ex. Message; if(ex. InnerException! =NULL) msg =Ex. Innerexception.message; MessageBox.Show (msg); } } Private voidCheckBox_CheckedChanged (Objectsender, EventArgs e) {gbproxy.enabled=checkbox.checked; } PrivateWebProxy Createproxy () {varHost =TxtHost.Text.Trim (); if(string. Isnullorwhitespace (host)) {MessageBox.Show ("Please enter a proxy address"); return NULL; } varPort =0; Try{Port=Convert.ToInt32 (TxtPort.Text.Trim ()); } Catch(Exception) {MessageBox.Show ("Please enter the correct proxy port"); return NULL; } varCRE =NewNetworkCredential (txtUsername.Text, Txtpwd.text); varProxy =NewWebProxy (TxtHost.Text.Trim (), port) {Credentials =CRE}; returnproxy; } } }
Adding proxy proxies to WebClient in C #