コード例 #1
0
        public void LoginAsync(string login, string password, Action <string> callback, Action <Exception> callbackError, bool useSsl)
        {
            if (login == null)
            {
                throw new ArgumentNullException("login");
            }
            if (password == null)
            {
                throw new ArgumentNullException("password");
            }

            WebClient client = new WebClient();

            Uri uri = new UriBuilder
            {
                Host   = this.Host,
                Path   = @"/webman/login.cgi",
                Query  = string.Format("username={0}&passwd={1}", login, password),
                Port   = this.Port,
                Scheme = useSsl ? "https" : "http"
            }.Uri;

            client.DownloadStringCompleted += (sender, e) =>
            {
                if (e.Error != null)
                {
                    if (uri.Scheme == "https")
                    {
                        throw new SynoNetworkException("Open Syno could not connect to the server. Please make sure your server's SSL certificate has been issued by a trusted Certificate Authority. see http://bit.ly/qODji5  for further detail.", e.Error);
                    }

                    throw new SynoNetworkException("Open Syno could not complete the operation. Please check that your phone is not in flight mode and that you are getting a proper signal.", e.Error);
                }
                else
                {
                    string rawCookie = ((WebClient)sender).ResponseHeaders["Set-Cookie"];
                    if (rawCookie == null)
                    {
                        try
                        {
                            if (JObject.Parse(e.Result)["success"].Value <bool>() != true)
                            {
                                throw new SynoLoginException("The login and the password don't match, please check your credentials", null);
                            }
                        }
                        catch (JsonReaderException exception)
                        {
                            PiggybackingJsonReaderException extendedException = new PiggybackingJsonReaderException("Failed JSON document was : " + e.Result, exception);
                            callbackError(extendedException);
                        }
                    }
                    else
                    {
                        string cookie = rawCookie.Split(';').Where(s => s.StartsWith("id=")).Single();
                        this.Token = cookie;

                        // Delete ascii urls patches
                    }

                    callback(this.Token);
                }
            };

            client.DownloadStringAsync(uri);
        }
コード例 #2
0
        public void LoginAsync(string login, string password, Action<string> callback, Action<Exception> callbackError, bool useSsl)
        {
            if (login == null) throw new ArgumentNullException("login");
            if (password == null) throw new ArgumentNullException("password");

            WebClient client = new WebClient();

            Uri uri = new UriBuilder
                {
                    Host = this.Host,
                    Path = @"/webman/login.cgi",
                    Query = string.Format("username={0}&passwd={1}", login, password),
                    Port = this.Port,
                    Scheme = useSsl ? "https" : "http"
                }.Uri;

            client.DownloadStringCompleted += (sender, e) =>
                                                  {
                                                      if (e.Error != null)
                                                      {
                                                          if (uri.Scheme == "https")
                                                          {
                                                              throw new SynoNetworkException("Open Syno could not connect to the server. Please make sure your server's SSL certificate has been issued by a trusted Certificate Authority. see http://bit.ly/qODji5  for further detail.", e.Error);
                                                          }

                                                          throw new SynoNetworkException("Open Syno could not complete the operation. Please check that your phone is not in flight mode and that you are getting a proper signal.", e.Error);
                                                      }
                                                      else
                                                      {
                                                          string rawCookie = ((WebClient)sender).ResponseHeaders["Set-Cookie"];
                                                          if (rawCookie == null)
                                                          {
                                                              try
                                                              {
                                                                  if (JObject.Parse(e.Result)["success"].Value<bool>() != true)
                                                                  {
                                                                      throw new SynoLoginException("The login and the password don't match, please check your credentials", null);
                                                                  }
                                                              }
                                                              catch (JsonReaderException exception)
                                                              {
                                                                  PiggybackingJsonReaderException extendedException = new PiggybackingJsonReaderException("Failed JSON document was : " + e.Result, exception);
                                                                  callbackError(extendedException);
                                                              }

                                                          }
                                                          else
                                                          {
                                                              string cookie = rawCookie.Split(';').Where(s => s.StartsWith("id=")).Single();
                                                              this.Token = cookie;

                                                              // Delete ascii urls patches

                                                          }

                                                          callback(this.Token);
                                                      }

                                                  };

            client.DownloadStringAsync(uri);
        }