How do you add this big header in POST request in C#?

时间:2017-10-12 09:49:13

标签: c# post header

I've found quite a lot of answers with regards to my question yet I am sure that I am doing something wrong because none really works and I just want to pass my own cookie session as well as the token and some other information from header when sending a POST request to server. This is being executed when button is pressed on the application:

    private void button1_Click(object sender, EventArgs e)
    {
        PostRequest("https://www.example.com");
    }

--

    async static void PostRequest(string url)
    {
        //PASS HEADER HERE


        //POST DATA
        IEnumerable<KeyValuePair<string, string>> queries = new List<KeyValuePair<string, string>>()
        {
            new KeyValuePair<string, string>("data1": "name"),
            new keyvaluepair<string, string>("data2": "555")
        };

        HttpContent q = new FormUrlEncodedContent(queries);

        using (HttpClient client = new HttpClient()) //using for disposing if not in use
        {
            using (HttpResponseMessage response = await client.PostAsync(url, q)) //url, content in postAsync
            {
                using (HttpContent content = response.Content) //store content (data) into a 'content'
                {
                    string mycontent = await content.ReadAsStringAsync();
                    //HttpContentHeaders headers = content.Headers;
                    Console.WriteLine(mycontent);
                }
            }
        }
    }

-- This is the cookie I want to pass and and the token along with other info in header:

POST /test/data HTTP/1.1

Host: host.datanet.com

User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0

Accept: */*

Accept-Language: en-GB,en;q=0.5

Accept-Encoding: gzip, deflate, br

Content-Type: application/json

X-XSRF-TOKEN: eyJpdiI6ImxXMHNNQ01RT0RHTEMreU9lc0duZWc9PSIsInZhbHVlIjoiZ08zMFJDaTlZV1h5ZHlVRDBzUkFNNXBlY0ZuTFlCQ2V0SlVpRlpJRUUrRUl0NlwvUE1zN1Vza2F5blhDTStNM2NlM05VNXFZc1IxclNPSHhkNkU5Mmt3PT0iLCJtYWMiOiJmMGQxZTg5YmRlYjNmMzNjMWZlN2ZlM2E5YmE1NzRiMTgyMDQ5NDY1ZGZjNjliMDJlNDA2MGQzMjc3MjU2MzE2In0=

Referer: https://refererwebsitetest.html

Content-Length: 61

Cookie: session=eyJpdiI6IkI4XC9ndnFr6UlwvbEZMWhIK1wvbTFRSkE9PSIsInZhbHVlIjoicStyR2c3T240UW41RW5OdEFtcM2NjMjJ2V2d3Q0Yk9cL0tjZEFyV1NwSmpDbmdyc3BGZklva3RhaFJ5SWFCOFdiOHhobE1ySlU3NnJhMXMrckMzeXkxOiI1MjQxMkZuTndnPT0iLCJtYWMiY2VjMDVmiODE4YjI2YzViMjdiYzQ4MTk1ZDllMDVjOGE0MGRkMTFiYWFkNDRlZjM1MGQ4YzZjIn0%3D; XSRF-TOKEN=eyJpdiI6ImxXMHNNQ01RT0RHTEMreU9lc0duZWc9PSIsInZhbHVlIjoiZ08zMFJDaTlZV1h5ZHlVRDBzUkFNNXBlY0ZuTFlCQ2V0SlVpRlpJlwvUE1zN1Vza2F5blhDTStNM2NlM05VNXFZc1IxclNPSHhkNkU5Mmt3PT0iLCJtYWMiOiJmMGQxZTg5YmRlYjNmMzNjMWZlN2ZlM2E5YmE1NzRiMTgyMDQ5NDY1ZGZjNjliMDJlNDA2MGQzMjc3MjU2MzE2In0%3D

Connection: keep-alive

I am really a beginner in C# and would appreciate any help. Thank you!


Updated code:

async static void PostRequest(string url)
    {
        //PASS HEADER DATA
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);

        //Repeat for each header you want
        request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
        request.Headers.Add("Content-Type", "application/json");
        request.Headers.Add("X-XSRF-TOKEN", "eyJpdiI6ImxXMHNNQ01RT0RHTEMreU9lc0duZWc9PSIsInZhbHVlIjoiZ08zMFJDaTlZV1h5ZHlVRDBzUkFNNXBlY0ZuTFlCQ2V0SlVpRlpJRUUrRUl0NlwvUE1zN1Vza2F5blhDTStNM2NlM05VNXFZc1IxclNPSHhkNkU5Mmt3PT0iLCJtYWMiOiJmMGQxZTg5YmRlYjNmMzNjMWZlN2ZlM2E5YmE1NzRiMTgyMDQ5NDY1ZGZjNjliMDJlNDA2MGQzMjc3MjU2MzE2In0=");
        request.Headers.Add("Referer", "https://refererwebsitetest.html");
        request.Headers.Add("Content-Length", "61");
        request.Headers.Add("Cookie", "session=eyJpdiI6IkI4XC9ndnFr6UlwvbEZMWhIK1wvbTFRSkE9PSIsInZhbHVlIjoicStyR2c3T240UW41RW5OdEFtcM2NjMjJ2V2d3Q0Yk9cL0tjZEFyV1NwSmpDbmdyc3BGZklva3RhaFJ5SWFCOFdiOHhobE1ySlU3NnJhMXMrckMzeXkxOiI1MjQxMkZuTndnPT0iLCJtYWMiY2VjMDVmiODE4YjI2YzViMjdiYzQ4MTk1ZDllMDVjOGE0MGRkMTFiYWFkNDRlZjM1MGQ4YzZjIn0%3D; XSRF-TOKEN=eyJpdiI6ImxXMHNNQ01RT0RHTEMreU9lc0duZWc9PSIsInZhbHVlIjoiZ08zMFJDaTlZV1h5ZHlVRDBzUkFNNXBlY0ZuTFlCQ2V0SlVpRlpJlwvUE1zN1Vza2F5blhDTStNM2NlM05VNXFZc1IxclNPSHhkNkU5Mmt3PT0iLCJtYWMiOiJmMGQxZTg5YmRlYjNmMzNjMWZlN2ZlM2E5YmE1NzRiMTgyMDQ5NDY1ZGZjNjliMDJlNDA2MGQzMjc3MjU2MzE2In0%3D");
        request.Headers.Add("Connection", "keep-alive");


        using (HttpClient client = new HttpClient())
        {
            await client.SendAsync(request);
        }

        //POST DATA
        IEnumerable<KeyValuePair<string, string>> queries = new List<KeyValuePair<string, string>>()
        {
            new KeyValuePair<string, string>("data1": "name"),
            new KeyValuePair<string, string>("data2": "555")
        };

        HttpContent q = new FormUrlEncodedContent(queries);

        using (HttpClient client = new HttpClient()) //using for disposing if not in use
        {
            using (HttpResponseMessage response = await client.PostAsync(url, q)) //url, content in postAsync
            {
                using (HttpContent content = response.Content) //store content (data) into a 'content'
                {
                    string mycontent = await content.ReadAsStringAsync();
                    //HttpContentHeaders headers = content.Headers;
                    Console.WriteLine(mycontent);
                }
            }
        }
    }

it comes up with the following errors:

First error

Second error

// FIXED

applied the code from Microsoft website: https://docs.microsoft.com/en-us/dotnet/framework/network-programming/how-to-send-data-using-the-webrequest-class

lastly added headers from your example, THX works!

1 个答案:

答案 0 :(得分:0)

您可以使用HttpRequestMessage类并设置所需的标头,然后使用HttpClient上的SendAsync方法将其作为POST发送。

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);

//Repeat for each header you want
request.Headers.Add("headerName","headervalue");

using (HttpClient client = new HttpClient()) 
{
     await client.SendAsync(request); 
}