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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| '********************************************************************************************
'* Fonction qui envoie par POST les data sur l'url et retourne le contenu de la réponse *
'********************************************************************************************
Public Function PostData(ByVal url As String, ByVal data As String) As String
Dim dataStream As Stream
Dim reader As StreamReader
Dim response As WebResponse
Try
Dim request As WebRequest
' Create a request using a URL that can receive a post.
request = WebRequest.Create(url)
' Set the Method property of the request to POST.
request.Method = "POST"
' Create POST data and convert it to a byte array.
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(data)
' Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded"
' Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length
' Get the request stream.
dataStream = request.GetRequestStream()
' Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length)
' Close the Stream object.
dataStream.Close()
' Get the response.
response = request.GetResponse()
' Display the status.
'literal.Text = CType(response, HttpWebResponse).StatusDescription + "<br>"
' Get the stream containing content returned by the server.
dataStream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
reader = New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
Return responseFromServer
Catch ex As Exception
If isErrorEnabled Then
log.Error(Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString + " " + Reflection.MethodBase.GetCurrentMethod().ToString, ex)
End If
Return ""
Finally
' Clean up the streams.
reader.Close()
dataStream.Close()
response.Close()
End Try
End Function |
Partager