' 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.Xml
Imports System.Text
Imports System.IO
Imports System.Threading

Public Class DP_local_server
    Public Sub Local_Server()


debut:
        System.Threading.Thread.Sleep(100)
        Dim file As String
        file = Dir(Config.LocalNetwork_ServerName & "\DP\Clients\*.xml")
        If file <> "" Then
            Try


                System.Threading.Thread.Sleep(500)
                Dim ReqXml As New XmlDocument

                ReqXml.Load(Config.LocalNetwork_ServerName & "\DP\Clients\" & file)
                Select Case ReqXml.FirstChild.Name

                    Case "RCMR_IN000031UV01" 'consultation
                        Affichage_relay(ReqXml)
                        My.Computer.FileSystem.MoveFile(Config.LocalNetwork_ServerName & "\DP\Clients\" & file, Config.LocalNetwork_ServerName & "\DP\XML\log\" & file, True)

                    Case "PRPA_IN201311UV02" 'création
                        Select Case ReqXml.FirstChild.Item("interactionId").GetAttribute("extension")
                            Case "CREATION"
                                Création_relay(ReqXml)
                            Case "REFUS_CREATION"
                                Refus_Création_relay(ReqXml)

                        End Select

                    Case "PRPA_IN201314UV02"
                        Suppression_relay(ReqXml)

                    Case "RCMR_IN000002UV02"
                        Dispensation_relay(ReqXml)


                    Case "iam"
                        IAM_Send_requeste_relay(ReqXml, Mid(file, 1, InStr(file, ".") - 1))

                End Select
            Catch ex As Exception
                My.Computer.FileSystem.MoveFile(Config.LocalNetwork_ServerName & "\DP\Clients\" & file, Config.LocalNetwork_ServerName & "\DP\Clients\error\" & file, True)
            End Try

        End If




        GoTo debut

    End Sub

    Public Sub send_AR_MSG()

debut:
        System.Threading.Thread.Sleep(1000 * 180)
        Dim data = DP_Alerte.DP_MsgTableAdapter.GetData_where_AR_must_be_send(Now)
        If data.Rows.Count > 0 Then
            DP_Alerte.AR_MSG(data.Item(0).MsgRef)
        End If
        GoTo debut
    End Sub

    Public Sub Affichage_relay(ByVal ReqXml As XmlDocument)
        Dim ConTyp As String
        Dim XMLelementliste = ReqXml.GetElementsByTagName("RCMR_IN000031UV01")
        ConTyp = XMLelementliste.Item(0).Item("interactionId").GetAttribute("extension")


        'If ConTyp <> "EDITION" Then
        '    COMPTOIR_MAIN.DP_affichage_statut("Accès au DP en cours")
        'End If

        Dim X_DP_ReqId As String = XMLelementliste.Item(0).Item("id").GetAttribute("extension")


        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


            Select Case ConTyp
                Case "EDITION"
                    DP_objRequest = System.Net.HttpWebRequest.CreateHttp(Config.DP_URI & Config.DP_EtaTyp & "/" & Config.DP_EtaIdent & "/edition")
                Case "CONSULTATION_TOUT", "CONSULTATION", "REFUS_CONSULTATION"
                    DP_objRequest = System.Net.HttpWebRequest.CreateHttp(Config.DP_URI & Config.DP_EtaTyp & "/" & Config.DP_EtaIdent & "/consultation")
            End Select


            DP_objRequest.Timeout = "10000"

            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.consultation"

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

            DP.DP_ReqTableAdapter.Insert("", X_DP_ReqId, "Local_Server", 1, Now, "", "")

            Dim postdata As String = ReqXml.OuterXml

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

            DP_objRequest.ContentLength = byteArray.Length

            Dim dataStream As Stream = DP_objRequest.GetRequestStream

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

            dataStream.Close()

            Dim objResponse As System.Net.HttpWebResponse
            Dim request_time_debut, request_time_fin As Date
            request_time_debut = Now
            objResponse = DP_objRequest.GetResponse()
            request_time_fin = Now
            Select Case ConTyp
                Case "EDITION"
                    DP_Connect.HEARTBEAT_variables.calcul_moyenne(DP_Connect.HEARTBEAT_variables.MetNbEd, DP_Connect.HEARTBEAT_variables.MetEdi, request_time_debut, request_time_fin)
                Case "CONSULTATION_TOUT", "CONSULTATION"
                    DP_Connect.HEARTBEAT_variables.calcul_moyenne(DP_Connect.HEARTBEAT_variables.MetNbCs, DP_Connect.HEARTBEAT_variables.MetCon, request_time_debut, request_time_fin)
            End Select


            Dim StreamReader As System.IO.StreamReader

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


            histo_dp.LoadXml(StreamReader.ReadToEnd.ToString)
            StreamReader.Close()
            histo_dp.Save(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & "_ok")



        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)
                My.Computer.FileSystem.WriteAllText(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & "_error", StreamReader.ReadToEnd.ToString, False)
                StreamReader.Close()
            Else
                My.Computer.FileSystem.WriteAllText(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & "_ko", wex.Message.ToString, False)
            End If

        Catch ex As Exception
            My.Computer.FileSystem.WriteAllText(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & "_ko", ex.Message.ToString, False)
        End Try

    End Sub

    Public Sub Création_relay(ByVal ReqXml As XmlDocument)

        Dim XMLelementliste = ReqXml.GetElementsByTagName("PRPA_IN201311UV02")

        Dim X_DP_ReqId As String = XMLelementliste.Item(0).Item("id").GetAttribute("extension")
        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 & "/creation")

            DP_objRequest.Timeout = "10000"

            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.creation+signature"

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

            DP_objRequest.Headers.Add("X-DP-Sequence-Number", X_DP_Sequence_Number.ToString)

            DP.DP_ReqTableAdapter.Insert(X_DP_Sequence_Number.ToString, X_DP_ReqId, "Local_Server", 1, Now, "", "")


            'signature
            DP.Sign_XML(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".xml", X_DP_ReqId, Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".signed")
            My.Computer.FileSystem.MoveFile(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".xml", Config.LocalNetwork_ServerName & "\DP\xml\log\" & X_DP_ReqId & ".xml", True)
            'envoi
            Dim xmldoc As New XmlDocument
            xmldoc.Load(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".signed")
            My.Computer.FileSystem.MoveFile(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".signed", Config.LocalNetwork_ServerName & "\DP\xml\log\" & X_DP_ReqId & ".signed", True)
            Dim postdata_signed As String = xmldoc.OuterXml
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postdata_signed)
            DP_objRequest.ContentLength = byteArray.Length
            Dim dataStream As Stream = DP_objRequest.GetRequestStream
            dataStream.Write(byteArray, 0, byteArray.Length)
            dataStream.Close()
            Dim objResponse As System.Net.HttpWebResponse
            Dim request_time_debut, request_time_fin As Date
            request_time_debut = Now
            objResponse = DP_objRequest.GetResponse()
            request_time_fin = Now
            DP_Connect.HEARTBEAT_variables.calcul_moyenne(DP_Connect.HEARTBEAT_variables.MetNbCr, DP_Connect.HEARTBEAT_variables.MetCre, 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\Clients\" & X_DP_ReqId & "_ok", StreamReader.ReadToEnd.ToString, False)
            DP.Next_Sequence_Number(objResponse)
            DP.Missing_Sequence_Number(objResponse, X_DP_ReqId)
        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)
                My.Computer.FileSystem.WriteAllText(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & "_error", StreamReader.ReadToEnd.ToString, False)
                StreamReader.Close()
            Else
                My.Computer.FileSystem.WriteAllText(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & "_ko", wex.Message.ToString, False)
                DP.reset_connection()
            End If

        Catch ex As Exception
            My.Computer.FileSystem.WriteAllText(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & "_ko", ex.Message.ToString, False)
        End Try

    End Sub

    Public Sub Refus_Création_relay(ByVal ReqXML As XmlDocument)

        Dim XMLelementliste = ReqXML.GetElementsByTagName("PRPA_IN201311UV02")

        Dim X_DP_ReqId As String = XMLelementliste.Item(0).Item("id").GetAttribute("extension")

        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 & "/refus_creation")

            DP_objRequest.Timeout = "10000"

            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.refuscreation+signature"


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

            DP_objRequest.Headers.Add("X-DP-Sequence-Number", X_DP_Sequence_Number.ToString)

            DP.DP_ReqTableAdapter.Insert(X_DP_Sequence_Number.ToString, X_DP_ReqId, "Local_Server", 1, Now, "", "")

            'signature
            DP.Sign_XML(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".xml", X_DP_ReqId, Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".signed")
            My.Computer.FileSystem.MoveFile(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".xml", Config.LocalNetwork_ServerName & "\DP\xml\log\" & X_DP_ReqId & ".xml", True)
            'envoi
            Dim xmldoc As New XmlDocument
            xmldoc.Load(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".signed")
            My.Computer.FileSystem.MoveFile(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".signed", Config.LocalNetwork_ServerName & "\DP\xml\log\" & X_DP_ReqId & ".signed", True)
            Dim postdata_signed As String = xmldoc.OuterXml
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postdata_signed)
            DP_objRequest.ContentLength = byteArray.Length
            Dim dataStream As Stream = DP_objRequest.GetRequestStream
            dataStream.Write(byteArray, 0, byteArray.Length)
            dataStream.Close()
            Dim objResponse As System.Net.HttpWebResponse
            Dim request_time_debut, request_time_fin As Date
            request_time_debut = Now
            objResponse = DP_objRequest.GetResponse()
            request_time_fin = Now
            DP_Connect.HEARTBEAT_variables.calcul_moyenne(DP_Connect.HEARTBEAT_variables.MetNbCr, DP_Connect.HEARTBEAT_variables.MetCre, 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\Clients\" & X_DP_ReqId & "_ok", StreamReader.ReadToEnd.ToString, False)
            DP.Next_Sequence_Number(objResponse)
            DP.Missing_Sequence_Number(objResponse, X_DP_ReqId)
        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)
                My.Computer.FileSystem.WriteAllText(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & "_error", StreamReader.ReadToEnd.ToString, False)
                StreamReader.Close()
            Else

                My.Computer.FileSystem.WriteAllText(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & "_ko", wex.Message.ToString, False)
                DP.reset_connection()
            End If

        Catch ex As Exception
            My.Computer.FileSystem.WriteAllText(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & "_ko", ex.Message.ToString, False)
        End Try


    End Sub

    Public Sub Suppression_relay(ByVal ReqXml As XmlDocument)
        Dim XMLelementliste = ReqXml.GetElementsByTagName("PRPA_IN201314UV02")

        Dim X_DP_ReqId As String = XMLelementliste.Item(0).Item("id").GetAttribute("extension")
        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 & "/suppression")

            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.suppression+signature"


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

            DP_objRequest.Headers.Add("X-DP-Sequence-Number", X_DP_Sequence_Number.ToString)

            DP.DP_ReqTableAdapter.Insert(X_DP_Sequence_Number.ToString, X_DP_ReqId, "Local_Server", 1, Now, "", "")

            'signature
            DP.Sign_XML(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".xml", X_DP_ReqId, Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".signed")
            My.Computer.FileSystem.MoveFile(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".xml", Config.LocalNetwork_ServerName & "\DP\xml\log\" & X_DP_ReqId & ".xml", True)
            'envoi
            Dim xmldoc As New XmlDocument
            xmldoc.Load(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".signed")
            My.Computer.FileSystem.MoveFile(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".signed", Config.LocalNetwork_ServerName & "\DP\xml\log\" & X_DP_ReqId & ".signed", True)
            Dim postdata_signed As String = xmldoc.OuterXml
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postdata_signed)
            DP_objRequest.ContentLength = byteArray.Length
            Dim dataStream As Stream = DP_objRequest.GetRequestStream
            dataStream.Write(byteArray, 0, byteArray.Length)
            dataStream.Close()
            Dim objResponse As System.Net.HttpWebResponse
            Dim request_time_debut, request_time_fin As Date
            request_time_debut = Now
            objResponse = DP_objRequest.GetResponse()
            request_time_fin = Now
            DP_Connect.HEARTBEAT_variables.calcul_moyenne(DP_Connect.HEARTBEAT_variables.MetNbSu, DP_Connect.HEARTBEAT_variables.MetSup, 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\Clients\" & X_DP_ReqId & "_ok", StreamReader.ReadToEnd.ToString, False)
            DP.Next_Sequence_Number(objResponse)
            DP.Missing_Sequence_Number(objResponse, X_DP_ReqId)
        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)
                My.Computer.FileSystem.WriteAllText(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & "_error", StreamReader.ReadToEnd.ToString, False)
                StreamReader.Close()
            Else
                My.Computer.FileSystem.WriteAllText(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & "_ko", wex.Message.ToString, False)
                DP.reset_connection()
            End If
        Catch ex As Exception

            My.Computer.FileSystem.WriteAllText(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & "_ko", ex.Message.ToString, False)
        End Try
    End Sub

    Public Sub Dispensation_relay(ByVal ReqXml As XmlDocument)
        Dim XMLelementliste = ReqXml.GetElementsByTagName("RCMR_IN000002UV02")

        Dim X_DP_ReqId As String = XMLelementliste.Item(0).Item("id").GetAttribute("extension")

        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 & "/alimentation")

            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", X_DP_ReqId)

            DP_objRequest.Headers.Add("X-DP-Sequence-Number", X_DP_Sequence_Number.ToString)

            DP.DP_ReqTableAdapter.Insert(X_DP_Sequence_Number.ToString, X_DP_ReqId, "Local_Server", 1, Now, "", "")

            'signature
            DP.Sign_XML(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".xml", X_DP_ReqId, Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".signed")
            My.Computer.FileSystem.MoveFile(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".xml", Config.LocalNetwork_ServerName & "\DP\xml\log\" & X_DP_ReqId & ".xml", True)
            'envoi
            Dim xmldoc As New XmlDocument
            xmldoc.Load(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".signed")
            My.Computer.FileSystem.MoveFile(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".signed", Config.LocalNetwork_ServerName & "\DP\xml\log\" & X_DP_ReqId & ".signed", True)
            Dim postdata_signed As String = xmldoc.OuterXml
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postdata_signed)
            DP_objRequest.ContentLength = byteArray.Length
            Dim dataStream As Stream = DP_objRequest.GetRequestStream
            dataStream.Write(byteArray, 0, byteArray.Length)
            dataStream.Close()
            Dim objResponse As System.Net.HttpWebResponse
            Dim request_time_debut, request_time_fin As Date
            request_time_debut = Now
            objResponse = DP_objRequest.GetResponse()
            request_time_fin = Now
            DP_Connect.HEARTBEAT_variables.calcul_moyenne(DP_Connect.HEARTBEAT_variables.MetNbAl, DP_Connect.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\Clients\" & X_DP_ReqId & "_ok", StreamReader.ReadToEnd.ToString, False)
            StreamReader.Close()
            DP.Next_Sequence_Number(objResponse)
            DP.Missing_Sequence_Number(objResponse, X_DP_ReqId)
        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)
                My.Computer.FileSystem.WriteAllText(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & "_error", StreamReader.ReadToEnd.ToString, False)
                StreamReader.Close()
            Else
                My.Computer.FileSystem.WriteAllText(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & "_ko", wex.Message.ToString, False)
                DP.reset_connection()
            End If

        Catch ex As Exception
            My.Computer.FileSystem.WriteAllText(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & "_ko", ex.Message.ToString, False)
        End Try


    End Sub

    Public Sub IAM_Send_requeste_relay(ByVal ReqXml As XmlDocument, ByVal X_DP_ReqId As String)
        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 = "10000"

            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", X_DP_ReqId)
            Dim postdata As String = ReqXml.OuterXml

            My.Computer.FileSystem.MoveFile(Config.LocalNetwork_ServerName & "\DP\Clients\" & X_DP_ReqId & ".xml", Config.LocalNetwork_ServerName & "\DP\XML\log\" & X_DP_ReqId & ".xml", True)
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postdata)
            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(X_DP_ReqId, "ALIMENTATION_IAM", "")


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


            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 = "ERREUR_TECHNIQUE" Then
                DP.DP_ReqTableAdapter.Insert("", X_DP_ReqId, "A_REEMETTRE", 1, Now.AddMinutes(60), "IAM", "IAM")
                DP.Write_Trace_Tech(X_DP_ReqId, "REP_ALIMENTATION_IAM", "ERREUR_TECHNIQUE")
            ElseIf réponse = "" Then
                DP.DP_ReqTableAdapter.Insert("", X_DP_ReqId, "OK", 1, Now, "IAM", "IAM")
                DP.Write_Trace_Tech(X_DP_ReqId, "REP_ALIMENTATION_IAM", "OK")
            Else
                DP.DP_ReqTableAdapter.Insert("", X_DP_ReqId, réponse, 1, Now, "IAM", "IAM")
                DP.Write_Trace_Tech(X_DP_ReqId, "REP_ALIMENTATION_IAM", réponse)
            End If


        Catch wex As System.Net.WebException
            DP.DP_ReqTableAdapter.Insert("", X_DP_ReqId, "A_REEMETTRE", 1, Now.AddMinutes(60), "IAM", "IAM")
            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_ALIMENTATION_IAM", CType(wex.Response, System.Net.HttpWebResponse).StatusCode & "|" & StreamReader.ReadToEnd.ToString)
                StreamReader.Close()
            Else
                DP.Write_Trace_Tech(X_DP_ReqId, "REP_ALIMENTATION_IAM", wex.Message.ToString)
                DP.reset_connection()
            End If
        Catch ex As Exception
            DP.Write_Trace_Tech(X_DP_ReqId, "REP_ALIMENTATION_IAM", ex.Message.ToString)
        End Try

    End Sub
End Class