public static int GetResultBMSAPI(ResponseBMS Response) { if (Response == null) { MessageBox.Show("Ответ не был получен", "Ошибка"); return(1); } if (Response.BpsResponse.state == "ERROR") { MessageBox.Show(Response.BpsResponse.stateCode, "Ошибка"); return(1); } else { MessageBox.Show("Операция прошла успешно", "Сообщение"); } return(0); }
public static ResponseBMS BMSRequest(string typeRequest, string request, string data, string login, string password) { ResponseBMS Response = new ResponseBMS(); try { CredentialCache cache = new CredentialCache(); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(request); string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(login + ":" + password)); req.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials; //Игнорируем недостоверный сертификат SSL ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { return(true); }; req.KeepAlive = false; req.Method = typeRequest; req.Timeout = 20000; if (!Equals(typeRequest, "GET")) { byte[] byteArray = Encoding.UTF8.GetBytes(data); req.ContentType = "application/json"; req.ContentLength = byteArray.Length; Stream dataStreamReq = req.GetRequestStream(); dataStreamReq.Write(byteArray, 0, byteArray.Length); dataStreamReq.Close(); } HttpWebResponse response = (HttpWebResponse)req.GetResponse(); Stream dataStreamResp = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStreamResp, Encoding.Default); Response = JsonConvert.DeserializeObject <ResponseBMS>(reader.ReadToEnd()); reader.Close(); dataStreamResp.Close(); response.Close(); return(Response); } catch (WebException e) { using (Stream streamData = e.Response.GetResponseStream()) using (var reader = new StreamReader(streamData)) { Response = JsonConvert.DeserializeObject <ResponseBMS>(reader.ReadToEnd()); reader.Close(); streamData.Close(); } return(Response); } catch (Exception) { return(Response); } }