C#設置當前程序經過IE代理服務器上網

注意:如下設置只在當前程序中有效,對IE瀏覽器無效,且關閉程序後,自動釋放代碼。瀏覽器

Proxy.cs類.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

public static class Proxy
{
    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 static void SetIEProxy(string strProxy)
    {
        const int INTERNET_OPTION_PROXY = 38;
        const int INTERNET_OPEN_TYPE_PROXY = 3;
        Struct_INTERNET_PROXY_INFO struct_IPI;
        struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
        struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy);
        struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local");
        IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI));
        Marshal.StructureToPtr(struct_IPI, intptrStruct, true);
        bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(struct_IPI));
    }
}代理

 

調用blog

設置代理:Proxy.SetIEProxy("127.0.0.1:8080");ip

取消代理:Proxy.SetIEProxy(string.Empty);     //設置爲空便可get


Proxy類下載:點擊下載string

 

出處:http://blog.csdn.net/joyhen/article/details/19114999io

相關文章
相關標籤/搜索