VB .NET通过URL POST请求和GET请求

Imports System.IO
Imports System.Net
Imports System.Text
Public Shared Function GetData(ByVal url As String, ByVal data As String) As String  
   
        Dim request As HttpWebRequest = WebRequest.Create(url + "?" + data)  
        request.Method = "GET"  
        Dim sr As StreamReader = New StreamReader(request.GetResponse().GetResponseStream)  
        Return sr.ReadToEnd  
End Function  
   
Public Shared Function PostData(ByVal url As String, ByVal data As String) As String  
   
        ServicePointManager.Expect100Continue = False  
        Dim request As HttpWebRequest = WebRequest.Create(url)  
        '//Post请求方式  
        request.Method = "POST"  
   
        '内容类型  
        request.ContentType = "application/x-www-form-urlencoded"  
        '将URL编码后的字符串转化为字节  
        Dim encoding As New UTF8Encoding()  
        Dim bys As Byte() = encoding.GetBytes(data)  
        '设置请求的 ContentLength   
        request.ContentLength = bys.Length  
        '获得请 求流  
        Dim newStream As Stream = request.GetRequestStream()  
        newStream.Write(bys, 0, bys.Length)  
        newStream.Close()  
        '获得响应流  
        Dim sr As StreamReader = New StreamReader(request.GetResponse().GetResponseStream)  
        Return sr.ReadToEnd  
End Function

HTTP POST GET 带cookie

Imports System.Net
Imports System.IO
 
Public Class Form1
Dim cookies As New CookieContainer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(httpsend_get("http://www.baidu.com"))
End Sub
Function httpsend_get(ByVal url As String) As String
Dim webRequest As HttpWebRequest
webRequest = CType(Net.WebRequest.Create(url), HttpWebRequest)
webRequest.Method = "get"
webRequest.ContentType = "application/x-www-form-urlencoded"
webRequest.CookieContainer = cookies '加上上次的cookie
Dim responseReader As StreamReader
responseReader = New StreamReader(webRequest.GetResponse().GetResponseStream())
 
'遍历cookies转换成字符串
Dim currCookies As String = ""
For Each cook In cookies.GetCookies(New Uri(url))
currCookies = currCookies + cook.ToString() + vbCrLf
Next
MsgBox(currCookies) '显示cookie
Return responseReader.ReadToEnd()
responseReader.Close()
 
End Function
 
Function httpsend_post(ByVal url As String, ByVal data As String) As String
Dim webRequest As HttpWebRequest
webRequest = CType(Net.WebRequest.Create(url), HttpWebRequest)
webRequest.Method = "get"
webRequest.ContentType = "application/x-www-form-urlencoded"
webRequest.CookieContainer = cookies '加上上次的cookie
 
Dim requestWriter As StreamWriter
requestWriter = New StreamWriter(webRequest.GetRequestStream())
requestWriter.Write(data)
requestWriter.Close()
 
Dim responseReader As StreamReader
responseReader = New StreamReader(webRequest.GetResponse().GetResponseStream())
 
'遍历cookies转换成字符串
Dim currCookies As String = ""
For Each cook In cookies.GetCookies(New Uri(url))
currCookies = currCookies + cook.ToString() + vbCrLf
Next
MsgBox(currCookies) '显示cookie
Return responseReader.ReadToEnd()
responseReader.Close()
 
End Function
 
End Class

发表评论

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据