zoukankan      html  css  js  c++  java
  • Asp.net 获取网页源代码

    测试环境:VS2008+VB.net

     1     Public Shared Function GetWebContent(ByVal sURL As String) As String
    2 Try
    3 Dim request As Net.WebRequest
    4 Dim response As Net.WebResponse
    5 request = Net.WebRequest.Create(sURL)
    6 response = request.GetResponse()
    7 Dim reader As New System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")) //读取的编码
    8 Dim s As String = reader.ReadToEnd
    9 reader.Close()
    10 reader.Dispose()
    11 response.Close()
    12 Return s
    13 Catch ex As Exception
    14 Return ""
    15 End Try
    16 End Function

    提交数据(Post)并获取返回值

     1     Public Shared Function PostData(ByVal sUrl As String, ByVal sData As String) As String
    2 Try
    3 Dim Encoding As New ASCIIEncoding
    4 Dim bData As Byte() = Encoding.GetBytes(sData)
    5 Dim Request As Net.WebRequest = Net.WebRequest.Create(sUrl)
    6 Request.Method = "POST"
    7 Request.ContentType = "application/x-www-form-urlencoded"
    8 Request.ContentLength = bData.Length
    9 Dim newStream As System.IO.Stream = Request.GetRequestStream()
    10 newStream.Write(bData, 0, sData.Length)
    11 newStream.Close()
    12 Dim Response As Net.WebResponse = Request.GetResponse
    13 Dim Reader As New System.IO.StreamReader(Response.GetResponseStream(), Text.Encoding.GetEncoding("utf-8"))
    14 Dim content As String = Reader.ReadToEnd
    15 Reader.Close()
    16 Reader.Dispose()
    17 Response.Close()
    18 Return content
    19 Catch ex As Exception
    20 Return ""
    21 End Try
    22 End Function

    只是需要注意一点,如果提交的参数中包含中文,需要进行编码。提交的Url需要使用绝对路径,开发环境中可以使用http://localhost:端口/目录/

    1 Dim sUrl As String="网址" //必须是绝对路径。如http://www.xxx.com/提交的页面
    2 Dim sData As String
    3 sData="try="+System.Web.HttpUtility.UrlEncode("测试")+"&try2="+System.Web.HttpUtility.UrlEncode("测试2")
    4 Response.Write(PostData(sUrl,sData))



     

  • 相关阅读:
    Git 获取远程分支
    entOS查看系统信息-CentOS查看命令
    CentOS6.5下用yum安装 git
    CENTOS如何禁用ROOT本地或远程SSH登录
    ProtoBuf练习(二)
    ProtoBuf练习(一)
    ProtoBuf练习
    Protocol Buffers官方文档(开发指南)
    Protocol Buffers官方文档(proto3语言指南)
    Boost Python学习笔记(五)
  • 原文地址:https://www.cnblogs.com/imlions/p/2337715.html
Copyright © 2011-2022 走看看