How do I use InternetSetOption?

 ̄綄美尐妖づ 提交于 2019-11-27 04:40:37

问题


This question:

Using Proxy with web browser control

Told me to use "InternetSetOption". How do I use it? How might I implement it in code (example?)?

Thanks!


回答1:


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);

private void RefreshIESettings(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));

    // Converting structure to IntPtr
    Marshal.StructureToPtr(struct_IPI, intptrStruct, true);

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

private void Usage()
{
    RefreshIESettings("1.2.3.4:8080");
    object nullObject = 0;
    string strTemp = "";
    object nullObjStr = strTemp;
    axWebBrowser1.Navigate("http://test.com", ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
}


来源:https://stackoverflow.com/questions/6353715/how-do-i-use-internetsetoption

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!