Add custom header in HttpWebRequest
Asked Answered
L

4

106

I need to add some custom headers to the HttpWebRequest object. How can I add Custom Header to HttpWebRequest object in Windows Phone 7.

Libidinous answered 15/12, 2011 at 12:0 Comment(0)
D
199

You use the Headers property with a string index:

request.Headers["X-My-Custom-Header"] = "the-value";

According to MSDN, this has been available since:

  • Universal Windows Platform 4.5
  • .NET Framework 1.1
  • Portable Class Library
  • Silverlight 2.0
  • Windows Phone Silverlight 7.0
  • Windows Phone 8.1

https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.headers(v=vs.110).aspx

Denverdeny answered 15/12, 2011 at 12:5 Comment(4)
also the string can be replaced with enum HttpRequestHeader like this httpWebRequest.Headers[HttpRequestHeader.Authorization] = "value";Lubbi
That would not answer the original question, @Lubbi . The HttpRequestHeader enumeration is for standard headers – not for custom ones.Denverdeny
there is a property called name in google drive file upload api, which should be sent via post method. So, request.Headers["name"] = "hello.txt"; So, its not reflecting. Any Help What should we use for custom ones?Gavingavini
POST data is not sent through Headers, so this is not the right place for your question. Please post a new question.Denverdeny
N
24

A simple method of creating the service, adding headers and reading the JSON response,

private static void WebRequest()
    {
        const string WEBSERVICE_URL = "<<Web service URL>>";
        try
        {
            var webRequest = System.Net.WebRequest.Create(WEBSERVICE_URL);
            if (webRequest != null)
            {
                webRequest.Method = "GET";
                webRequest.Timeout = 12000;
                webRequest.ContentType = "application/json";
                webRequest.Headers.Add("Authorization", "Basic dchZ2VudDM6cGFdGVzC5zc3dvmQ=");

                using (System.IO.Stream s = webRequest.GetResponse().GetResponseStream())
                {
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(s))
                    {
                        var jsonResponse = sr.ReadToEnd();
                        Console.WriteLine(String.Format("Response: {0}", jsonResponse));
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
Nationality answered 25/5, 2016 at 7:46 Comment(1)
Authorization is not a custom header, and should be handled in a more controlled way.Denverdeny
R
2

You can add values to the HttpWebRequest.Headers collection.

According to MSDN, it should be supported in windows phone: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.headers%28v=vs.95%29.aspx

Riha answered 15/12, 2011 at 12:6 Comment(0)
O
2

First of all you need to visit the web page from where you are trying to fetch response. Right Click>Inspect>Network>(Refresh it)> Under Name Click on the first Link>Now you can see the Request Headers and Response Headers

From there you you can see the Request Headers and add them accordingly Like an example:

HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(url);

        HttpWReq.Method = "GET";
        HttpWReq.Headers.Add("accept-encoding", "gzip, deflate, br");
        HttpWReq.Headers.Add("cache-control", "max-age=0");
         HttpWReq.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36";
        HttpWReq.Headers.Add("accept-encoding", "gzip, deflate, br");
        HttpWReq.Headers.Add("accept-language", "en-US,en;q=0.9");
        HttpWReq.Headers.Add("cache-control", "max-age=0");
        HttpWReq.Headers.Add("upgrade-insecure-requests", "1");
Okra answered 13/9, 2021 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.