让webbrowser也可以设置代理服务器

来源:互联网 发布:java内存泄露他检测 编辑:程序博客网 时间:2024/04/28 23:46

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Runtime.InteropServices;
namespace _g
{
  public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }

  private void button1_Click(object sender, EventArgs e)
  {
  //System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)WebRequest.Create("http://book.3g.cn/index.aspx?cin=3&sid=&frcin=1&rd=38");
  //WebProxy _WP = new WebProxy("211.95.176.6", 808);
  //_WP.BypassProxyOnLocal = true;
  //ICredentials credentials = new NetworkCredential("","","");
  //_WP.Credentials = credentials;



  //req.Accept = "TCL E737 TCL-e737/401/WAP-2.0/MIDP-1.0/CLDC-1.0";
  //req.Proxy = _WP;
  //System.IO.Stream str=null;
  //System.IO.StreamReader sr=null;
  //System.Net.WebResponse webresponse=null;
  //try
  //{
  // webresponse = req.GetResponse();
  // str = webresponse.GetResponseStream();
  // string PageCode = "gb2312";

  RefreshIESettings("211.95.176.6:808");

  System.Object nullObject = 0;

  string strTemp = String.Empty;

  System.Object nullObjStr = strTemp;

  this.webBrowser1.Navigate("http://book.3g.cn/index.aspx?cin=3&sid=&frcin=1&rd=38", null, null, null);

  //if ((webresponse.ContentType != null) && (webresponse.ContentType.IndexOf("charset") != -1)) //处理页面编码,页面有设置则按设置,否则按系统默认编码
  //{
  //MessageBox.Show(response.ContentType.ToString());
  //PageCode = GetCharType(webresponse.ContentType.ToString());
  //sr = new StreamReader(str, System.Text.Encoding.GetEncoding(PageCode));
  // this.richTextBox1.Text = sr.ReadToEnd();
  //}
  //}
  //catch (Exception ex)
  //{
  // MessageBox.Show(ex.Message);
  //}
  //finally
  //{
  // str.Close();
  // sr.Close();

  //}

  }
  private string GetCharType(string tmp1) //获取页面编码
  {
  string tmp = "";
  tmp = tmp1;
  int a = tmp.IndexOf("charset=") + 8;
  tmp = tmp.Substring(a, tmp.Length - a);
  return tmp;
  }
  public struct Struct_INTERNET_PROXY_INFO
  {

  public int dwAccessType;

  public IntPtr proxy;

  public IntPtr proxyBypass;

  };

  [DllImport("wininet.dll", SetLastError = true)]

  private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);

  public void RefreshIESettings(string strProxy)
  {

  const int INTERNET_OPTION_PROXY = 38;

  const int INTERNET_OPEN_TYPE_PROXY = 3;

  Struct_INTERNET_PROXY_INFO struct_IPI;

  // Filling in structure

  struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;

  struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy);

  struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local");

  // Allocating memory

  IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI));

  // Converting structure to IntPtr

  Marshal.StructureToPtr(struct_IPI, intptrStruct, true);

  bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(struct_IPI));

  }
  }

}
使用的时候,调用RefreshIESettings

RefreshIESettings("211.95.176.6:808");

using System.Runtime.InteropServices;//需要添加这个引用

原创粉丝点击