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