2

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!

Community
  • 1
  • 1
Alper
  • 1
  • 12
  • 39
  • 78
  • For god's sake, @Jacob. You don't ask a new question every time you don't get the answer you want, you edit your question and make it BETTER. You've asked variants of this particular question at least 5 times now. – Chris Eberle Jun 15 '11 at 13:43
  • Possible duplicate: http://stackoverflow.com/questions/6352780/using-a-proxy-in-c-webbrowser – Chris Eberle Jun 15 '11 at 13:45
  • Possible duplicate: http://stackoverflow.com/questions/6247368/how-do-i-access-registry-from-c-for-a-proxy – Chris Eberle Jun 15 '11 at 13:46
  • Possible duplicate: http://stackoverflow.com/questions/6247220/how-do-i-use-a-socket-proxy-in-c-webbrowser – Chris Eberle Jun 15 '11 at 13:47
  • Possible duplicate: http://stackoverflow.com/questions/6246498/how-do-i-use-a-socket-proxy-in-c-webbrowser – Chris Eberle Jun 15 '11 at 13:47
  • Possible duplicate: http://stackoverflow.com/questions/6244642/how-do-i-make-my-program-connect-through-a-proxy – Chris Eberle Jun 15 '11 at 13:47

1 Answers1

6
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);
}
Alex Aza
  • 76,499
  • 26
  • 155
  • 134
  • It says that 'axWebBrowser1' does not exist in current context. Marshal does not exist in current context. DllImport could not be found. -- What's wrong? What am I missing? – Alper Jun 15 '11 at 06:20
  • @Jacob - of course axWebBrowser1 does not exist, because it is an example. You need to replace with the reference to your WebBrowswer control. – Alex Aza Jun 15 '11 at 06:23
  • Well, what about 'Marshal' and 'DllImport' - won't work without those? – Alper Jun 15 '11 at 06:24
  • @Jacob - for `DllImport` add 'using System.Runtime.InteropServices' – Alex Aza Jun 15 '11 at 06:24