我正在尝试使用EBay登录到我的C#帐户。我看过不同的帖子,发现了以下3种选择。不巧的是,他们中没有一个在工作。我还列出了我获得的响应头值,以及Fiddler在登录时显示的响应头值。因为我只得到一个"Set-cookie“值,而Fiddler显示了10个"Set-Cookie”值,这是有区别的。我哪里出问题了?另外,为什么我只得到一个“设定-饼干”值?如果有人能分享工作解决方案的话,我非常感激。以下是我尝试过的三种选择:
string userName = "myUserName";
string password = "myPassword";
string myEbayUrl = "http://my.ebay.com/ws/eBayISAPI.dll?MyEbay&gbh=1";
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)";
string pageSource;
CookieContainer cookieContainer = new CookieContainer();
IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultCredentials;
//OPTION 1
Debug.WriteLine("OPTION 1:");
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(signInUrl);
req.CookieContainer = cookieContainer;
req.Method = method;
req.ContentType = contentType;
req.UserAgent = userAgent;
req.Proxy = proxy;
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();
//login
HttpWebResponse signInRes = (HttpWebResponse)req.GetResponse();
//loop through header items
foreach (var item in signInRes.Headers.AllKeys)
{
Debug.WriteLine(item + " : " + signInRes.Headers[item.ToString()]);
}
HttpWebRequest myEbayReq = (HttpWebRequest)HttpWebRequest.Create(myEbayUrl);
myEbayReq.CookieContainer = cookieContainer;
myEbayReq.Method = method;
myEbayReq.ContentType = contentType;
myEbayReq.UserAgent = userAgent;
myEbayReq.AllowAutoRedirect = false;
myEbayReq.Proxy = proxy;
//get MyEbay page behind login
using (StreamReader sr = new StreamReader(myEbayReq.GetResponse().GetResponseStream()))
{
pageSource = sr.ReadToEnd();
}
savePage(pageSource, "option1");
//OPTION 2 without cookiecontainer
Debug.WriteLine("OPTION 2:");
string cookieHeader;
WebRequest request = WebRequest.Create(signInUrl);
request.ContentType = contentType;
request.Method = method;
request.Proxy = proxy;
byte[] bytes = Encoding.ASCII.GetBytes(postData);
request.ContentLength = bytes.Length;
using (Stream os = request.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
//login
WebResponse loginResp = request.GetResponse();
cookieHeader = loginResp.Headers["Set-cookie"];
//loop through header items
foreach (var item in loginResp.Headers.AllKeys)
{
Debug.WriteLine(item + " : " + loginResp.Headers[item.ToString()]);
}
//get MyEbay page behind login
WebRequest getRequest = WebRequest.Create(myEbayUrl);
getRequest.Proxy = proxy;
getRequest.Headers.Add("Cookie", cookieHeader);
WebResponse getResponse = getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
}
savePage(pageSource, "option2");
//OPTION 3 using derived web client class
Debug.WriteLine("OPTION 3:");
using (var client = new LoginWebClient())
{
client.Proxy = proxy;
var values = new NameValueCollection
{
{ "userid", userName },
{ "pass", password },
};
// login
client.UploadValues(signInUrl, values);
//loop through header items
foreach (var item in client.ResponseHeaders.AllKeys)
{
Debug.WriteLine(item +" : "+client.ResponseHeaders[item.ToString()]);
}
//get MyEbay page behind login
pageSource = client.DownloadString(myEbayUrl);
}
savePage(pageSource,"option3");派生的web客户端类:
public class LoginWebClient : WebClient
{
public CookieContainer CookieContainer { get; private set; }
public LoginWebClient()
{
CookieContainer = new CookieContainer();
}
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = CookieContainer;
}
return request;
}
}这里是我得到的响应头值:
备选案文1:
GMT;Path=/,dp1=bpbf/%2344e96da9a^u1p/QEBfX0BAX19AQA**4e96da9a^;Domain=.ebay.com;Path=/,GMT;Path=/,cssg=a594ba2b12b0a040b12546d5ffc24e56;Domain=.ebay.com;Path=/,s=CgAD4ACBMtviaYTU5NGJhMmIxMmIwYTA0MGIxMjU0NmQ1ZmZjMjRlNTblRK7X;Domain=.ebay.com;Path=/;HttpOnly,nonsession=CgADKACBWG6iaYTU5NGJhMmIxMmIwYTA0MGIxMjU0NmQ1ZmZjMjRlNTYAywABTLWuIjE5937t;Domain=.ebay.com;Expires=Thu,2011年10月13日-2011年12:33:30;Path=/ Server : Apache-Coyote/1.1缓存-控制:私有Pragma :无缓存内容-Type: text/html;charset=UTF-8内容-长度: 16980 Date : Wed,2010年10月13日12:33:30格林尼治时间
备选案文2:
GMT:=/,cssg=a594c8cf12b0a02662265926ffc9c4a3;Domain=.ebay.com;Path=/,s=CgAD4ACBMtvidYTU5NGM4Y2YxMmIwYTAyNjYyMjY1OTI2ZmZjOWM0YTM4WWlH;Domain=.ebay.com;Path=/,nonsession=CgADKACBWG6idYTU5NGM4Y2YxMmIwYTAyNjYyMjY1OTI2ZmZjOWM0YTMAywABTLWuJTFk6RbF;Domain=.ebay.com;Expires=Thu,2011年10月13日至10月12日:33:33 GMT;Path=/,cssg=a594c8cf12b0a02662265926ffc9c4a3;Domain=.ebay.com;Path=/,s=CgAD4ACBMtvidYTU5NGM4Y2YxMmIwYTAyNjYyMjY1OTI2ZmZjOWM0YTM4WWlH;Domain=.ebay.com;Path=/,nonsession=CgADKACBWG6idYTU5NGM4Y2YxMmIwYTAyNjYyMjY1OTI2ZmZjOWM0YTMAywABTLWuJTFk6RbF;Domain=.ebay.com;Expires=Thu,2011年10月13日至10月12日:33:33 GMT;Path=/ Server : Apache-Coyote/1.1缓存-控制:私有Pragma :无缓存内容-Type: text/html;charset=UTF-8内容长度: 16979 Date : Wed,2010年10月13日12:33:32格林尼治标准时间
备选方案3:
连接:保持活动代理-连接:保持活动内容-长度: 8154内容-类型: text/html日期:2010年10月13日12:33:37 GMT ETag : cd78002149191b683d4d0b3c98f6d5e3最近修改:55,2010年10月13日11:55:34 GMT服务器: Apache-Coyote/1.1经: 1.1土星
和这里是Fiddler显示的响应头值:
Set-Cookie: ds1=ats/1286972903913;Domain=.ebay.com;Path=/
Set-Cookie: ds2=alss/0.4cb6f76c^;Domain=.ebay.com;Path=/ /
Cookie: ebay=%5Elrtjs%3D2.6%5EsfLMD%3D0%5Esbf%3D%23a0000000004%5Ecos%3D-7%5Ecv%3D15555%5Elvmn%3D0%7C0%7C%5Esin%3Din%5Ejs%3D1%5E;Domain=.ebay.com;Path=/
Set-Cookie: Domain=.ebay.com;Expires=Fri,2012年10月12日12:28:28 GMT;Path=/
ns1=BAQAAASuJIxIvAAaAANgAXk6W2WxmMDAwYzg2fDYwMV4xMjg2ODg2ODY3MTQ4XmMybHlMbkp2ZEdoelkyaHBiR1E9XjFeM3wyfDY1fDV8NHw3XjFeMl40XjJeMTJeMTJeMl4xXjFeMF4xXjBeMV44MTMxAKUAGE6W2Ww2NzQ2MTc1Mi8wOzEwMjU4MTk5NjkvMDtugrDk7fid3JLf0Q9Jz19v95p6vg*;Domain=.ebay.com;Expires=Thu,13-2011年10月12日:28:28格林尼治标准时间;路径=/;HttpOnly
Set-Cookie: cssg=a4e34c0312b0a02694e761b7fff52ae9;Domain=.ebay.com;Path=/
Set-Cookie: HttpOnly
Set-Cookie: Domain=.ebay.com;Expires=Thu,2011年10月13日12:28 GMT;Path=/
Cookie: secses=BAQAAASuJIxIvAAaAAUsAF06W2Ww0Y2I1YTVlYy4wLjEuMi40MS4xLjAuMxdf9sPE5nmJj5E24Fy2hCJhKoHt;Domain=.ebay.com;Path=/
Set-Cookie: lucky9=6866724;Domain=.ebay.com;Expires=Mon,12:28 GMT;Path=/
发布于 2010-10-13 13:26:14
我同意使用API的答案。如果您想要做的一切都是在代码中,那么这是最好的方法。
但是,如果您对打开浏览器窗口感兴趣,那么我建议您使用硒。这是针对web可用性测试,但您可以使用它打开浏览器窗口,键入和提交表单字段。
例如:
var selenium = new DefaultSelenium(host,port,browserString,startUrl);
selenium.Open("http://www.site.com");
selenium.Type("username","myusername");
selenium.Type("password","mypassword");
selenium.Click("submit");可能很有用(除非您试图返回标头,等等)。此外,您还可以使用标准的XPath等来获取页面上的元素数组,并在C#代码中执行所需的操作。
https://stackoverflow.com/questions/3923966
复制相似问题