求一段asp页面post远程网站并获取返回值显示到当前页面希望代码完整,有注释。 100
1个回答
展开全部
Private Function httpPost(ByVal content As String, ByVal url As String) As String
Try
Dim sPostData As String = content '要post的内容
Dim ByteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(sPostData) 'utf8
Dim hwRequest As HttpWebRequest = WebRequest.Create(url) '发送到的运程地址
hwRequest.Method = "POST"
hwRequest.ContentType = "application/x-www-form-urlencoded"
hwRequest.ContentLength = ByteArray.Length
Dim newStream As Stream = hwRequest.GetRequestStream()
newStream.Write(ByteArray, 0, ByteArray.Length)
newStream.Close()
'获得响应
Dim response As HttpWebResponse = hwRequest.GetResponse
newStream = response.GetResponseStream
Dim reader As New StreamReader(newStream)
Dim str As String = reader.ReadToEnd '响应字符串
reader.Close()
newStream.Close()
response.Close()
Return str
Catch ex As Exception
Return ex.Message
End Try
End Function
更多追问追答
追问
非常感谢,请问我新建的页面后缀应该是什么 ? 这段代码放在什么页面代码什么位置,请不要见笑 完全不懂
追答
一两句根本没法说清楚。 我觉的你还是先从基础来吧,否则你很难学习的。先装个 visual studio环境 ,学习一下最VB.net的基本语法。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询