0

I'm trying to log into Amazon Seller Central account and then return the HTML from the orders page (which requires a login). My streamreader is returning null, so something must be wrong with my code or my idea of doing this is flawed.

In the first section of this code before the ConsoleReadline(). I'm POST my username and password and then I receive a cookie, which shows up in the console, but I believe, I've got the 2nd part wrong when I try to retrieve the HTML of the next URI string.

CODE

using System;
using System.IO;
using System.Text;
using System.Net;


namespace HandleWebRequest
{
    class Program
    {
        static void Main(string[] args)
        {
            string loginUri = "https://sellercentral.amazon.com/gp/homepage.html?";
            string orderUri =
                "https://sellercentral.amazon.com/gp/orders-v2/list/ref=ag_myo_wos3_home?byDate=shipDate&statusFilter=ItemsToShip&searchFulfillers=mfn&ignoreSearchType=1&searchType=OrderStatus&_encoding=UTF8&searchDateOption=preSelected&sortBy=OrderStatusDescending&shipSearchDateOption=noTimeLimit";
            string username = "myusernmae";
            string password = "mypassword";
            string reqString = "username=" + username + "&password=" + password;
            byte[] requestData = Encoding.UTF8.GetBytes(reqString);

            CookieContainer cc = new CookieContainer();
            var request = (HttpWebRequest) WebRequest.Create(loginUri);
            request.Proxy = null;
            request.CookieContainer = cc;
            request.Method = "POST";

            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = requestData.Length;
            using (Stream s = request.GetRequestStream())
                s.Write(requestData, 0, requestData.Length);

            using (var response = (HttpWebResponse)request.GetResponse())
                foreach (Cookie c in response.Cookies)
                {
                    Console.WriteLine(c.Name + " = " + c.Value);
                    Console.WriteLine(c.Domain);
                }

            Console.ReadLine();

            request = (HttpWebRequest) WebRequest.Create(orderUri);
            request.CookieContainer = cc;
            using (WebResponse response = request.GetResponse())
            using (Stream s = response.GetResponseStream())
            using (StreamReader sr = new StreamReader(s))
                File.WriteAllText("code.html", sr.ReadToEnd());

            System.Diagnostics.Process.Start("code.html");
        }
    }
}
3
  • does it work if you print the output to console, instead of writing to file? In other words, are you even getting an output from the server at all? maybe the content length is zero?
    – feroze
    Feb 12, 2016 at 4:51
  • response is a new object; it doesn't just inherit the request's cookie container. You need to feed the cookies from response.Cookies into your CookieContainer, otherwise the next request won't have them.
    – Nyerguds
    Apr 12, 2016 at 10:00
  • On a related note, HttpWebResponse isn't IDisposable, so there is no use putting that inside a Using statement.
    – Nyerguds
    Apr 12, 2016 at 10:07

1 Answer 1

0

response is a new object; it doesn't just inherit the cookie container you put into request. You need to feed the cookies from response.Cookies into your CookieContainer, otherwise the next request won't have them:

foreach (Cookie c in response.Cookies)
    cc.Add(new Uri(this.m_HttpWebResponse.ResponseUri.Scheme + "://" + c.Domain), c);
request = (HttpWebRequest) WebRequest.Create(orderUri);
request.CookieContainer = cc;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.