Exemple #1
0
        public async static Task <string> Call(APITypes type, T item)
        {
            string     URL        = APIExtensions.getURL(type);
            HttpClient httpClient = new HttpClient();

            if (APIExtensions.isTokenNeeded(type))
            {
                httpClient.DefaultRequestHeaders.Add("Authorization", "Basic " + await FileHandle.GetToken());
            }
            if (item == null)
            {
                string content = await httpClient.GetAsync(URL).Result.Content.ReadAsStringAsync();

                Debug.WriteLine("GET: " + content);
                return(content); // HTTP GET
            }
            try
            {
                string data    = JsonConvert.SerializeObject(item);
                string content = await httpClient.PostAsync(URL, new StringContent(data, System.Text.Encoding.UTF8, "application/json")).Result.Content.ReadAsStringAsync();

                Debug.WriteLine("POST: " + content);
                return(content);
            } catch (JsonSerializationException)
            {
                ExceptionHandle.ThrowDebug("json error");
                return(null);
            }
        }
Exemple #2
0
 public async static void ThrowException(string response)
 {
     try
     {
         Entity.Exception errorObject = JsonConvert.DeserializeObject <Entity.Exception>(response);
         ExceptionHandle.ThrowDebug(errorObject.message);
     }
     catch
     {
         ExceptionHandle.ThrowDebug("json exception parse error");
     }
 }
Exemple #3
0
        public static async Task <Uri> Upload(StorageFile file, string URL, string param, string type)
        {
            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");

            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(URL);

            wr.ContentType = "multipart/form-data; boundary=" + boundary;
            wr.Method      = "POST";

            Stream rs = await wr.GetRequestStreamAsync();

            rs.Write(boundarybytes, 0, boundarybytes.Length);

            string header = string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n", param, "path_file", type);

            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
            rs.Write(headerbytes, 0, headerbytes.Length);

            Stream fileStream = await file.OpenStreamForReadAsync();

            byte[] buffer    = new byte[4096];
            int    bytesRead = 0;

            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                rs.Write(buffer, 0, bytesRead);
            }

            byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
            rs.Write(trailer, 0, trailer.Length);

            WebResponse wresp = null;

            try
            {
                wresp = await wr.GetResponseAsync();

                Stream       stream2 = wresp.GetResponseStream();
                StreamReader reader2 = new StreamReader(stream2);
                return(new Uri(reader2.ReadToEnd(), UriKind.Absolute));
            }
            catch
            {
                ExceptionHandle.ThrowDebug("upload error");
                return(null);
            }
        }
Exemple #4
0
 public async static Task <ObservableCollection <Song> > GetMine(bool isRequired)
 {
     if (_mysongs.Count != 0 && !isRequired)
     {
         return(_mysongs);
     }
     try
     {
         _mysongs = JsonConvert.DeserializeObject <ObservableCollection <Song> >(await ApiHandle <string> .Call(APITypes.GetSavedSongs, null));
         return(_mysongs);
     } catch
     {
         ExceptionHandle.ThrowDebug("json error");
         return(null);
     }
 }