DOT NET POST and GET

2013-09-09

vb.net http post and get function

GET

Public Function PostData(ByRef url As String, ByRef data As String) As String
    'Address of URL
    Dim request As String = url & "?" & data
    ' Get HTML data
    Dim client As WebClient = New WebClient()
    Dim data As Stream = client.OpenRead(request)
    Dim reader As StreamReader = New StreamReader(data)
    Dim str As String = ""
    str = reader.ReadToEnd
End Function


POST

Public Function PostData(ByRef url As String, ByRef data As String) As String
    Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
    request.Method = "POST"
    request.ContentType = "application/x-www-form-urlencoded"
    Dim postData As String = data
    request.ContentLength = postData.Length

    Dim writer As New StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII)

    writer.Write(postData)
    writer.Close()

    Dim stream As Stream = request.GetResponse().GetResponseStream()
    Dim reader As New StreamReader(stream)
    Dim response As String = String.Empty
    response = reader.ReadToEnd
    Return response
End Function

Contact

Github

Codepen

歡迎參觀我的賣場
© 2013 Copyright Digishot Web | Design Tools
Visitors【526555】
digishot webdesign studio