Get data with post method using webclient

StewartBW 1,765 Reputation points
2024-05-30T13:11:52.0833333+00:00

Hello

There's a POST sample at the bottom of this page:

https://ws.interfax.net/admin.asmx?op=GetAccountPPCardsBalance2

Sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.

POST /admin.asmx/GetAccountPPCardsBalance2 HTTP/1.1

Host: ws.interfax.net

Content-Type: application/x-www-form-urlencoded

Content-Length: length

PrimaryUsername=string&PrimaryPassword=string

I've no idea how to figure it out using a webclient and get the data back which is an integer number.

Anyone can help please?

Thanks in advance

Developer technologies VB
Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2024-05-31T01:47:42.0266667+00:00

    Hi @StewartBW ,

    Check the following steps.

    1. Create an instance of HttpClient to manage the HTTP communication.
    2. Use StringContent class to prepare the request body with the necessary form URL-encoded parameters (PrimaryUsername and PrimaryPassword).
    3. Use PostAsync method to send the POST request to the specified URL.
    4. Use the XDocument class to resolve Balance and ResultCode from the responding XML.
            Dim url As String = "https://ws.interfax.net/admin.asmx/GetAccountPPCardsBalance2"
            Dim primaryUsername As String = "your_primary_username"
            Dim primaryPassword As String = "your_primary_password"
    
            Using client As New HttpClient()
                Dim content As New StringContent($"PrimaryUsername={primaryUsername}&PrimaryPassword={primaryPassword}", Encoding.UTF8, "application/x-www-form-urlencoded")
    
                Dim response As HttpResponseMessage = Await client.PostAsync(url, content)
    
                response.EnsureSuccessStatusCode()
    
                Dim responseContent As String = Await response.Content.ReadAsStringAsync()
    
                Dim doc As XDocument = XDocument.Parse(responseContent)
                Dim ns As XNamespace = "http://interfax.net/Admin"
    
                Dim balance As Decimal = Decimal.Parse(doc.Root.Element(ns + "Balance").Value)
                Dim resultCode As Integer = Integer.Parse(doc.Root.Element(ns + "ResultCode").Value)
            End Using
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.