I am trying to log into my EBay account using C#. I have looked at various posts and found the following 3 options. Unforunately, none of them are working. I am also listing the response header values that I get and that Fiddler shows when doing a login. There is a difference as I only get one "Set-cookie" value and Fiddler shows 10 "Set-Cookie" values. Where am I going wrong? Also, why do I only get one "Set-Cookie" value? Very grateful if anyone can share a working solution. Here are the 3 options that I tried:
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");
The derived web client class:
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;
}
}
Here are the response header values I get:
OPTION 1:
Set-Cookie : ebay=%5Esbf%3D%23%5E; Domain=.ebay.com; Path=/,dp1=bpbf/%2344e96da9a^u1p/QEBfX0BAX19AQA**4e96da9a^; Domain=.ebay.com; Expires=Fri, 12-Oct-2012 12:33:30 GMT; Path=/,cssg=a594ba2b12b0a040b12546d5ffc24e56; Domain=.ebay.com; Path=/,s=CgAD4ACBMtviaYTU5NGJhMmIxMmIwYTA0MGIxMjU0NmQ1ZmZjMjRlNTblRK7X;Domain=.ebay.com;Path=/; HttpOnly,nonsession=CgADKACBWG6iaYTU5NGJhMmIxMmIwYTA0MGIxMjU0NmQ1ZmZjMjRlNTYAywABTLWuIjE5937t; Domain=.ebay.com; Expires=Thu, 13-Oct-2011 12:33:30 GMT; Path=/
Server : Apache-Coyote/1.1
Cache-Control : private
Pragma : no-cache
Content-Type : text/html;charset=UTF-8
Content-Length : 16980
Date : Wed, 13 Oct 2010 12:33:30 GMT
OPTION 2:
Set-Cookie : ebay=%5Esbf%3D%23%5E; Domain=.ebay.com; Path=/,dp1=bpbf/%2344e96da9d^u1p/QEBfX0BAX19AQA**4e96da9d^; Domain=.ebay.com; Expires=Fri, 12-Oct-2012 12:33:33 GMT; Path=/,cssg=a594c8cf12b0a02662265926ffc9c4a3; Domain=.ebay.com; Path=/,s=CgAD4ACBMtvidYTU5NGM4Y2YxMmIwYTAyNjYyMjY1OTI2ZmZjOWM0YTM4WWlH; Domain=.ebay.com; Path=/,nonsession=CgADKACBWG6idYTU5NGM4Y2YxMmIwYTAyNjYyMjY1OTI2ZmZjOWM0YTMAywABTLWuJTFk6RbF; Domain=.ebay.com; Expires=Thu, 13-Oct-2011 12:33:33 GMT; Path=/
Server : Apache-Coyote/1.1
Cache-Control : private
Pragma : no-cache
Content-Type : text/html;charset=UTF-8
Content-Length : 16979
Date : Wed, 13 Oct 2010 12:33:32 GMT
OPTION 3:
Connection : Keep-Alive
Proxy-Connection : Keep-Alive
Content-Length : 8154
Content-Type : text/html
Date : Wed, 13 Oct 2010 12:33:37 GMT
ETag : cd78002149191b683d4d0b3c98f6d5e3
Last-Modified : Wed, 13 Oct 2010 11:55:34 GMT
Server : Apache-Coyote/1.1
Via : 1.1 SATURN
And here are the response header values that Fiddler shows:
Set-Cookie: ds1=ats/1286972903913; Domain=.ebay.com; Path=/
Set-Cookie: ds2=alss/0.4cb6f76c^; Domain=.ebay.com; Path=/
Set-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: dp1=bvrvi/3%7C0%7C250707265333%7C250707988126%7C250707990497%7C4cc2d4ec^pcid/1578961514e96d96c^a1p/04cb6f76c^fm/4.3.24cdbe2b3^mpc/0%7C04cc2d4ec^pbf/%23180c20000044e96d96c^tzo/-784cb5b3fc^u1p/ZXF1aW5veDIwMTI*4e96d96c^idm/14cb83979^u1f/Ewald4e96d96c^; Domain=.ebay.com; Expires=Fri, 12-Oct-2012 12:28:28 GMT; Path=/
Set-Cookie: ns1=BAQAAASuJIxIvAAaAANgAXk6W2WxmMDAwYzg2fDYwMV4xMjg2ODg2ODY3MTQ4XmMybHlMbkp2ZEdoelkyaHBiR1E9XjFeM3wyfDY1fDV8NHw3XjFeMl40XjJeMTJeMTJeMl4xXjFeMF4xXjBeMV44MTMxAKUAGE6W2Ww2NzQ2MTc1Mi8wOzEwMjU4MTk5NjkvMDtugrDk7fid3JLf0Q9Jz19v95p6vg*;Domain=.ebay.com;Expires=Thu, 13-Oct-2011 12:28:28 GMT;Path=/; HttpOnly
Set-Cookie: cssg=a4e34c0312b0a02694e761b7fff52ae9; Domain=.ebay.com; Path=/
Set-Cookie: s=BAQAAASuJIxIvAAWAAAEAC0y292hlcXVpbm94MjAxMgASAApMtvdsdGVzdENvb2tpZQADAAVMtvdsMTYzODQA9AAiTLb3bCQyJGFQOVp0bEMzJEF3V1pTamRvVTFGYW0zdGI1aC9WZjEBRQAITpbZbDRjOTlkN2Y0AAYAAUy292wwAP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mJj5E24Fy2hCJhKoHt; Domain=.ebay.com; Path=/
Set-Cookie: lucky9=6866724; Domain=.ebay.com; Expires=Mon, 12-Oct-2015 12:28:28 GMT; Path=/
I agree with the answer about using the API. If everything you want to do is in code then this is the best way.
However, if you're interested in it opening a browser window, etc then I recommend using Selenium. This is aimed at web usability testing, but you can use it to open up browser windows, type into and submit form fields.
e.g:
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");
Might be useful (unless you're trying to get the headers returned, etc). Also, you can use standard XPath, etc to get an array of elements on the page and do what you need in the C# code.