为什么使用 InternetSetCookie 在 WebBroser 控件上设置 cookie 的代码不起作用?
我完成这个示例是为了尝试理解为什么我根本不使用 WebBrowser 发送 cookie,这很简单,表单上有一个 WebBrowser,仅此而已:
namespace BrowserTest
{
public partial class Form1 : Form
{
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool InternetSetCookie(string url, string name, string data);
public static bool SetWinINETCookieString(string url, string name, string data)
{
return Form1.InternetSetCookie(url, name, data);
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// None of two works
//SetWinINETCookieString("www.nonexistent.com", null, "dataToTest=thisIsTheData");
SetWinINETCookieString("www.nonexistent.com", "dataToTest", "thisIsTheData");
this.webBrowser1.Navigate("www.nonexistent.com");
}
}
}
这就是 Fiddler 所说的我要发送的内容:
看起来每个使用此功能的人都成功了,但我一生都无法让它工作。我在不同的电脑上尝试过,但也失败了。任何帮助都会很棒,谢谢。
I've done this sample to try to understand why I'm not sending cookies at all with my WebBrowser, it's pretty simple, the form has a WebBrowser on it, that's all:
namespace BrowserTest
{
public partial class Form1 : Form
{
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool InternetSetCookie(string url, string name, string data);
public static bool SetWinINETCookieString(string url, string name, string data)
{
return Form1.InternetSetCookie(url, name, data);
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// None of two works
//SetWinINETCookieString("www.nonexistent.com", null, "dataToTest=thisIsTheData");
SetWinINETCookieString("www.nonexistent.com", "dataToTest", "thisIsTheData");
this.webBrowser1.Navigate("www.nonexistent.com");
}
}
}
And that's what Fidller says I'm sending:
Looks like everyone using this function succeeds but for the life of me that I cannot get it working. I've tried at different computers and it fails there too. Any help will be great, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己刚刚遇到过这个问题。为了完整起见,您需要检查从 InternetSetCookie 返回的值,如果为 false,请调用 GetLastError,这会给您返回代码 87 - 无效参数。
IE
......
Just came across this myself.For completeness, you need to check the value returned from InternetSetCookie and if false, call GetLastError which would have given you a return code of 87 - invalid parameter.
i.e.
......
我发现了,问题出在没有检查操作结果。 www.nonexistent.com 不是有效的 URI,它必须是 http://www.nonexistent.com
I found it, the problem was not checking the result of the operation. www.nonexistent.com is not a valid URI, it has to be http://www.nonexistent.com