1

我正在尝试使用以下代码登录我的 eBay 帐户:

string signInURL = "https://signin.ebay.com/ws/eBayISAPI.dll?co_partnerid=2&siteid=0&UsingSSL=1";
string postData = String.Format("MfcISAPICommand=SignInWelcome&userid={0}&pass={1}", "username", "password");
string contentType = "application/x-www-form-urlencoded";
string method = "POST";
string userAgent = "Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)";

CookieContainer cookieContainer = new CookieContainer();

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(signInURL);
req.CookieContainer = cookieContainer;
req.Method = method;
req.ContentType = contentType;
req.UserAgent = userAgent;
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes(postData);
req.ContentLength = loginDataBytes.Length;
Stream stream = req.GetRequestStream();
stream.Write(loginDataBytes, 0, loginDataBytes.Length);
stream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader xsr = new StreamReader(res.GetResponseStream());
String responseText = xsr.ReadToEnd();

显然替换了我的真实用户名和密码。当我查看字符串responseText时,我看到来自 eBay 的部分响应是

您使用的浏览器拒绝 cookie。

任何想法我做错了什么?

PS 是的,我也在使用 eBay API,但这与我想用 API 做的事情略有不同。

4

3 回答 3

1

在进行 POST 之前,您需要下载包含您在代码中提交的表单的页面,获取他们给您的 cookie,将其放入您的CookieContainer(确保您获得正确的路径)并将其重新发布到您的请求中。

澄清一下,虽然您可能发布了正确的数据,但您并没有发送需要与之一起使用的 cookie。您将从登录页面获取此 cookie。

于 2011-05-30T05:35:01.187 回答
1

你正在做一个直接的http请求。Ebay 网站具有与浏览器对话的功能(可能存储会话 cookie)。除非您使请求代码足够聪明以正确使用 cookie,否则它将无法正常工作。您可能不得不改用 Internet Explorer 对象。

于 2009-08-31T17:55:25.873 回答
0

您需要拦截 http 流量以查看到底发生了什么。我使用提琴手2。是调试http的好工具。所以我可以知道谁错了,我的应用程序或远程 Web 服务器。

使用 fiddler,您可以看到请求标头、响应标头及其 cookie 以及响应内容。它用于您的应用程序和 Ebay 的中间。

根据我的经验。我认为这是因为发送给您的 Ebay cookie 没有发送回 Ebay 服务器。无论是与否,Fiddler 都会证明这一点。

另一件事是,您收到的响应 cookie 应该使用相同的 CookieContainer 发送回下一个请求。

您应该注意到 CookieContainer 在 .Add(Cookie) 和 .GetCookies(uri) 方法上有一个错误。您可能不使用它,但内部代码可能会使用它。

查看详细信息并在此处修复:

http://dot-net-expertise.blogspot.com/2009/10/cookiecontainer-domain-handling-bug-fix.html

呼叫MeLaNN

于 2009-10-08T14:18:57.070 回答