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
| Private Function Post_request(ByVal url As String, ByVal data As String, Optional ByVal cookies As String = "", Optional ByVal proxy As String = Nothing, Optional ByVal proxyport As Integer = Nothing, Optional ByVal timeout As Integer = 30000, Optional ByVal login As String = "", Optional ByVal pass As String = "") As String
Dim reader As IO.StreamReader
Dim writer As IO.StreamWriter
Dim res As String = ""
Try
Dim hwebrequest As System.Net.HttpWebRequest = CType(System.Net.HttpWebRequest.Create(url), HttpWebRequest) ' on crée la requete web à partir de l'url
If proxy <> "" Then
Dim hproxy As WebProxy
hproxy = New WebProxy(proxy, proxyport)
WebRequest.DefaultWebProxy = hproxy
End If
If login <> "" Then
Dim hcredential As New System.Net.NetworkCredential(login, pass)
hwebrequest.Credentials = hcredential
End If
If cookies <> "" Then
hwebrequest.CookieContainer = New CookieContainer
hwebrequest.CookieContainer.SetCookies(New Uri(url.Substring(0, url.IndexOf("/", 8))), cookies)
End If
hwebrequest.Method = "POST" 'on defini la methode à utiliser
hwebrequest.ContentType = "application/x-www-form-urlencoded" 'on defini le type de contenu de la requete
hwebrequest.ContentLength = data.Length 'on defini la taille des données à envoyé
writer = New IO.StreamWriter(hwebrequest.GetRequestStream) 'on crée un objet streamwriter qui va nous permettre d'envoyer nos données# writer.Write(data) 'on ecrit les données
writer.Close() 'on ferme l'objet streamwriter
Dim hwebresponse As System.Net.HttpWebResponse = CType(hwebrequest.GetResponse, HttpWebResponse) 'on crée un objet de reponse a notre requete
reader = New IO.StreamReader(hwebresponse.GetResponseStream) 'on redefini notre streamreader avec la reponse à la requete en argument
res = reader.ReadToEnd() 'on renvoie l'ensemble du resultat
reader.Close() 'on ferme l'objet
Return res
Catch ex As Exception
Return ex.Message.ToString
End Try
End Function |
Partager