Imports System.Text Imports System.IO Imports System.Net Imports System.Web Class APIStore 'HTTP的Post请求方式(推荐) 'strUrl 请求地址 'param 请求数据 Public Function APIStorePost(strUrl As String, param As String) As String Dim httpWebRequest As HttpWebRequest = TryCast(WebRequest.Create(strUrl), HttpWebRequest) httpWebRequest.Method = "POST" '指定允许数据发送的请求的一个协议方法 httpWebRequest.ContentType = "application/x-www-form-urlencoded" '设置 ContentType 属性设置为适当的值 Dim data As Byte() = Encoding.UTF8.GetBytes(param) Using stream As Stream = httpWebRequest.GetRequestStream() '写入数据 stream.Write(data, 0, data.Length) End Using Dim webResponse As WebResponse = TryCast(httpWebRequest.GetResponse(), HttpWebResponse) '发起请求,得到返回对象 Dim dataStream As Stream = webResponse.GetResponseStream() Dim reader As New StreamReader(dataStream, Encoding.UTF8) Dim returnStr As String = reader.ReadToEnd() ' Clean up the streams and the response. reader.Close() webResponse.Close() Return returnStr End Function 'HTTP的Get请求方式 'strUrl 请求地址 'param 请求数据 Public Function APIStoreGet(strUrl As String, param As String) As String Dim httpWebRequest As HttpWebRequest = TryCast(WebRequest.Create(Convert.ToString(strUrl & Convert.ToString("?")) & param), HttpWebRequest) httpWebRequest.Method = "GET" '指定允许数据发送的请求的一个协议方法 httpWebRequest.ContentType = "application/x-www-form-urlencoded" '设置 ContentType 属性设置为适当的值 Dim webResponse As WebResponse = TryCast(httpWebRequest.GetResponse(), HttpWebResponse) '发起请求,得到返回对象 Dim dataStream As Stream = webResponse.GetResponseStream() Dim reader As New StreamReader(dataStream, Encoding.UTF8) Dim returnStr As String = reader.ReadToEnd() ' Clean up the streams and the response. reader.Close() webResponse.Close() Return returnStr End Function '获取内容 '@param type 请求方式,POST,GET,推荐POST Public Sub getBalance(type As String) Dim url As String="https://v.1dq.com/api/v4/bankCard" Dim params As String="key=您申请的key&bankcard=123456789" Dim returnStr As String = Nothing If type.Equals("GET") Then returnStr = Convert.ToString("get result:") & Me.APIStoreGet(url, param) Else returnStr = Convert.ToString("post result:") & Me.APIStorePost(url, param) End If Console.WriteLine(returnStr) End Sub End Class