使用.NET HttpWebRequest登录亚马逊卖家中心

时间:2010-07-01 19:50:53

标签: c# .net httpwebrequest amazon

我需要从我们的亚马逊卖家中央帐户中删除订单信息,因此我尝试使用.NET表单应用程序中的HttpWebRequest来访问它。我知道登录并不难,因为如果我在IE中打开这个本地HTML:

<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>

并提交我成功登录亚马逊主页。但是我不能通过代码让这个工作,我总是再次返回登录页面,这里是代码:

string sUrl = "https://sellercentral.amazon.co.uk/gp/sign-in/sign-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();

任何想法我做错了什么?我想我的HttpWebRequest提交必须有不同的东西,而不是亚马逊拒绝通过IE提交的表单,但我无法弄清楚是什么?任何帮助非常感谢 - 谢谢。

1 个答案:

答案 0 :(得分:0)

你有没有看过Amazon Web Services

他们也有C# library

如果您仍坚持按照自己的方式进行操作,请尝试在请求中设置CookieContainer()。

相关问题