Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. public class Session
  2. {
  3. private readonly static Lazy<Session> _instance = new Lazy<Session>(()=>new Session());
  4. public static Session Instance { get { return _instance.Value; } }
  5.  
  6. private Session()
  7. {
  8. }
  9.  
  10. public string Token { get; private set; }
  11.  
  12. public void SetToken(string token)
  13. {
  14. Token = token;
  15. }
  16. }
  17.  
  18. public class Test
  19. {
  20. public async Task<string> Post(string uri, string data)
  21. {
  22. var request = (HttpWebRequest)WebRequest.Create(uri);
  23. request.Method = "POST";
  24. request.ContentType = "application/json";
  25.  
  26. var buffer = Encoding.UTF8.GetBytes(data);
  27. request.ContentLength = buffer.Length;
  28.  
  29. var stream = await request.GetRequestStreamAsync();
  30. await stream.WriteAsync(buffer, 0, buffer.Length);
  31. stream.Close();
  32.  
  33. var response = await request.GetResponseAsync();
  34. stream = response.GetResponseStream();
  35. if(stream == null)
  36. throw new ArgumentNullException();
  37.  
  38. var reader = new StreamReader(stream);
  39. var result = await reader.ReadToEndAsync();
  40.  
  41.  
  42. reader.Close();
  43. stream.Close();
  44. response.Close();
  45. return result;
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement