开发者

Log in to Amazon seller central using .NET HttpWebRequest

开发者 https://www.devze.com 2023-01-05 22:47 出处:网络
I need to scrape order information from our Amazon seller central account so I am trying to access it using HttpWebRequest from a .NET forms app. I know it is not that hard to get logged in because if

I need to scrape order information from our Amazon seller central account so I am trying to access it using HttpWebRequest from a .NET forms app. I know it is not that hard to get logged in because if I open this local HTML in IE:

<html>
<body>
<form action="https://sellercentral.amazon.co.uk/gp/sign-in/sign-in.html/ref=ag_login_lgin_myo" method="post" name="signin">
    <input type="hidden" name="protocol" value="https" />
    <input type="hidden" name="action" value="sign-in" />
    <input type="text" name="email" value="myemail@domain.com"/>
    <input type="password" name="password" value="xxxxxx"/>
    <input type="submit" name="sign-in-button"/>
</form>
</body>
</html>

and submit I successfully get the logged in Amazon home page back. However I can't get this to work via code, I always get the login page back again instead, here is the code:

string sUrl = "https://sellercentral.amazon.co.uk/gp/sign-in/s开发者_运维知识库ign-in.html/ref=ag_login_lgin_myo";
string sPostData = "";
sPostData += "protocol=https";
sPostData += "&action=sign-in";
sPostData += "&email=myemail@domain.com";
sPostData += "&password=xxxxxx";
sPostData += "&sign-in-button=";

// initialise request object
HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(sUrl);
oRequest.Timeout = 30000;

// set fake headers
oRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1)";

// set the method & content type
oRequest.Method = "POST";
oRequest.ContentType = "application/x-www-form-urlencoded";

// prepare post data
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byteArr = encoding.GetBytes(sPostData);

// write to request
oRequest.ContentLength = byteArr.Length;
Stream reqStream = oRequest.GetRequestStream();
reqStream.Write(byteArr, 0, byteArr.Length);
reqStream.Close();

// fetch the page
HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();

// convert response to a string
StreamReader sr = new StreamReader(oResponse.GetResponseStream());
string responseHTML = sr.ReadToEnd().ToLower();
sr.Close();

Any ideas what I'm doing wrong? I guess there must be something different about my HttpWebRequest submit compared to the form submit via IE which Amazon is rejecting but I can't figure out what? Any help much appreciated - thanks.


Have you looked into Amazon Web Services ?

They also have a C# library

If you still insist on doing it your way, try setting a CookieContainer() on the request.

0

精彩评论

暂无评论...
验证码 换一张
取 消