Chashitsu

http request c#

Dec 27th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //Request library
  7. using System.Net;
  8. using System.IO;
  9.  
  10. namespace TestApplication
  11. {
  12.     class Connect
  13.     {
  14.         public string id;
  15.         public string type;
  16.  
  17.         protected string api = "https://api.stackexchange.com/2.2/";
  18.         protected string options = "?order=desc&sort=name&site=stackoverflow";
  19.  
  20.         public string request()
  21.         {
  22.             string totalUrl = this.join(id);
  23.  
  24.             return this.HttpGet(totalUrl);
  25.         }
  26.  
  27.         protected string join(string s)
  28.         {
  29.             return api + type + "/" + s + options;
  30.         }
  31.  
  32.         protected string get(string url)
  33.         {
  34.             try
  35.             {
  36.                 string rt;
  37.  
  38.                 WebRequest request = WebRequest.Create(url);
  39.  
  40.                 WebResponse response = request.GetResponse();
  41.  
  42.                 Stream dataStream = response.GetResponseStream();
  43.  
  44.                 StreamReader reader = new StreamReader(dataStream);
  45.  
  46.                 rt = reader.ReadToEnd();
  47.  
  48.                 Console.WriteLine(rt);
  49.  
  50.                 reader.Close();
  51.                 response.Close();
  52.  
  53.                 return rt;
  54.             }
  55.  
  56.             catch(Exception ex)
  57.             {
  58.                 return "Error: " + ex.Message;
  59.             }
  60.         }
  61.         public string HttpGet(string URI)
  62.         {
  63.             WebClient client = new WebClient();
  64.  
  65.             // Add a user agent header in case the
  66.             // requested URI contains a query.
  67.  
  68.             client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
  69.  
  70.             Stream data = client.OpenRead(URI);
  71.             StreamReader reader = new StreamReader(data);
  72.             string s = reader.ReadToEnd();
  73.             data.Close();
  74.             reader.Close();
  75.  
  76.             return s;
  77.         }
  78.     }
  79. }
Add Comment
Please, Sign In to add comment