博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#中给WebClient添加代理Proxy
阅读量:5057 次
发布时间:2019-06-12

本文共 2573 字,大约阅读时间需要 8 分钟。

效果图:

 

using System;  using System.Collections.Generic;  using System.ComponentModel;  using System.Data;  using System.Drawing;  using System.Linq;  using System.Net;  using System.Text;  using System.Windows.Forms;    namespace TestProxy  {      public partial class MainForm : Form      {          public MainForm()          {              InitializeComponent();          }            private void btnDownload_Click(object sender, EventArgs e)          {              try              {                  Start();              }              catch (Exception ex)              {                  MessageBox.Show(ex.Message);              }          }            private void Start()          {              txtResult.Clear();              var client = new WebClient();              var uri = new Uri(txtUrl.Text.Trim());              client.DownloadStringCompleted += client_DownloadStringCompleted;              client.Encoding = Encoding.UTF8;                if (checkBox.Checked)              {                  var proxy = CreateProxy();                  if (proxy == null) return;                  client.Proxy = proxy;              }              client.DownloadStringAsync(uri);          }            private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)          {              try              {                  txtResult.Text = e.Result;              }              catch (Exception ex)              {                  var msg = ex.Message;                  if (ex.InnerException != null) msg = ex.InnerException.Message;                  MessageBox.Show(msg);              }          }            private void checkBox_CheckedChanged(object sender, EventArgs e)          {              gbProxy.Enabled = checkBox.Checked;          }            private WebProxy CreateProxy()          {              var host = txtHost.Text.Trim();              if (string.IsNullOrWhiteSpace(host))              {                  MessageBox.Show("请输入代理地址");                  return null;              }              var port = 0;              try              {                  port = Convert.ToInt32(txtPort.Text.Trim());              }              catch (Exception)              {                  MessageBox.Show("请输入正确的代理端口");                  return null;              }              var cre = new NetworkCredential(txtUserName.Text, txtPwd.Text);              var proxy = new WebProxy(txtHost.Text.Trim(), port) {Credentials = cre};              return proxy;          }      }  }

 

转载于:https://www.cnblogs.com/sanler/p/7268624.html

你可能感兴趣的文章
判断网页请求与FTP请求
查看>>
15.二叉链表类
查看>>
页面置换算法-LRU(Least Recently Used)c++实现
查看>>
如何获取Android系统时间是24小时制还是12小时制
查看>>
Android实现获取本机中所有图片
查看>>
CSS3 总结-2
查看>>
fur168.com 改成5917电影
查看>>
PHP上传RAR压缩包并解压目录
查看>>
codeforces global round 1题解搬运
查看>>
python os模块
查看>>
Codeforces 719B Anatoly and Cockroaches
查看>>
jenkins常用插件汇总
查看>>
c# 泛型+反射
查看>>
第九章 前后查找
查看>>
Python学习资料
查看>>
多服务器操作利器 - Polysh
查看>>
[LeetCode] Candy
查看>>
Jmeter学习系列----3 配置元件之计数器
查看>>
jQuery 自定义函数
查看>>
jq 杂
查看>>