Click here to Skip to main content
15,356,705 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HI , I try POST on server some text but on server nothing came. I use a source:
How to: Send data by using the WebRequest class - .NET Framework | Microsoft Docs[^]
t

I edit it for vb.net with small diference: I need use specify port, I dont known if I wrote this well:
WebRequest.Create("http://10.000.10.123:1234")
, but when I run a code reply from server is "ok" in output.

What I have tried:

My code:

Dim request As WebRequest = WebRequest.Create("http://10.000.10.123:1234")
        request.Method = "POST"
        Dim postData As String = "GENESYS_TEST."
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = byteArray.Length
        Dim dataStream As Stream = request.GetRequestStream()
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()
        Dim response As WebResponse = request.GetResponse()
        Console.WriteLine((CType(response, HttpWebResponse)).StatusDescription)

        Using CSharpImpl.__Assign(dataStream, response.GetResponseStream())
            Dim reader As StreamReader = New StreamReader(dataStream)
            Dim responseFromServer As String = reader.ReadToEnd()
            Console.WriteLine(responseFromServer)
        End Using

        response.Close()
    End Sub

    Private Class CSharpImpl
        <Obsolete("Please refactor calling code to use normal Visual Basic assignment")>
        Shared Function __Assign(Of T)(ByRef target As T, value As T) As T
            target = value
            Return value
        End Function
Posted
Updated 18-May-22 22:45pm

1 solution

Now its working :

Dim request As WebRequest = WebRequest.Create("http://10.000.10.123:1234")
        request.Method = "POST"
        Dim postData As String = "GENESYS_TEST."
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = byteArray.Length
        Dim dataStream As Stream = request.GetRequestStream()
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()
        Dim response As WebResponse = request.GetResponse()
        Console.WriteLine((CType(response, HttpWebResponse)).StatusDescription)

        Using CSharpImpl.__Assign(dataStream, response.GetResponseStream())
            Dim reader As StreamReader = New StreamReader(dataStream)
            Dim responseFromServer As String = reader.ReadToEnd()
            Console.WriteLine(responseFromServer)
        End Using

        response.Close()
    End Sub

    Private Class CSharpImpl
        <Obsolete("Please refactor calling code to use normal Visual Basic assignment")>
        Shared Function __Assign(Of T)(ByRef target As T, value As T) As T
            target = value
            Return value
        End Function
   
Comments
Richard Deeming 19-May-22 6:22am
   
The "working" code from your solution is identical to the "not working" code from your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900