//处理请求 protected override void ProcessRecord() { LoginReq loginReq = new LoginReq(); loginReq.ServerIp = Server; loginReq.AuthUser = User; loginReq.AuthKey = EncryptUtil.SHA256Encrypt(Password); if (String.IsNullOrEmpty(AuthType)) { loginReq.AuthType = AuthType; } AuthorizeResp authorizeResp = new AuthorizeResp(); string strJsonData = RestClientUtil.SendLoginRequest(loginReq); if (StringUtil.HasErrorCode(strJsonData)) { ErrorInfo errorInfo = JsonUtil.JsonDeserialize<ErrorInfo>(strJsonData); authorizeResp.status = "FAILED"; authorizeResp.failedCause = errorInfo.ErrorDes; } else { authorizeResp.status = "SUCCESS"; } authorizeResp.name = Server; MessageContext.authorizeResp.name = Server; authorizeResp.user = User; MessageContext.authorizeResp.user = User; authorizeResp.port = SDKConstant.HTTPS_PORT; WriteObject(authorizeResp); }
public static string SendLoginRequest(LoginReq loginReq) { endPoint = SDKConstant.PRPTOCOL_HTTPS + loginReq.ServerIp + ":" + SDKConstant.HTTPS_PORT; Uri address = new Uri(endPoint + SDKConstant.SESSION_URI); // Create the web request HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address); request.Proxy = null; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); // Set type request.Method = "POST"; request.ContentType = "application/json;charset=UTF-8"; request.Accept = "application/json;version=5.0;charset=UTF-8"; request.Headers.Add("X-Auth-User", loginReq.AuthUser); request.Headers.Add("X-Auth-Key", loginReq.AuthKey); if (!String.IsNullOrEmpty(loginReq.AuthType)) { request.Headers.Add("X-Auth-AuthType ", loginReq.AuthType); } request.Headers.Add("Accept-Language", "zh_CN"); byte[] byteData = UTF8Encoding.UTF8.GetBytes("{}"); try { using (Stream writeStream = request.GetRequestStream()) { writeStream.Write(byteData, 0, byteData.Length); writeStream.Close(); } } catch(Exception e) { var message = String.Format("Request failed. cause by {0}", e.Message); return SDKConstant.ERROR_CONNECT; } // Get response HttpWebResponse response = null; try { response = (HttpWebResponse)request.GetResponse(); } catch (WebException e) { response = (HttpWebResponse)e.Response; } string responseValue = String.Empty; using (Stream responseStream = response.GetResponseStream()) { if (responseStream != null) { using (StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("UTF-8"))) { responseValue = reader.ReadToEnd(); reader.Close(); } } } if (!String.IsNullOrEmpty(response.Headers["X-Auth-Token"])) { tokenId = response.Headers["X-Auth-Token"]; } if (response.StatusCode == HttpStatusCode.NotFound) { var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode); return SDKConstant.ERROR_HTTP; } return responseValue; }