'用法:
1 |
httpsend_get("http://hujianbo.cn/archives/105") |
完整代码:
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
Function httpsend_get(ByVal geturl As String) As String Dim myHttpWebRequest As System.Net.HttpWebRequest Dim myHttpWebResponse As System.Net.HttpWebResponse Try Dim URL As String = geturl Dim myUri As Uri = New Uri(geturl) Dim myWebRequest As System.Net.WebRequest = System.Net.WebRequest.Create(URL) myHttpWebRequest = CType(myWebRequest, System.Net.HttpWebRequest) myHttpWebRequest.KeepAlive = True myHttpWebRequest.Timeout = 300000 myHttpWebRequest.Method = "GET" Dim myWebResponse As System.Net.WebResponse = myHttpWebRequest.GetResponse() '获得响应信息 myHttpWebResponse = CType(myWebResponse, System.Net.HttpWebResponse) Dim iStatCode As Integer = CInt(myHttpWebResponse.StatusCode) myHttpWebRequest = CType(myWebRequest, System.Net.HttpWebRequest) myHttpWebResponse = CType(myWebResponse, System.Net.HttpWebResponse) Dim myStream As System.IO.Stream = myHttpWebResponse.GetResponseStream() Dim srReader As System.IO.StreamReader = New System.IO.StreamReader(myStream, System.Text.Encoding.Default) Dim sTemp As String = srReader.ReadToEnd() httpsend_get = sTemp srReader.Close() myStream.Close() myWebResponse.Close() myWebRequest.Abort() Catch WebExcp As System.Net.WebException 'Response.Write(Replace(WebExcp.Message.ToString(), "The remote server returned an error: (500) Internal Server Error.", "服务器出现故障无法连接")) httpsend_get = Replace(WebExcp.Message.ToString(), "The remote server returned an error: (500) Internal Server Error.", "服务器出现故障无法连接") Catch ex As Exception 'Response.Write(ex.ToString()) httpsend_get = ex.ToString End Try End Function '=========================================================================================== 'HTTP请求/回应 _END '=========================================================================================== 'HTTP读取 'HTTP 1.1 POST方式 'geturl=网址 'other=?号后面的参数 Function http_post(ByVal geturl As String, ByVal text As String) As String Dim myHttpWebRequest As System.Net.HttpWebRequest Dim myHttpWebResponse As System.Net.HttpWebResponse Try Dim URL As String = geturl Dim myUri As Uri = New Uri(geturl) Dim myWebRequest As System.Net.WebRequest = System.Net.WebRequest.Create(URL) myHttpWebRequest = CType(myWebRequest, System.Net.HttpWebRequest) myHttpWebRequest.KeepAlive = True myHttpWebRequest.Timeout = 3000000 myHttpWebRequest.Method = "POST" Dim postdata As String = URLEncoding(text) Dim encoding As New System.Text.ASCIIEncoding Dim byte1 As Byte() = encoding.GetBytes(postdata) myHttpWebRequest.ContentType = "application/x-www-form-urlencoded" myHttpWebRequest.ContentLength = postdata.Length Dim newStream As System.IO.Stream = myHttpWebRequest.GetRequestStream() newStream.Write(byte1, 0, byte1.Length) newStream.Close() Dim myWebResponse As System.Net.WebResponse = myHttpWebRequest.GetResponse() myHttpWebResponse = CType(myWebResponse, System.Net.HttpWebResponse) Dim iStatCode As Integer = CInt(myHttpWebResponse.StatusCode) myHttpWebResponse = CType(myWebResponse, System.Net.HttpWebResponse) Dim myStream As System.IO.Stream = myHttpWebResponse.GetResponseStream() Dim srReader As System.IO.StreamReader = New System.IO.StreamReader(myStream, encoding.Default) Dim sTemp As String = srReader.ReadToEnd() http_post = sTemp srReader.Close() myStream.Close() myWebResponse.Close() myWebRequest.Abort() Catch WebExcp As System.Net.WebException http_post = Replace(WebExcp.Message.ToString(), "The remote server returned an error: (500) Internal Server Error.", "服务器出现故障无法连接") Catch ex As Exception http_post = ex.ToString() End Try End Function 'Unicode转换 Function URLEncoding(ByVal vstrIn) As String Dim strReturn As String Dim i As Integer Dim ThisChr As String Dim innerCode As Integer Dim Hight8 As Integer Dim Low8 As Integer strReturn = "" For i = 1 To Len(vstrIn) ThisChr = Mid(vstrIn, i, 1) If Math.Abs(Asc(ThisChr)) < &HFF Then strReturn = strReturn & ThisChr Else innerCode = Asc(ThisChr) If innerCode < 0 Then innerCode = innerCode + &H10000 End If Hight8 = (innerCode And &HFF00) &HFF Low8 = innerCode And &HFF strReturn = strReturn & "%" & Hex(Hight8) & "%" & Hex(Low8) End If Next URLEncoding = strReturn strReturn = Nothing i = Nothing ThisChr = Nothing innerCode = Nothing Hight8 = Nothing Low8 = Nothing End Function '反解 URLEncoding Function UTF2GB(ByVal UTFStr) Dim gbstr Dim dig For dig = 1 To Len(UTFStr) If Mid(UTFStr, dig, 1) = "%" Then If Len(UTFStr) >= dig + 8 Then gbstr = gbstr & ConvChinese(Mid(UTFStr, dig, 9)) dig = dig + 8 Else gbstr = gbstr & Mid(UTFStr, dig, 1) End If Else gbstr = gbstr & Mid(UTFStr, dig, 1) End If Next UTF2GB = gbstr End Function Function ConvChinese(ByVal x) Dim A = Split(Mid(x, 2), "%") Dim i = 0 Dim j = 0 Dim un_icode Dim digs For i = 0 To UBound(A) A(i) = c16to2(A(i)) Next For i = 0 To UBound(A) - 1 digs = InStr(A(i), "0") un_icode = "" For j = 1 To digs - 1 If j = 1 Then A(i) = Right(A(i), Len(A(i)) - digs) un_icode = un_icode & A(i) Else i = i + 1 A(i) = Right(A(i), Len(A(i)) - 2) un_icode = un_icode & A(i) End If Next If Len(c2to16(un_icode)) = 4 Then ConvChinese = ConvChinese & ChrW(Int("&H" & c2to16(un_icode))) Else ConvChinese = ConvChinese & Chr(Int("&H" & c2to16(un_icode))) End If Next End Function Function c2to16(ByVal x) Dim i = 1 For i = 1 To Len(x) Step 4 c2to16 = c2to16 & Hex(c2to10(Mid(x, i, 4))) Next End Function Function c2to10(ByVal x) c2to10 = 0 If x = "0" Then Exit Function Dim i = 0 For i = 0 To Len(x) - 1 If Mid(x, Len(x) - i, 1) = "1" Then c2to10 = c2to10 + 2 ^ (i) Next End Function Function c16to2(ByVal x) Dim i = 0 Dim tempstr For i = 1 To Len(Trim(x)) tempstr = c10to2(CInt(Int("&h" & Mid(x, i, 1)))) Do While Len(tempstr) < 4 tempstr = "0" & tempstr Loop c16to2 = c16to2 & tempstr Next End Function Function c10to2(ByVal x) Dim mysign = System.Math.Sign(x) Dim tempnum Dim i x = System.Math.Abs(x) Dim DigS = 1 Do If x < 2 ^ DigS Then Exit Do Else DigS = DigS + 1 End If Loop tempnum = x i = 0 For i = DigS To 1 Step -1 If tempnum >= 2 ^ (i - 1) Then tempnum = tempnum - 2 ^ (i - 1) c10to2 = c10to2 & "1" Else c10to2 = c10to2 & "0" End If Next If mysign = -1 Then c10to2 = "-" & c10to2 End Function |
原文链接:HTTP GET/POST 调用方法 VB.NET,转载请注明来源!