-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpHelper.vb
60 lines (51 loc) · 2.09 KB
/
HttpHelper.vb
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
Imports System.IO
Imports System.Net
Imports System.Text
Namespace HTSQuery
''' <summary>
''' Helper class which can perform HTTP GET
''' and HTTP POST operations.
''' </summary>
Public Class HttpHelper
''' <summary>
''' Performs a Http Get operation.
''' </summary>
''' <param name="url">A valid Url.</param>
''' <returns>Response from server</returns>
Public Shared Function HttpGET(url As String) As String
Using web As New WebClient()
Dim response As String = web.DownloadString(url)
Return response
End Using
End Function
''' <summary>
''' Posts data to the specified URL.
''' </summary>
''' <param name="url">The URL.</param>
''' <param name="data">The data.</param>
''' <returns>Response from server</returns>
''' <remarks>Code here helped in writing this method
''' http://msdn.microsoft.com/en-us/library/debx8sh9.aspx
''' </remarks>
Public Shared Function HttpPOST(url As String, data As String) As String
Dim responseData As String
Dim request As WebRequest = WebRequest.Create(url)
request.Method = "POST"
request.ContentType = "application/json"
Dim byteData As Byte() = Encoding.UTF8.GetBytes(data)
request.ContentLength = byteData.Length
Using stream As Stream = request.GetRequestStream()
stream.Write(byteData, 0, byteData.Length)
stream.Close()
End Using
Using response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
Using dataStream As Stream = response.GetResponseStream()
Using reader As New StreamReader(dataStream)
responseData = reader.ReadToEnd()
End Using
End Using
End Using
Return responseData
End Function
End Class
End Namespace