Add custom header to HttpWebRequest

I need to add some custom headers to an HttpWebRequest object. How to add a custom title to an HttpWebRequest object in Windows Phone 7.

+54
c # header windows-phone-7
Dec 15 '11 at 12:00
source share
3 answers

You are using the Headers property with a string index:

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

According to MSDN, it was available with:

  • Universal Windows 4.5 Platform
  • .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

+115
Dec 15 '11 at 12:05
source

An easy way to create a service, add headers and read a 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()); } } 
+10
May 25 '16 at 7:46 a.m.
source

You can add values ​​to the HttpWebRequest.Headers collection.

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

+2
Dec 15 '11 at 12:06
source



All Articles