Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.51 KB | None | 0 0
  1. // MAIN METHOD
  2. public void GetTags (string applicationCode, string HWID, Action <string> callback)
  3. {
  4.     const string applicationProperty = "application";
  5.     const string hwidProperty = "hwid";
  6.     JObject json = new JObject (new JProperty (applicationProperty, applicationCode), new JProperty (hwidProperty, HWID));
  7.  
  8.     const string getTagsAction = "getTags";
  9.     CallUri (getTagsAction, json, callback);
  10. }
  11.  
  12. private void CallUri (string action, JObject data, Action <string> callback)
  13. {
  14.     string uriString = string.Format ("https://cp.pushwoosh.com/json/1.3/{0}", action);
  15.     Uri url = new Uri (uriString);
  16.  
  17.     const string requestProperty = "request";
  18.     JObject json = new JObject (new JProperty (requestProperty, data));
  19.  
  20.     DoPostRequest (url, json, callback);
  21. }
  22.  
  23. private void DoPostRequest (Uri url, JObject data, Action <string> callback)
  24. {
  25.     HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create (url);
  26.     req.ContentType = "text/json";
  27.     req.Method = "POST";
  28.  
  29.     using (var streamWriter = new StreamWriter (req.GetRequestStream ())) {
  30.         streamWriter.Write (data.ToString ());
  31.     }
  32.  
  33.     HttpWebResponse httpResponse;
  34.  
  35.     try {
  36.         httpResponse = (HttpWebResponse) req.GetResponse ();
  37.     }
  38.     catch (Exception exc) {
  39.         throw new Exception (string.Format ("Problem with {0}, {1}", url, exc.Message));
  40.     }
  41.  
  42.     using (var streamReader = new StreamReader (httpResponse.GetResponseStream ())) {
  43.         var responseText = streamReader.ReadToEnd ();
  44.  
  45.         callback (responseText);
  46.  
  47.         Debug.LogFormat ("responseText: {0}", responseText);
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement