private void buttonSendCommand_Click(object sender, EventArgs e) { if (serialPort1.IsOpen) { serialPort1.Write(WCommandCurrent.toByteAray().ToArray(), 0, WCommandCurrent.toByteAray().Count); WResponseCurrent = new WResponse(); // empty response } }
public bool validURLAccess(System.String sURL, System.String sUserName, System.String sUserPass) { try { string strResponse = string.Empty; string strMethod = "GET"; System.Uri requestURL = new Uri(sURL); // Chequear si es una solicitud FTP System.Net.WebRequest WRequest; System.Net.WebResponse WResponse; /* * if(sURL.Substring(0,6).ToLower()=="ftp://") * { * FtpRequestCreator Creator = new FtpRequestCreator(); * System.Net.WebRequest.RegisterPrefix("ftp:", Creator); * strMethod = "dir"; * } */ WRequest = System.Net.WebRequest.Create(sURL); WRequest.Method = strMethod; if (sUserName != string.Empty) // we will add the user and password for basic auth. { System.Net.NetworkCredential myCred = new System.Net.NetworkCredential(sUserName, sUserPass); System.Net.CredentialCache MyCrendentialCache = new System.Net.CredentialCache(); MyCrendentialCache.Add(requestURL, "Basic", myCred); WRequest.Credentials = MyCrendentialCache; } else //Set the default Credentials. This will allow NTLM or Kerbeors authentication with out prompting the user { // the default credentials are usually the Windows credentials (user name, password, and domain) of the user running the application WRequest.Credentials = System.Net.CredentialCache.DefaultCredentials; } WResponse = (System.Net.WebResponse)WRequest.GetResponse(); System.IO.StreamReader sr = new System.IO.StreamReader(WResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("iso-8859-1")); //Convert the stream to a string strResponse = sr.ReadToEnd(); System.Diagnostics.Debug.WriteLine(strResponse); sr.Close(); return(true); } catch (System.Exception Ex) { System.Diagnostics.Debug.WriteLine(Ex.ToString()); throw new System.Exception(Ex.Message.ToString(), Ex); } }
/// <summary> /// 登录 /// </summary> /// <param name="userName">用户名</param> /// <param name="passWord">密码</param> /// <param name="domain">域</param> /// <param name="loginUrl">登录地址</param> /// <param name="IsLogin">不使用缓存,重新登录</param> public void LogLan(string userName, string passWord, string domain, string loginUrl = "", bool IsLogin = false) { if (string.IsNullOrEmpty(loginUrl)) { loginUrl = LoginUrl; } else { LoginUrl = loginUrl; } HttpWebRequest WRequest; HttpWebResponse WResponse; if (IsLogin) { myCredCache = null; } if (myCredCache == null) { myCredCache = new CredentialCache(); } if (myCredCache.GetCredential(new Uri(LoginUrl), "NTLM") == null) { myCredCache.Add(new Uri(LoginUrl), "NTLM", new NetworkCredential(userName, passWord, domain)); // Pre-authenticate the request. WRequest = (HttpWebRequest)HttpWebRequest.Create(LoginUrl); // Set the username and the password. WRequest.Credentials = myCredCache; // This property must be set to true for Kerberos authentication. // Keep the connection alive. WRequest.UserAgent = "Upload Test"; WRequest.Method = "HEAD"; WRequest.Timeout = 10000; WResponse = (HttpWebResponse)WRequest.GetResponse(); WResponse.Close(); } }
// Get Data from Debug Source private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) { if (DebugFull_B) { debugDataQueue.Enqueue(serialPort1.ReadExisting()); } else { int i = 0; int length = serialPort1.BytesToRead; byte[] bytes = new byte[length]; byte singleByte = 0x00; while (length != 0) { // Get one byte to test singleByte = (byte)serialPort1.ReadByte(); // Check if response has been sent if ((ResponseState == 0) && (singleByte == 0x02)) { ResponseState = 1; ResponseIndex = 0; } switch (ResponseState) { // STX case 1: WResponseByteArray[ResponseIndex++] = singleByte; ResponseState++; break; // ID case 2: WResponseByteArray[ResponseIndex++] = singleByte; ResponseState++; break; // Status case 3: WResponseByteArray[ResponseIndex++] = singleByte; if ((WResponseByteArray[1] & 0x80) == 0x80) { ResponseState++; // Get Lentgh and Data } else { ResponseState = 6; // Get ACK } break; // Length case 4: WResponseByteArray[ResponseIndex++] = singleByte; ResponseState++; break; // Data case 5: WResponseByteArray[ResponseIndex++] = singleByte; if ((ResponseIndex - 4) == WResponseByteArray[3]) { ResponseState++; } break; // ACK case 6: WResponseByteArray[ResponseIndex++] = singleByte; ResponseState++; break; // ETX case 7: WResponseByteArray[ResponseIndex++] = singleByte; WResponseCurrent = new WResponse(WResponseByteArray, ResponseIndex); //textBoxResponse.Text = ""; //for (int j = 0; j < ReponseIndex; j++) //{ // StringBuilder sb1 = new StringBuilder(2); // sb1.AppendFormat("0x{0:X2}", WResponseByteArray[j]); // textBoxResponse.Text += sb1.ToString(); // textBoxResponse.Text += ' '; //} ResponseState = 0; // Reset state machine break; default: bytes[i++] = singleByte; break; } length--; } debugDataQueue.Enqueue(System.Text.Encoding.ASCII.GetString(bytes)); } }