Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

c# - Why can I not save response headers, for the use in future requests as request headers?

I am trying to post a data to a website, this post request needs some headers set. I mimic the behaviour with HTTP requests, and I can see that their server responds with these needed headers. However I can not figure out how to add these response headers to my WebHeaderCollection, which I am storing them into.

Below I have pasted my code, that GET's the right pages, to aquire the needed response headers, I try to add them to my WebHeaderCollection, by: headers.Add(resp.Headers); I print the headers after each request is sent, and I see that the correct headers are being responded with, from the server, but they aren't getting stored in my headercollection. For information I am storing cookies this way, and that works.

using System;
using System.Net;
using System.Threading;

namespace PostWithCookies
{
    internal class FLAPIPOST
    {
        private static void Main(string[] args)
        {
            CookieContainer myCookies = new CookieContainer();

            // Obtain the WebHeaderCollection instance containing the header name/value pair from the response.
            WebHeaderCollection myWebHeaderCollection = new WebHeaderCollection();

            string src = HttpMethods.Get("https://footlocker.dk", "https://footlocker.dk", ref myCookies, ref myWebHeaderCollection);

            HttpMethods.PrintCookies(ref myCookies);
            Thread.Sleep(1000);

            HttpMethods.Get("https://www.footlocker.dk/built/137/app.client.js", "https://www.footlocker.dk/", ref myCookies, ref myWebHeaderCollection);
            Thread.Sleep(1000);

            HttpMethods.Get("https://www.footlocker.dk/built/137/41.client.js", "https://www.footlocker.dk/", ref myCookies, ref myWebHeaderCollection);
            Thread.Sleep(1000);

            HttpMethods.Get("https://www.footlocker.dk/built/137/9.client.js", "https://www.footlocker.dk/", ref myCookies, ref myWebHeaderCollection);
            Thread.Sleep(1000);

            HttpMethods.Get("https://www.footlocker.dk/built/137/19.client.js", "https://www.footlocker.dk/", ref myCookies, ref myWebHeaderCollection);
            Thread.Sleep(1000);

            HttpMethods.Get("https://www.footlocker.dk/built/137/1.client.js", "https://www.footlocker.dk/", ref myCookies, ref myWebHeaderCollection);
            Thread.Sleep(1000);

            HttpMethods.Get("https://www.footlocker.dk/built/137/4.client.js", "https://www.footlocker.dk/", ref myCookies, ref myWebHeaderCollection);
            Thread.Sleep(1000);

            HttpMethods.Get("https://www.footlocker.dk/built/137/5.client.js", "https://www.footlocker.dk/", ref myCookies, ref myWebHeaderCollection);



            Thread.Sleep(3000);

            src = HttpMethods.Get("https://www.footlocker.dk/api/session", "https://www.footlocker.dk/", ref myCookies, ref myWebHeaderCollection);

            Thread.Sleep(3000);

            HttpMethods.PrintCookies(ref myCookies);

            Thread.Sleep(2000);

            Console.WriteLine(src);

            Console.WriteLine(myWebHeaderCollection.AllKeys);

            // Post a product to our card, with previous cookies, and headers:

            string dataToSend = "{
  "productQuantity": 1,
  "productId": "SIZE_1096403"
}";

            string addedToCart = HttpMethods.Post("https://www.footlocker.dk/api/users/carts/current/entries", dataToSend, 
                                 "https://www.footlocker.dk/en/product/Nike-Tuned-2---Men-Shoes/314206149404.html", ref myCookies, ref myWebHeaderCollection);

            Console.WriteLine("Added to cart?");
            Console.WriteLine(addedToCart);


        }
    }
}

I will post my methods for the get requests, and the post requests:

using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text;

namespace PostWithCookies
{
    internal class HttpMethods
    {
        public static string Get(string url, string referer, ref CookieContainer cookies, ref WebHeaderCollection headers)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "GET";
            req.CookieContainer = cookies;
            req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0";
            req.Referer = referer;

            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            cookies.Add(resp.Cookies);
            headers.Add(resp.Headers);            

            string pageSrc;
            using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
            {
                pageSrc = sr.ReadToEnd();
            }
            Console.WriteLine(resp.StatusCode);
            Console.WriteLine(resp.Headers);
            return pageSrc;
        }

        public static string Post(string url, string postData, string referer, ref CookieContainer cookies, ref WebHeaderCollection headers)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.CookieContainer = cookies;
            req.Referer = referer;
            req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0";
            req.ContentType = "application/json";
            req.Accept = "application/json";

            Stream postStream = req.GetRequestStream();
            byte[] postBytes = Encoding.ASCII.GetBytes(postData);
            postStream.Write(postBytes, 0, postBytes.Length);
            postStream.Dispose();

            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

            cookies.Add(resp.Cookies);
            headers.Add(resp.Headers);

            StreamReader sr = new StreamReader(resp.GetResponseStream());
            string pageSrc = sr.ReadToEnd();
            sr.Dispose();
            return pageSrc;
        }        
    }
}
question from:https://stackoverflow.com/questions/65919125/why-can-i-not-save-response-headers-for-the-use-in-future-requests-as-request-h

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...