请求方式:POST
接口版本: v2
接口描述:
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 | 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://api-v2.1dq.com/v2/Oss/getTempAuth.json" Dim params As String = "" 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 |