' Copyright (C) 2025  P4pillon.org
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU Affero General Public License as
' published by the Free Software Foundation, either version 3 of the
' License, or (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU Affero General Public License for more details.
'
' You should have received a copy of the GNU Affero General Public License
' along with this program.  If not, see <https://www.gnu.org/licenses/>.
Imports System.IO
Imports System.Text
Imports System.Net.Security
Imports System.Xml
Imports System.Threading
Imports System.Net
Imports Microsoft.VisualBasic
Imports System

Public Class DP_Connect
    Public Structure HEARTBEAT_variables_structure
        Dim ReqDelai_last As Date ' date du dernier heartbeat réussi pour calcul de ReqDelai entre Heartbeat secondes

        Dim MetNbC As Integer 'Nombre de reconnexions
        Dim MetCnx As Integer 'Temps de connexion millisecondes

        Dim MetNbCr As Integer 'Nombre de créations
        Dim MetCre As Integer 'Temps de création DP millisecondes

        Dim MetNbCs As Integer 'Nombre de consultations
        Dim MetCon As Integer 'Temps de consultation DP millisecondes

        Dim MetNbAl As Integer 'Nombre d’alimentations
        Dim MetAli As Integer 'Temps d’alimentation DP millisecondes

        Dim MetNbEd As Integer 'Nombre d’éditions
        Dim MetEdi As Integer 'Temps d’édition DP millisecondes

        Dim MetNbSu As Integer 'Nombre de suppressions
        Dim MetSup As Integer 'Temps de suppression DP millisecondes

        Public Sub calcul_moyenne(ByRef nombre As Integer, ByRef moyenne As Integer, ByVal request_time_debut As Date, ByVal request_time_fin As Date)
            Dim date_dif As Global.System.TimeSpan = request_time_fin.Subtract(request_time_debut)
            Dim millisecs As Integer = date_dif.Milliseconds


            moyenne = ((moyenne * nombre) + millisecs) / (nombre + 1)
            nombre = nombre + 1
   
        End Sub

    End Structure
    Public HEARTBEAT_variables As HEARTBEAT_variables_structure

    Public Function Heartbeat(ByVal ReqCtx As String) As Boolean
        'RichTextBox1.Clear()

        Dim X_DP_ReqId As String = System.Guid.NewGuid.ToString
        Dim objResponse As System.Net.HttpWebResponse
        Dim objRequest As System.Net.HttpWebRequest
        Try





            objRequest = System.Net.HttpWebRequest.CreateHttp(Config.DP_URI & Config.DP_EtaTyp & "/" & Config.DP_EtaIdent & "/heartbeat")
            'objRequest = System.Net.HttpWebRequest.CreateHttp("http://127.0.0.1:13000/" & Config.DP_EtaTyp & "/" & Config.DP_EtaIdent & "/heartbeat")
            'objRequest.SendChunked = False
    
            objRequest.AllowAutoRedirect = False


            objRequest.Method = "POST"
            objRequest.Timeout = 30000
            If ReqCtx = "FIN" Or ReqCtx = "DESACTIVATION" Then
                objRequest.KeepAlive = False
            End If
            objRequest.ClientCertificates.Add(DP.DP_get_certif)

            objRequest.UserAgent = Config.DP_EtaTyp & "-" & Config.DP_EtaIdent & "-" & Config.DP_LogValid & "-" & Config.DP_LogVers & "-" & Config.DP_LogVDP

            objRequest.ContentType = "application/x-www-form-urlencoded"


            objRequest.Headers.Add("X-DP-ReqId", X_DP_ReqId)

           
            Dim postData As String
            postData = "statut=${ReqCtx}&delaiHeartbeats=${ReqDelai}&nombreReconnexions=${MetNbC}&delaiReconnexion=${MetCnx}&nombreCreations=${MetNbCr}&delaiCreation=${MetCre}&" & _
                        "nombreConsultations=${MetNbCs}&delaiConsultation=${MetCon}&nombreAlimentations=${MetNbAl}&delaiAlimentation=${MetAli}&nombreEditions=${MetNbEd}&" & _
                        "delaiEdition=${MetEdi}&nombreSuppressions=${MetNbSu}&delaiSuppression=${MetSup}"

            postData = Replace(postData, "${ReqCtx}", ReqCtx)
            Dim ReqDelai As Integer
            If ReqCtx = "DEBUT" Or HEARTBEAT_variables.ReqDelai_last.ToShortDateString = "01/01/0001" Then
                ReqDelai = 0
            Else
                ReqDelai = DateDiff(DateInterval.Second, HEARTBEAT_variables.ReqDelai_last, Now)
            End If
            postData = Replace(postData, "${ReqDelai}", ReqDelai.ToString)
            postData = Replace(postData, "${MetNbC}", HEARTBEAT_variables.MetNbC)
            postData = Replace(postData, "${MetCnx}", HEARTBEAT_variables.MetCnx)
            postData = Replace(postData, "${MetNbCr}", HEARTBEAT_variables.MetNbCr)
            postData = Replace(postData, "${MetCre}", HEARTBEAT_variables.MetCre)
            postData = Replace(postData, "${MetNbCs}", HEARTBEAT_variables.MetNbCs)
            postData = Replace(postData, "${MetCon}", HEARTBEAT_variables.MetCon)
            postData = Replace(postData, "${MetNbAl}", HEARTBEAT_variables.MetNbAl)
            postData = Replace(postData, "${MetAli}", HEARTBEAT_variables.MetAli)
            postData = Replace(postData, "${MetNbEd}", HEARTBEAT_variables.MetNbEd)
            postData = Replace(postData, "${MetEdi}", HEARTBEAT_variables.MetEdi)
            postData = Replace(postData, "${MetNbSu}", HEARTBEAT_variables.MetNbSu)
            postData = Replace(postData, "${MetSup}", HEARTBEAT_variables.MetSup)

            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
            objRequest.ContentLength = byteArray.Length
            'Get the request stream.
            Dim dataStream As Stream = objRequest.GetRequestStream
            ' Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length)
            'Close the Stream object.
            dataStream.Close()
            DP.Write_Trace_Tech(X_DP_ReqId, "HEARTBEAT", ReqCtx & "|" & ReqDelai.ToString & "|" & HEARTBEAT_variables.MetNbC & "|" & HEARTBEAT_variables.MetCnx & "|" & HEARTBEAT_variables.MetNbCr & "|" _
                                & HEARTBEAT_variables.MetCre & "|" & HEARTBEAT_variables.MetNbCs & "|" & HEARTBEAT_variables.MetCon & "|" & HEARTBEAT_variables.MetNbAl & "|" & _
                                 HEARTBEAT_variables.MetAli & "|" & HEARTBEAT_variables.MetNbEd & "|" & HEARTBEAT_variables.MetEdi & "|" & HEARTBEAT_variables.MetNbSu & "|" & HEARTBEAT_variables.MetSup)



            objResponse = objRequest.GetResponse
     
            If objResponse.StatusCode = 302 Then
                If InStr(objResponse.GetResponseHeader("location"), "lecture_msg") <> 0 Then
                    Dim MsgRef = Mid(objResponse.GetResponseHeader("location"), InStrRev(objResponse.GetResponseHeader("location"), "/") + 1)
                    'While MsgRef <> ""
                    DP.Write_Trace_Tech(X_DP_ReqId, "REP_HEARTBEAT", "MESSAGE:" & MsgRef)
                    DP_Alerte.LECTURE_MSG(MsgRef)
                    'End While
                End If
            Else
                DP.Write_Trace_Tech(X_DP_ReqId, "REP_HEARTBEAT", "OK")
                
            End If

          

            HEARTBEAT_variables.ReqDelai_last = Now
            HEARTBEAT_variables.MetNbC = 0
            HEARTBEAT_variables.MetCnx = 0
            HEARTBEAT_variables.MetNbCr = 0
            HEARTBEAT_variables.MetCre = 0
            HEARTBEAT_variables.MetNbCs = 0
            HEARTBEAT_variables.MetCon = 0
            HEARTBEAT_variables.MetNbAl = 0
            HEARTBEAT_variables.MetAli = 0
            HEARTBEAT_variables.MetNbEd = 0
            HEARTBEAT_variables.MetEdi = 0
            HEARTBEAT_variables.MetNbSu = 0
            HEARTBEAT_variables.MetSup = 0

       


            Dim StreamReader As System.IO.StreamReader
            StreamReader = New System.IO.StreamReader(objResponse.GetResponseStream())
            StreamReader.Close()


            Return True


        Catch wex As System.Net.WebException
            If Not wex.Response Is Nothing Then
                Dim StreamReader As System.IO.StreamReader
                StreamReader = New System.IO.StreamReader(wex.Response.GetResponseStream)
                DP.Write_Trace_Tech(X_DP_ReqId, "REP_HEARTBEAT", CType(wex.Response, System.Net.HttpWebResponse).StatusCode & "|" & wex.Message)
                StreamReader.Close()
            Else
                DP.Write_Trace_Tech(X_DP_ReqId, "REP_HEARTBEAT", wex.Message.ToString)
                DP.reset_connection()
            End If



        End Try

       

        Return False
    End Function

    Public Sub connect()
        If Config.DP_is_local_client = True Then
            DP_connection_établie = True
            Exit Sub
        End If



        Dim nbre_tentatives As Integer = 1
        DP_connection_établie = False

        Dim Connection_debut, Connection_fin As Date


        'routine d'envoie des AR_MSG
        If Not DP_AP_MSG_thread Is Nothing Then DP_AP_MSG_thread.Abort()



        Dim objRequest = System.Net.HttpWebRequest.CreateHttp(Config.DP_URI & Config.DP_EtaTyp & "/" & Config.DP_EtaIdent)
        objRequest.ServicePoint.ConnectionLimit = 1
        objRequest.Method = "POST"
        objRequest.ClientCertificates.Add(DP.DP_get_certif)

        Dim datastream As Stream
        Try
            Connection_debut = Now
            datastream = objRequest.GetRequestStream
            Connection_fin = Now
            If objRequest.ServicePoint.CurrentConnections > 0 Then
                HEARTBEAT_variables.calcul_moyenne(HEARTBEAT_variables.MetNbC, HEARTBEAT_variables.MetCnx, Connection_debut, Connection_fin)
            End If
        Catch ex As Exception
            DP.Write_Trace_Tech(ex.Message, "CONNEXION_REFUSEE", DP.DP_get_certif.Subject)
        End Try
        DP_connection_établie = objRequest.ServicePoint.CurrentConnections > 0

        While DP_connection_établie = False And nbre_tentatives <= 3

            nbre_tentatives = nbre_tentatives + 1
            Dim Generator As System.Random = New System.Random()
            System.Threading.Thread.Sleep(45000 + Generator.Next(0, 30000))
            objRequest = System.Net.HttpWebRequest.CreateHttp(Config.DP_URI & Config.DP_EtaTyp & "/" & Config.DP_EtaIdent)
            objRequest.Method = "POST"
            Try
                Connection_debut = Now
                datastream = objRequest.GetRequestStream
                Connection_fin = Now
                If objRequest.ServicePoint.CurrentConnections > 0 Then
                    HEARTBEAT_variables.calcul_moyenne(HEARTBEAT_variables.MetNbC, HEARTBEAT_variables.MetCnx, Connection_debut, Connection_fin)
                End If
            Catch ex As Exception
                DP.Write_Trace_Tech(ex.Message, "CONNEXION_REFUSEE", DP.DP_get_certif.Subject)
            End Try
            DP_connection_établie = objRequest.ServicePoint.CurrentConnections > 0
        End While

        If DP_connection_établie = False Then

            Dim nbre_minute_avant_tentative As Integer = Val(InputBox("Le logiciel n’arrive pas à se connecter au Dossier Pharmaceutique. L’accès est provisoirement désactivé pendant une période de 15 min." + Chr(10) _
                                                                      + "Saisissez une valeur pour modifier ce délai (0 pour desactiver la connection au DP)", "Problème de connexion au DP", "15"))
            If nbre_minute_avant_tentative = 0 Then
                DP.Write_Trace_Tech("DP désactivé par l'utilisateur après echec connexion", "CONNEXION_REFUSEE", DP.DP_get_certif.Subject)
                DP_connection_établie = False
                Config.DP_Activer = False
                Dim ConfigXml As New XmlDocument
                ConfigXml.Load(Application.StartupPath + "\config.xml")
                Dim XMLelementliste As XmlNodeList
                XMLelementliste = ConfigXml.GetElementsByTagName("DP")
                XMLelementliste.Item(0).Item("DP_Activer").InnerText = "NON"
                ConfigXml.Save("config.xml")
                Exit Sub
            End If
            While DP_connection_établie = False

                System.Threading.Thread.Sleep(nbre_minute_avant_tentative * 60 * 1000)
                objRequest = System.Net.HttpWebRequest.CreateHttp(Config.DP_URI & Config.DP_EtaTyp & "/" & Config.DP_EtaIdent)
                objRequest.Method = "POST"
                Try
                    Connection_debut = Now
                    datastream = objRequest.GetRequestStream
                    Connection_fin = Now
                    If objRequest.ServicePoint.CurrentConnections > 0 Then
                        HEARTBEAT_variables.calcul_moyenne(HEARTBEAT_variables.MetNbC, HEARTBEAT_variables.MetCnx, Connection_debut, Connection_fin)
                    End If
                Catch ex As Exception
                    DP.Write_Trace_Tech(ex.Message, "CONNEXION_REFUSEE", DP.DP_get_certif.Subject)
                End Try
                DP_connection_établie = objRequest.ServicePoint.CurrentConnections > 0
            End While

        End If



        DP.Write_Trace_Tech("", "CONNEXION_ETABLIE", DP.DP_get_certif.Subject)
        If DP_Connection_reseted = True Then
            DP_Connection_reseted = False

        End If
        Try
            objRequest.GetResponse()
        Catch ex As Exception

        End Try

        If Config.DP_is_local_client = False Then
            DP_local_server_thread = New Thread(New ThreadStart(AddressOf DP_local_server.Local_Server))
            DP_local_server_thread.Start()

            DP_AP_MSG_thread = New Thread(New ThreadStart(AddressOf DP_local_server.send_AR_MSG))
            DP_AP_MSG_thread.Start()
        End If

        Me.DP_ReqTableAdapter1.Update_Requetes_Perimées(DateTime.Now.AddMonths(-4)) 'les requetes en file d'attente non transmises sous 4 mois ne doivent pas être transmises

        'DP_queue_iam()
        'DP_queue()

        If DP_Heartbeat_Debut_envoyé = False Then
            Dim Generator2 As System.Random = New System.Random()
            'System.Threading.Thread.Sleep(Generator2.Next(0, 15 * 60 * 1000))
            Heartbeat("DEBUT")


            DP_Heartbeat_Debut_envoyé = True
        End If





        While DP_connection_établie = True

            DP_queue_iam()
            DP_queue()
            Dim Generator2 As System.Random = New System.Random()
            System.Threading.Thread.Sleep(885000 + Generator2.Next(0, 30000))
            Heartbeat("SUITE")
        End While






    End Sub

#Region "FILE D'ATTENTE"
    Public Sub DP_queue()

        While Me.DP_ReqTableAdapter1.GetData_Queue(Now).Rows.Count > 0

            Dim req_row = Me.DP_ReqTableAdapter1.GetData_Queue(Now).Item(0)

            Try
                Dim xmlreq As New XmlDocument
                xmlreq.Load(Config.LocalNetwork_ServerName & "\DP\XML\log\" & req_row.ReqId & ".xml")

                If DP_objRequest_has_been_used = True Then
                    If Not DP_objRequest Is Nothing Then DP_objRequest.Headers.Clear()
                Else
                    DP_objRequest_has_been_used = True
                End If

                'signature
                DP.Sign_XML(Config.LocalNetwork_ServerName & "\DP\XML\log\" & req_row.ReqId & ".xml", req_row.ReqId, Config.LocalNetwork_ServerName & "\DP\XML\log\" & req_row.ReqId & ".signed")

                Dim xmldoc As New XmlDocument
                xmldoc.Load(Config.LocalNetwork_ServerName & "\DP\XML\log\" & req_row.ReqId & ".signed")

                Dim postdata_signed As String = xmldoc.OuterXml

                Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postdata_signed)



                DP_objRequest = System.Net.HttpWebRequest.CreateHttp(Config.DP_URI & Config.DP_EtaTyp & "/" & Config.DP_EtaIdent & "/alimentation")

                DP_objRequest.ContentLength = byteArray.Length

                DP_objRequest.Timeout = "30000"

                DP_objRequest.Method = "POST"

                DP_objRequest.ClientCertificates.Add(DP.DP_get_certif)

                DP_objRequest.UserAgent = Config.DP_EtaTyp & "-" & Config.DP_EtaIdent & "-" & Config.DP_LogValid & "-" & Config.DP_LogVers & "-" & Config.DP_LogVDP

                DP_objRequest.ContentType = "application/vnd.cnop.dp.alimentation+signature"

                DP_objRequest.Headers.Add("X-DP-ReqId", req_row.ReqId)

                DP_objRequest.Headers.Add("X-DP-Sequence-Number", req_row.Sequence_Number)

                DP_objRequest.Headers.Add("X-DP-ASyncReq", "Alimentation")



                '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                'envoi


                Dim dataStream As Stream = DP_objRequest.GetRequestStream

                dataStream.Write(byteArray, 0, byteArray.Length)

                dataStream.Close()

                DP.Write_Trace_Tech(req_row.ReqId, "REQUETE_EN_ATTENTE_RENVOYEE", "")

                Dim objResponse As System.Net.HttpWebResponse
                Dim request_time_debut, request_time_fin As Date
                DP.Write_Trace_Tech(req_row.ReqId, "ALIMENTATION", req_row.Identifiant_patient)
                request_time_debut = Now
                objResponse = DP_objRequest.GetResponse()
                request_time_fin = Now
                HEARTBEAT_variables.calcul_moyenne(HEARTBEAT_variables.MetNbAl, HEARTBEAT_variables.MetAli, request_time_debut, request_time_fin)

                Dim StreamReader As System.IO.StreamReader
                StreamReader = New System.IO.StreamReader(objResponse.GetResponseStream())

                My.Computer.FileSystem.WriteAllText(Config.LocalNetwork_ServerName & "\DP\XML\log\Réponse\REP_REQUETE_EN_ATTENTE_RENVOYEE_" & Format(Now, "yyyyMMddHHmmss") & "_" & req_row.ReqId & ".xml", StreamReader.ReadToEnd.ToString, False)
                DP.Write_Trace_Fonc_Queue("Alimentation du DP effectuée", req_row.référence_interne, "réemission requete en echec", req_row.Identifiant_patient)
                DP.Write_Trace_Tech(req_row.ReqId, "REP_ALIMENTATION", "OK")
                Me.DP_ReqTableAdapter1.Update_byReqID(req_row.Sequence_Number, "OK", req_row.nbr_trans + 1, Now, req_row.Identifiant_patient, req_row.référence_interne, req_row.ReqId)

            Catch ex As System.Net.WebException

                If Not ex.Response Is Nothing Then
                    Dim StreamReader As System.IO.StreamReader
                    StreamReader = New System.IO.StreamReader(ex.Response.GetResponseStream)

                    My.Computer.FileSystem.WriteAllText(Config.LocalNetwork_ServerName & "\DP\XML\log\Réponse\temp", StreamReader.ReadToEnd.ToString, False)
                    StreamReader.Close()

                    Dim xml_retour As New XmlDocument
                    xml_retour.Load(Config.LocalNetwork_ServerName & "\DP\XML\log\Réponse\temp")
                    Dim XMLelementliste As XmlNodeList
                    XMLelementliste = xml_retour.GetElementsByTagName("acknowledgementDetail")

                    If XMLelementliste.Item(0).Item("code").GetAttribute("code") <> "ERREUR_TECHNIQUE" Then
                        Me.DP_ReqTableAdapter1.Update_byReqID(req_row.Sequence_Number.ToString, XMLelementliste.Item(0).Item("code").GetAttribute("code"), req_row.nbr_trans + 1, Now, req_row.Identifiant_patient, req_row.référence_interne, req_row.ReqId)
                        DP.Write_Trace_Tech(req_row.ReqId, "REP_ALIMENTATION", XMLelementliste.Item(0).Item("code").GetAttribute("code") & "|" & XMLelementliste.Item(0).Item("text").InnerText)
                        DP.Write_Trace_Fonc_Queue("Échec de l’alimentation du DP", req_row.référence_interne, "Alimentation non effectuée suite à un problème technique", req_row.Identifiant_patient)

                    Else
                        Dim next_trans_time As Date = Now
                        Dim Generator As System.Random = New System.Random()
                        Select Case req_row.nbr_trans
                            Case 2
                                next_trans_time = next_trans_time.AddHours(24)
                                next_trans_time = next_trans_time.AddMinutes(Generator.Next(0, 59))
                            Case 3
                                next_trans_time = next_trans_time.AddHours(48)
                                next_trans_time = next_trans_time.AddMinutes(Generator.Next(0, 59))
                        End Select
                        Me.DP_ReqTableAdapter1.Update_byReqID(req_row.Sequence_Number.ToString, XMLelementliste.Item(0).Item("code").GetAttribute("code"), req_row.nbr_trans + 1, next_trans_time, req_row.Identifiant_patient, req_row.référence_interne, req_row.ReqId)
                        DP.Write_Trace_Tech(req_row.ReqId, "REP_ALIMENTATION", XMLelementliste.Item(0).Item("code").GetAttribute("code") & "|" & XMLelementliste.Item(0).Item("text").InnerText)
                        If (req_row.nbr_trans) < 4 Then
                            DP.Write_Trace_Fonc_Queue("Échec de l’alimentation du DP", req_row.référence_interne, "Problème technique : l'alimentation est mise en file d’attente", req_row.Identifiant_patient)
                            DP.Write_Trace_Tech(req_row.ReqId, "REQUETE_EN_ATTENTE", XMLelementliste.Item(0).Item("code").GetAttribute("code") & "|" & XMLelementliste.Item(0).Item("text").InnerText)
                        Else
                            DP.Write_Trace_Fonc_Queue("Échec de l’alimentation du DP", req_row.référence_interne, "Alimentation non effectuée suite à un problème technique", req_row.Identifiant_patient)
                        End If
                    End If


                Else
                    DP.Write_Trace_Tech(req_row.ReqId, "REQUETE_EN_ATTENTE", ex.Message.ToString)
                    DP.Write_Trace_Fonc_Queue("Échec de l’alimentation du DP", req_row.référence_interne, "Problème technique : l'alimentation est mise en file d’attente", req_row.Identifiant_patient)
                    Me.DP_ReqTableAdapter1.Update_byReqID(req_row.Sequence_Number, "A_REEMETTRE", req_row.nbr_trans, req_row.next_trans_time.AddMinutes(15), req_row.Identifiant_patient, req_row.référence_interne, req_row.ReqId)


                End If



            End Try



        End While
    End Sub

    Public Sub DP_queue_iam()
        While Me.DP_ReqTableAdapter1.GetData_Queue_IAM.Rows.Count > 0

            Dim req_row = Me.DP_ReqTableAdapter1.GetData_Queue_IAM.Item(0)
            Try
                If DP_objRequest_has_been_used = True Then
                    If Not DP_objRequest Is Nothing Then DP_objRequest.Headers.Clear()
                Else
                    DP_objRequest_has_been_used = True
                End If
                DP_objRequest = System.Net.HttpWebRequest.CreateHttp(Config.DP_URI & Config.DP_EtaTyp & "/" & Config.DP_EtaIdent & "/iam")
                DP_objRequest.Timeout = "30000"
                DP_objRequest.Method = "POST"
                DP_objRequest.ClientCertificates.Add(DP.DP_get_certif)
                DP_objRequest.UserAgent = Config.DP_EtaTyp & "-" & Config.DP_EtaIdent & "-" & Config.DP_LogValid & "-" & Config.DP_LogVers & "-" & Config.DP_LogVDP
                DP_objRequest.ContentType = "application/vnd.cnop.dp.iam"
                DP_objRequest.Headers.Add("X-DP-ReqId", req_row.ReqId)
                DP_objRequest.Headers.Add("X-DP-ASyncReq", "Alimentation-IAM")
                Dim byteArray As Byte() = Encoding.UTF8.GetBytes(My.Computer.FileSystem.ReadAllText(Config.LocalNetwork_ServerName & "\DP\XML\log\" & req_row.ReqId & ".xml"))
                DP_objRequest.ContentLength = byteArray.Length
                ' Get the request stream.
                Dim dataStream As Stream = DP_objRequest.GetRequestStream()
                ' Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length)
                ' Close the Stream object.
                dataStream.Close()

                DP.Write_Trace_Tech(req_row.ReqId, "REQUETE_IAM_EN_ATTENTE_RENVOYEE", "")


                Dim DP_IAM_objResponse As System.Net.HttpWebResponse = DP_objRequest.GetResponse()

                'DP.Write_Trace_Tech(req_row.ReqId, "REP_ALIMENTATION_IAM_QUEUE", "OK")

                Me.DP_ReqTableAdapter1.Update_byReqID("", "OK", 2, Now, "IAM", req_row.référence_interne, req_row.ReqId)
                Dim StreamReader As System.IO.StreamReader
                StreamReader = New System.IO.StreamReader(DP_IAM_objResponse.GetResponseStream())
                Dim réponse As String = StreamReader.ReadToEnd.ToString
                If réponse = "" Then
                    DP.DP_ReqTableAdapter.Update_byReqID("", "OK", 2, Now, "IAM", req_row.référence_interne, req_row.ReqId)
                    DP.Write_Trace_Tech(req_row.ReqId, "REP_ALIMENTATION_IAM", "OK")
                Else
                    DP.DP_ReqTableAdapter.Update_byReqID("", réponse, 2, Now, "IAM", req_row.référence_interne, req_row.ReqId)
                    DP.Write_Trace_Tech(req_row.ReqId, "REP_ALIMENTATION_IAM", réponse)
                End If


            Catch wex As System.Net.WebException
                Me.DP_ReqTableAdapter1.Update_byReqID("", wex.Message.ToString, 2, Now, "IAM", req_row.référence_interne, req_row.ReqId)
                If Not wex.Response Is Nothing Then
                    Dim StreamReader As System.IO.StreamReader
                    StreamReader = New System.IO.StreamReader(wex.Response.GetResponseStream)

                    DP.Write_Trace_Tech(req_row.ReqId, "REP_ALIMENTATION_IAM", CType(wex.Response, System.Net.HttpWebResponse).StatusCode & "|" & wex.Message.ToString)
                    StreamReader.Close()
                Else
                    DP.Write_Trace_Tech(req_row.ReqId, "REP_ALIMENTATION_IAM", wex.Message.ToString)
                End If
            Catch ex As Exception
                DP.Write_Trace_Tech(req_row.ReqId, "REP_ALIMENTATION_IAM", ex.Message.ToString)
            End Try
        End While
    End Sub
#End Region
End Class