如何在C#interop中使用InternetQueryOption?

时间:2012-04-10 12:05:57

标签: c# c++ interop marshalling wininet

在我的C#代码中,我想使用MSDN中定义的InternetQueryOption,例如:

BOOL InternetQueryOption(
  __in     HINTERNET hInternet,
  __in     DWORD dwOption,
  __out    LPVOID lpBuffer,
  __inout  LPDWORD lpdwBufferLength
);

在我的C#代码中,我写道:

    [DllImport("wininet.dll", SetLastError = true)]
            static extern bool InternetQueryOption(
            IntPtr hInternet, 
            uint dwOption, 
            IntPtr lpBuffer, 
            ref int lpdwBufferLength);

我的C ++代码:

...
HINTERNET hRequest = HttpOpenRequest(hConnect, "POST","/BM-Login/auth-cup", NULL, NULL, accept, secureFlags, 0);
    DWORD dwFlags;
    DWORD dwBuffLen = sizeof(dwFlags);

    InternetQueryOption (hRequest, INTERNET_OPTION_SECURITY_FLAGS,
            (LPVOID)&dwFlags, &dwBuffLen);

    dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
      dwFlags |= SECURITY_FLAG_IGNORE_REVOCATION;
      dwFlags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
      dwFlags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID | SECURITY_FLAG_IGNORE_WRONG_USAGE;

    InternetSetOption (hRequest, INTERNET_OPTION_SECURITY_FLAGS,
                            &dwFlags, sizeof (dwFlags) );
...

如何在C#中编写相同内容?谢谢。 (抱歉我的英语非常糟糕)

1 个答案:

答案 0 :(得分:1)

我建议使用manged代码,而不是通过互操作来实现。看看WebRequest Class。另外,请查看我对stackoverflow问题C# https login and download file的回答,以获取如何使用此类的工作示例。