post data through httpWebRequest

前端 未结 5 1272
情话喂你
情话喂你 2020-12-08 22:42

I need to \"Post\" some data to an external website using HttpWebRequest object from my application(desktop) and get a response back into my application through

5条回答
  •  萌比男神i
    2020-12-08 23:22

    I use this function to post data. But the url you pass has to be formatted as such for example

    http://example.com/login.php?userid=myid&password=somepassword

    Private Function GetHtmlFromUrl(ByVal url As String) As String
    
            If url.ToString() = vbNullString Then
                Throw New ArgumentNullException("url", "Parameter is null or empty")
            End If
            Dim html As String = vbNullString
            Dim request As HttpWebRequest = WebRequest.Create(url)
            request.ContentType = "Content-Type: application/x-www-form-urlencoded"
            request.Method = "POST"
    
    
            Try
                Dim response As HttpWebResponse = request.GetResponse()
                Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
                html = Trim$(reader.ReadToEnd)
                GetHtmlFromUrl = html
            Catch ex As WebException
                GetHtmlFromUrl = ex.Message
            End Try
    
        End Function
    

提交回复
热议问题