Пример #1
0
        private static async void SendFileAsync(string type, string path, string port = "8000", string additionalKeys = "")
        {
            await Task.Run(() =>
            {
                HttpWebRequest requestToServerEndpoint =
                    (HttpWebRequest)WebRequest.Create("http://" + _ipAddress + ":" + port + "/api/" + type +
                                                      "?email=" + InfoSaver.GetEmail() + additionalKeys);

                string boundaryString = "----SomeRandomText";
                string fileUrl        = path;

                requestToServerEndpoint.Method      = WebRequestMethods.Http.Post;
                requestToServerEndpoint.ContentType = "multipart/form-data; boundary=" + boundaryString;
                requestToServerEndpoint.KeepAlive   = true;
                requestToServerEndpoint.Credentials = System.Net.CredentialCache.DefaultCredentials;

                MemoryStream postDataStream = new MemoryStream();
                StreamWriter postDataWriter = new StreamWriter(postDataStream);

                postDataWriter.Write("\r\n--" + boundaryString + "\r\n");
                postDataWriter.Write("Content-Disposition: form-data;"
                                     + "name=\"{0}\";"
                                     + "filename=\"{1}\""
                                     + "\r\nContent-Type: {2}\r\n\r\n",
                                     "file",
                                     Path.GetFileName(fileUrl),
                                     Path.GetExtension(fileUrl));
                postDataWriter.Flush();

                FileStream fileStream = new FileStream(fileUrl, FileMode.Open, FileAccess.Read);
                byte[] buffer         = new byte[1024];
                int bytesRead         = 0;
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    postDataStream.Write(buffer, 0, bytesRead);
                }

                fileStream.Close();

                postDataWriter.Write("\r\n--" + boundaryString + "--\r\n");
                postDataWriter.Flush();

                requestToServerEndpoint.ContentLength = postDataStream.Length;

                using (Stream s = requestToServerEndpoint.GetRequestStream())
                {
                    postDataStream.WriteTo(s);
                }

                Console.WriteLine(requestToServerEndpoint.GetResponse().Headers);

                while (!requestToServerEndpoint.HaveResponse)
                {
                    Thread.Sleep(1000);
                }

                postDataStream.Close();
            });
        }
Пример #2
0
 /// <summary>
 /// Checks if user data already stored locally and tries to login
 /// </summary>
 public void RetrieveLoginStatus()
 {
     if (InfoSaver.UserIsStored())
     {
         var result = TryLogin(InfoSaver.GetUser(), InfoSaver.GetPass(), InfoSaver.GetEmail());
         IsLoggedIn = result;
     }
     else
     {
         IsLoggedIn = false;
     }
 }