vb.net 2003发送post请求
时间: 2023-06-24 20:07:24 浏览: 182
在 VB.NET 2003 中,可以使用 `HttpWebRequest` 类发送 POST 请求。以下是示例代码:
```vb.net
Dim request As HttpWebRequest = CType(WebRequest.Create("http://example.com"), HttpWebRequest)
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
Dim postData As String = "param1=value1¶m2=value2"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
request.ContentLength = byteArray.Length
Dim dataStream As Stream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
Dim responseStream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(responseStream)
Dim responseString As String = reader.ReadToEnd()
Console.WriteLine(responseString)
reader.Close()
responseStream.Close()
response.Close()
```
在此示例中,我们首先创建了一个 `HttpWebRequest` 对象,然后设置其方法为 "POST"。然后,我们设置请求的内容类型为 "application/x-www-form-urlencoded",这是 POST 请求的默认类型。接下来,我们设置 POST 请求的参数并将它们转换为字节数组,然后将其写入请求流中。最后,我们使用 `GetResponse` 方法来获取服务器的响应。在获得响应后,我们可以读取响应流并关闭所有相关的流和响应对象。
请注意,此示例仅供参考,具体实现可能因实际应用而异。
阅读全文