Guest User

Untitled

a guest
Nov 17th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1.         private void HandleRequest(string url, string method = "GET", string data = null)
  2.         {
  3.             HttpWebRequest req = WebRequest.Create(new Uri(url)) as HttpWebRequest;
  4.             req.Method = method.ToUpper();
  5.             req.Accept = "application/xml";
  6.             req.ContentType = "application/xml";
  7.             req.AllowAutoRedirect = true;
  8.             string authInfo = Convert.ToBase64String(Encoding.GetEncoding(1252).GetBytes("yourapi:keyhere"));
  9.             req.Headers.Add(HttpRequestHeader.Authorization, string.Format("Basic {0}", authInfo));
  10.  
  11.             if (! string.IsNullOrEmpty(data))
  12.             {
  13.                 byte[] rawBytes = Encoding.GetEncoding(1252).GetBytes(data);
  14.                 req.ContentLength = rawBytes.Length;
  15.                 using (Stream reqStream = req.GetRequestStream())
  16.                 {
  17.                     reqStream.Write(rawBytes, 0, rawBytes.Length);
  18.                 }
  19.             }
  20.  
  21.             using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
  22.             {
  23.                 using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
  24.                 {
  25.                     Console.WriteLine(reader.ReadToEnd());
  26.                 }
  27.             }
  28.         }
Add Comment
Please, Sign In to add comment