Beispiel #1
0
 /// <summary>Set the default <tt>NtlmAuthenticator</tt>.</summary>
 /// <remarks>
 /// Set the default <tt>NtlmAuthenticator</tt>. Once the default authenticator is set it cannot be changed. Calling this metho again will have no effect.
 /// </remarks>
 public static void SetDefault(NtlmAuthenticator a)
 {
     lock (typeof(NtlmAuthenticator))
     {
         if (_auth != null)
         {
             return;
         }
         _auth = a;
     }
 }
		/// <summary>Set the default <tt>NtlmAuthenticator</tt>.</summary>
		/// <remarks>Set the default <tt>NtlmAuthenticator</tt>. Once the default authenticator is set it cannot be changed. Calling this metho again will have no effect.
		/// 	</remarks>
		public static void SetDefault(NtlmAuthenticator a)
		{
			lock (typeof(NtlmAuthenticator))
			{
				if (_auth != null)
				{
					return;
				}
				_auth = a;
			}
		}
Beispiel #3
0
 private SmbFolder(SmbFile parent, String subPath)
 {
     try
     {
         if (!subPath.EndsWith("/"))
         {
             subPath = subPath + "/";
         }
         if (parent == null || String.IsNullOrEmpty(parent.Server))
         {
                                 #if DEBUG_SAMBA
             Android.Util.Log.Debug("SmbClient", "Create file with root path: " + "smb://" + subPath);
                                 #endif
             if (parent.Principal == null || String.IsNullOrEmpty(((NtlmPasswordAuthentication)parent.Principal).Username))
             {
                 NtlmPasswordAuthentication auth = NtlmAuthenticator.RequestNtlmPasswordAuthentication("LAST_USED_AUTH", null);
                 if (auth != null)
                 {
                     mSmbFile = new SmbFile("smb://" + subPath, auth);
                 }
                 else
                 {
                     mSmbFile = new SmbFile("smb://" + subPath, new NtlmPasswordAuthentication(String.Empty, "GUEST", String.Empty));
                 }
             }
             else
             {
                 mSmbFile = new SmbFile("smb://" + subPath, (NtlmPasswordAuthentication)parent.Principal);
             }
         }
         else
         {
                                 #if DEBUG_SAMBA
             Android.Util.Log.Debug("SmbClient", "Create file with sub path: " + parent.Path + "/" + subPath);
                                 #endif
             mSmbFile = new SmbFile(parent, subPath);
         }
     }
     catch (SmbException ex)
     {
         LogException(ex);
         throw new System.IO.IOException(ex.Message, ex);
     }
 }
Beispiel #4
0
        public Task <List <String> > GetSubFolderNames()
        {
            if (mSmbFile == null)
            {
                return(Task.FromResult(new List <String>()));
            }

            return(Task.Factory.StartNew(() => {
                var result = new List <String>();
                try
                {
                    bool hasTriedLastAuth = false;
                    SmbOperations.WithRetries(() =>
                    {
                        result.Clear();
                        try
                        {
                                                        #if DEBUG_SAMBA
                            Android.Util.Log.Debug("SmbClient", "Getting sub folders");
                                                        #endif

                            var subList = mSmbFile.ListFiles();
                            foreach (var element in subList)
                            {
                                try
                                {
                                    if (!element.IsFile)
                                    {
                                        switch (element.Type)
                                        {
                                        case SmbFile.TypeFilesystem:
                                        case SmbFile.TypeWorkgroup:
                                        case SmbFile.TypeServer:
                                        case SmbFile.TypeShare:
                                            result.Add(element.Name);
                                            break;

                                        default:
                                            break;
                                        }
                                    }
                                }
                                catch (SmbException)
                                {
                                }
                                catch (Java.IO.IOException)
                                {
                                }
                            }
                            return result;
                        }
                        catch (SmbAuthException ex)
                        {
                            NtlmPasswordAuthentication auth = null;
                            if (!hasTriedLastAuth)
                            {
                                auth = NtlmAuthenticator.RequestNtlmPasswordAuthentication("LAST_USED_AUTH", ex);
                                hasTriedLastAuth = true;
                            }
                            else
                            {
                                auth = NtlmAuthenticator.RequestNtlmPasswordAuthentication(mSmbFile.Path, ex);
                            }
                            if (auth != null)
                            {
                                mSmbFile = new SmbFile(mSmbFile.Path, auth);
                                throw;
                            }
                            else
                            {
                                return result;
                            }
                        }
                    }, 5);
                }
                catch (SmbException ex)
                {
                    LogException(ex);
                    throw new System.IO.IOException(ex.Message, ex);
                }
                catch (Java.IO.IOException ex)
                {
                    throw new System.IO.IOException(ex.Message, ex);
                }
                return result;
            }));
        }
Beispiel #5
0
        /// <summary>
        ///     开始请求
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //初始化状态
                toolStripStatusState.Text = "请求中...";
                lbResCode.Text            = "请求中...";
                lbResCode.ForeColor       = Color.Black;
                Control c = JsonViewer.Controls.Find("txtJson", true)[0];
                ((TextBox)c)?.Clear();
                richResHeader.Clear();
                richExcutReq.Clear();

                var endPoint  = txtEndPoint.Text.Trim();
                var resource  = txtResource.Text.Trim();
                var reqbody   = richReqBody.Text;
                var userName  = txtUserName.Text.Trim();
                var pwd       = txtPassword.Text.Trim();
                var authType  = cbAuthType.Text;
                var mediaType = cbMediaType.Text;

                //保存请求数据
                SaveRestInfo();

                //认证
                IAuthenticator iaAuthenticator;
                if (authType == "NTLM")
                {
                    ICredentials ic = new NetworkCredential(userName, pwd);
                    iaAuthenticator = new NtlmAuthenticator(ic);
                }
                else if (authType == "Basic")
                {
                    iaAuthenticator = new HttpBasicAuthenticator(userName, pwd);
                }
                else
                {
                    //TODO
                    iaAuthenticator = new HttpBasicAuthenticator(userName, pwd);
                    //iaAuthenticator = new SimpleAuthenticator(userName, pwd);(userName, pwd);
                }

                if (string.IsNullOrEmpty(resource))
                {
                    Uri uri = new Uri(endPoint);
                    resource = uri.AbsolutePath;
                    endPoint = uri.AbsoluteUri.Replace(resource, "");
                }
                IRestSharp   iRestSharp = new RestSharpClient(endPoint, iaAuthenticator);
                IRestRequest iRequest   = new RestRequest(new Uri(endPoint + resource));
                iRequest.Method = (Method)Enum.Parse(typeof(Method), cbMethod.Text);

                foreach (var line in richCurrReqHeader.Lines)
                {
                    var val = line.Trim();
                    if (val.Contains(":"))
                    {
                        var firstIndex = val.IndexOf(":", StringComparison.Ordinal);
                        var key        = val.Substring(0, firstIndex);
                        var value      = val.Substring(firstIndex + 1, val.Length - firstIndex - 1);
                        iRequest.AddHeader(key, value);
                    }
                    else
                    {
                        MessageBox.Show("填写的请求头参数不正确");
                        break;
                    }
                }

                if (!string.IsNullOrEmpty(mediaType))
                {
                    iRequest.AddParameter(mediaType, reqbody, ParameterType.RequestBody);
                }
                //var iResponse = iRestSharp.Execute(iRequest);

                var asyncHandle = iRestSharp.ExecuteAsync(iRequest, response =>
                {
                    this.Invoke(new Action(() =>
                    {
                        toolStripStatusState.Text = response.ResponseStatus.ToString();
                    }));
                    var headerStr = "";
                    foreach (var parameter in response.Headers)
                    {
                        headerStr += parameter.Name + ":" + parameter.Value + "\n";
                    }

                    richExcutReq.Invoke(new Action(() =>
                    {
                        richExcutReq.Clear();
                        string reqContentType = "", reqBody = "";
                        foreach (var requestParameter in response.Request.Parameters)
                        {
                            if (requestParameter.Type == ParameterType.RequestBody)
                            {
                                reqContentType = requestParameter.Name;
                                reqBody        = requestParameter.Value.ToString();
                            }
                            else
                            {
                                richExcutReq.AppendText(requestParameter.Name + ":" + requestParameter.Value + "\n");
                            }
                        }
                        richExcutReq.AppendText("\n*****************************" + reqContentType + "*******************************\n");
                        richExcutReq.AppendText(reqBody);
                    }));
                    this.Invoke(new Action(() =>
                    {
                        richResHeader.Text  = headerStr.Trim(':');
                        lbResCode.Text      = Convert.ToInt32(response.StatusCode) + " " + response.StatusCode;
                        lbResCode.ForeColor = response.StatusCode == HttpStatusCode.OK ? Color.Green : Color.Red;
                    }));
                    JsonViewer.Invoke(new Action(() =>
                    {
                        JsonViewer.Json           = response.Content;
                        toolStripStatusState.Text = response.ResponseStatus.ToString();
                    }));
                });
                // abort the request on demand
                //asyncHandle.Abort();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "请求失败,请检查配置信息");
            }
        }