Pat*_*ick 9 c# cookies wininet dll-reference
我目前正致力于使用Csharp获取cookie数据.我正在使用DLLImport在wininet.dll中调用InternetGetCookie,但是当我尝试它时,函数返回ERROR_INSUFFICIENT_BUFFER(错误代码122).
谁能帮我这个 ?
这是Dll参考的代码:
[DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto, EntryPoint="InternetGetCookie")]
public static extern bool InternetGetCookie(string lpszUrl, string lpszCookieName,
ref StringBuilder lpszCookieData, ref int lpdwSize);
Run Code Online (Sandbox Code Playgroud)
这就是我调用函数的方式:
InternetGetCookie("http://example.com", null, ref lpszCookieData, ref size)
Run Code Online (Sandbox Code Playgroud)
谢谢.
Bra*_*ger 15
返回值告诉您,您提供给函数的缓冲区不够大,无法包含它想要返回的数据.你需要调用InternetGetCookie两次:一旦传入大小为0,找出缓冲区应该有多大; 第二次,使用正确大小的缓冲区.
另外,P/Invoke签名是错误的; StringBuilder不应该是ref参数(并且EntryPoint参数是错误的,因为它没有指定正确的入口点名称).
声明这样的函数:
[DllImport("wininet.dll", SetLastError = true)]
public static extern bool InternetGetCookie(string lpszUrl, string lpszCookieName,
StringBuilder lpszCookieData, ref int lpdwSize);
Run Code Online (Sandbox Code Playgroud)
然后像这样调用它:
// find out how big a buffer is needed
int size = 0;
InternetGetCookie("http://example.com", null, null, ref size);
// create buffer of correct size
StringBuilder lpszCookieData = new StringBuilder(size);
InternetGetCookie("http://example.com", null, lpszCookieData, ref size);
// get cookie
string cookie = lpszCookieData.ToString();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6331 次 |
| 最近记录: |