IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

Convertir curl en vb


Sujet :

VB.NET

  1. #1
    Membre averti
    Inscrit en
    Octobre 2009
    Messages
    33
    Détails du profil
    Informations forums :
    Inscription : Octobre 2009
    Messages : 33
    Par défaut Convertir curl en vb
    Bonjour,

    Mon fournisseur me fourni un exemple de requête curl que je voudrai utiliser dans du code vb.

    Exemple de requête fourni par mon fournisseur:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    curl -X POST "http://adresse.fr/api/auth/login?api_key=CleAPI" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"login\": \"MonLogin\", \"password\": \"MonMdp\"}"
    Voici mon code (j'ai bien entendu remplacé adresse.fr, CleAPI, MonLogin et MonMdp par les véritables valeurs) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
        Private Sub test2()
            Dim myReq As HttpWebRequest
            Dim myResp As HttpWebResponse
            Dim reader As StreamReader
     
            Try
                myReq = HttpWebRequest.Create("http://adresse.fr/api/auth/login?api_key=CleAPI")
     
                myReq.Method = "POST"
                myReq.ContentType = "application/json"
                myReq.Accept = "application/json"
                Dim myData As String = """{ \""login\"": \""MonLogin\"", \""password\"": \""MonMdp\""}"""
                MsgBox(1)
                myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count)
                MsgBox(2)
                myResp = myReq.GetResponse
                MsgBox(3)
                Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
                Dim myText As String
                myText = myreader.ReadToEnd()
     
                'Me.txtMuestra.Items.Add(myText)
                MsgBox(myText)
            Catch ex As Exception
                'txtMuestra.Items.Add(ex)
                MsgBox(ex)
            End Try
        End Sub
    Lors de l'exécution, j'obtiens bien le msgbox 1 et le 2 mais jamais le 3.

    Avez-vous une idée ?

    Merci.

  2. #2
    Membre Expert
    Avatar de wallace1
    Homme Profil pro
    Administrateur systèmes
    Inscrit en
    Octobre 2008
    Messages
    1 966
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Administrateur systèmes
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Octobre 2008
    Messages : 1 966
    Billets dans le blog
    7
    Par défaut
    Bonjour,

    Qqch comme ca :

    https://stackoverflow.com/questions/...l-using-vb-net

    Bon codage++

  3. #3
    Membre averti
    Inscrit en
    Octobre 2009
    Messages
    33
    Détails du profil
    Informations forums :
    Inscription : Octobre 2009
    Messages : 33
    Par défaut
    Bonjour,
    Merci pour la réponse.
    J'ai récupéré la fonction.
    Mais je ne parviens pas à récupérer la réponse du serveur:
    "La connexion sous-jacente a été fermée"

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    Imports System.IO 'pour stream
    Imports System.Net 'pour webrequest
    Imports System.Text 'pour encoding
     
    Public Class Form3
     
        Private Function SendRequest(ByVal uri As Uri, ByVal jsonDataBytes As Byte(), ByVal contentType As String, ByVal method As String) As String
            Dim response As String
            Dim request As WebRequest
     
            request = WebRequest.Create(uri)
            System.Net.ServicePointManager.UseNagleAlgorithm = False
            System.Net.ServicePointManager.Expect100Continue = False
            request.ContentLength = jsonDataBytes.Length
            request.ContentType = contentType
            request.Method = method
     
            Using requestStream = request.GetRequestStream
                requestStream.Write(jsonDataBytes, 0, jsonDataBytes.Length)
                requestStream.Close()
     
                Using responseStream = request.GetResponse.GetResponseStream
                    Using reader As New StreamReader(responseStream)
                        response = reader.ReadToEnd()
                    End Using
                End Using
            End Using
     
            Return response
        End Function
     
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim data = Encoding.UTF8.GetBytes("""{ \""login\"": \""Monlogin\"", \""password\"": \""MonMdp\""}""")
            Dim uri As New Uri("http://apiv2.frizbi.evolnet.fr/api/auth/login?api_key=MonApiKey")
            Dim result_post = SendRequest(uri, data, "application/json", "POST")
        End Sub
    End Class

    L'exception System.Net.WebException n'a pas été gérée
    Message="La connexion sous-jacente a été fermée*: Une erreur inattendue s'est produite lors de l'envoi."
    Source="System"
    StackTrace:
    à System.Net.HttpWebRequest.GetResponse() à EnvoiSMS.Form3.SendRequest(Uri uri, Byte[] jsonDataBytes, String contentType, String method) dans C:\Users\cma\MACON habitat\Service Informatique - General\Dev Interne\Programmes VB\EnvoiSMS_via_J2S\EnvoiSMS\EnvoiSMS\Form3.vb:ligne 22 à EnvoiSMS.Form3.Button1_Click(Object sender, EventArgs e) dans C:\Users\cma\MACON habitat\Service Informatique - General\Dev Interne\Programmes VB\EnvoiSMS_via_J2S\EnvoiSMS\EnvoiSMS\Form3.vb:ligne 35 à System.Windows.Forms.Control.OnClick(EventArgs e) à System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) à System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) à System.Windows.Forms.Control.WndProc(Message& m) à System.Windows.Forms.ButtonBase.WndProc(Message& m) à System.Windows.Forms.Button.WndProc(Message& m) à System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) à System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) à System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) à System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) à System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) à System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) à EnvoiSMS.My.MyApplication.Main(String[] Args) dans 17d14f5c-a337-4978-8281-53493378c1071.vb:ligne 81 à System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) à System.Threading.ThreadHelper.ThreadStart()
    InnerException: System.IO.IOException
    Message="Réception d'un EOF inattendu ou de 0 octet du flux de transport."
    Source="System"
    StackTrace:
    à System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count) à System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) à System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) à System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest) à System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult) à System.Threading.ExecutionContext.runTryCode(Object userData) à System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) à System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result) à System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size) à System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size) à System.Net.ConnectStream.WriteHeaders(Boolean async)
    InnerException:

Discussions similaires

  1. [PowerShell] convertir commande cURL
    Par aberzen dans le forum Scripts/Batch
    Réponses: 1
    Dernier message: 29/01/2020, 21h13
  2. [XL-2016] Convertir une commande Curl en URL
    Par lololulu dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 20/06/2019, 15h24
  3. Convertir un fichier avec CURL
    Par Stalk3R dans le forum Langage
    Réponses: 8
    Dernier message: 08/12/2010, 18h47
  4. [1.x] [JSON] [REST] [CURL] convertir réponse restful to object
    Par kawe22 dans le forum Symfony
    Réponses: 7
    Dernier message: 19/10/2010, 19h47
  5. convertir un nom long (win32) en format dos (8+3)
    Par kylekiller dans le forum Langage
    Réponses: 2
    Dernier message: 30/08/2002, 13h34

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo