《c 设置ie代理和使用代理访问网页》由会员分享,可在线阅读,更多相关《c 设置ie代理和使用代理访问网页(6页珍藏版)》请在金锄头文库上搜索。
1、C#设置IE代理和使用代理访问网页/打开注册表键 Microsoft.Win32.RegistryKey rk=Microsoft.Win32.Registry.CurrentUser.OpenSubKey(SoftwareMicrosoftWindowsCurrentVersionInternet Settings,true); /设置代理可用 rk.SetValue(ProxyEnable,1); /设置代理IP和端口 rk.SetValue(ProxyServer,222.222.222.222:808); rk.Close();使用WebProxy类,该类空间名 using S; We
2、bProxy proxyObject = new WebProxy("代理服务器IP和端口(例如:http:/proxyserver:80/)",true); WebRequest req = WebRequest.Create("http:/"); /使用WebRequest上网 req.Proxy = proxyObject; /设置代理public string CallWebPage(string url, string postDataStr, Encoding sendEncod,Encoding readEncod) string rStr
3、 = ; System.Net.WebRequest req = null; System.Net.WebResponse resp = null; System.IO.Stream os = null; System.IO.StreamReader sr = null; try /创建连接 req = System.Net.WebRequest.Create(url); /设置访问方式和发送的请求数据的内容类型 if (string.IsNullOrEmpty(postDataStr) req.ContentType = application/x-www-form-urlencoded;
4、req.Method = GET; else req.ContentType = application/x-www-form-urlencoded; req.Method = POST; if (sendEncod = null) sendEncod = System.Text.Encoding.Default; byte bytes = sendEncod.GetBytes(postDataStr); req.ContentLength = bytes.Length; os = req.GetRequestStream(); os.Write(bytes, 0, bytes.Length)
5、; os.Close(); /读取返回结果 resp = req.GetResponse(); if (readEncod = null) readEncod = System.Text.Encoding.Default; sr = new System.IO.StreamReader(resp.GetResponseStream(), readEncod); rStr = sr.ReadToEnd(); catch (Exception ex1) /LogUtil.Warn(HttpUtil.CallWebPage 异常: + ex1.Message); finally try /关闭资源
6、if (os != null) os.Dispose(); os.Close(); if (sr != null) sr.Dispose(); sr.Close(); if (resp != null) resp.Close(); if (req != null) req = null; catch (Exception ex2) /LogUtil.Exception(HttpUtil.CallWebPage 关闭连接异常: + ex2.Message); return rStr; 1 private void button3_Click(object sender, EventArgs e)
7、 2 3 /打开注册表键 4 Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(SoftwareMicrosoftWindowsCurrentVersionInternet Settings, true); 5 6 /设置代理可用 7 rk.SetValue(ProxyEnable, 1); 8 /设置代理IP和端口 9 rk.SetValue(ProxyServer, this.textBox1.Text.ToString() + : + this.textBox2.Text.To
8、String(); 10 rk.Close(); 11 Factory.ExecuteNonQuery(update IP set area=1 where address= + str1 + ); 12 this.dataGridView1.DataSource = Factory.GetDataTable(select * from ip); 13 MessageBox.Show(成功!); 14 15 16 private void button4_Click(object sender, EventArgs e) 17 18 /打开注册表键 19 Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(SoftwareMicrosoftWindowsCurrentVersionInternet Settings, true); 20 21 /设置代理不可用 22 rk.SetValue(ProxyEnable, 0); 23 rk.Close(); 24 MessageBox.Show(成功!); 25