Devo aggiungere alcune intestazioni personalizzate all’object HttpWebRequest
. Come posso aggiungere intestazione personalizzata all’object HttpWebRequest
in Windows Phone 7.
Si utilizza la proprietà Headers
con un indice stringa:
request.Headers["X-My-Custom-Header"] = "the-value";
Secondo MSDN, questo è disponibile da:
https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.headers(v=vs.110).aspx
Un semplice metodo per creare il servizio, aggiungere intestazioni e leggere la risposta JSON,
private static void WebRequest() { const string WEBSERVICE_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()); } }
È ansible aggiungere valori alla raccolta HttpWebRequest.Headers.
Secondo MSDN, dovrebbe essere supportato in windows phone: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.headers%28v=vs.95%29.aspx