VB.NET笔记 POST舟道网一例

本文介绍了一种通过VB.NET实现的HTTP请求与响应处理方法,包括如何获取sessionID及使用POST方法发送请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Function session() As String
        Dim url As New Uri("https://www.myzhoudao.com/homeindex/cntrSecondPage.jsp") '目标网址
        Dim req As HttpWebRequest = TryCast(WebRequest.Create(url), HttpWebRequest) '定义一个请求
        Dim rep As HttpWebResponse = TryCast(req.GetResponse, HttpWebResponse) '发送请求并定义一个响应
        Dim read_rep As String = New StreamReader(rep.GetResponseStream, Encoding.UTF8).ReadToEnd '获取响应并读取为文本格式
        Dim sss As String = Regex.Match(read_rep, "jsessionid.*?\d{9}").Value '过滤
        req.Method = "get"
        Return Mid(sss, 12)
    End Function
    Sub POST_Example()
        Dim url As New Uri("https://www.myzhoudao.com/idx/logisticsIndexAction!quickQueryJzx.action") '目标网址
        Dim req As HttpWebRequest = TryCast(WebRequest.Create(url), HttpWebRequest) '定义一个请求
        Dim postCookieContain As CookieContainer = New CookieContainer '定义一个cookie容器
        Dim postCookie As Cookie = New Cookie("jsessionid", session) '设置cookie
        postCookieContain.Add(url, postCookie) '将cookie添加到容器里


        '请求报头设置
        With req
            .Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
            .Connection = "keepalive"
            .ContentLength = 14
            .ContentType = "application/x-www-form-urlencoded"
            .Referer = "https://www.myzhoudao.com/homeindex/cntrSecondPage.jsp"
            .UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:57.0) Gecko/20100101 Firefox/57.0"
            .CookieContainer = postCookieContain '关联上cookie容器
            .Method = "post"
        End With


        '将请求数据写入请求报文
req.GetRequestStream.Write(Encoding.ASCII.GetBytes("xh=TBJU7279865"),0,Encoding.ASCII.GetBytes("xh=TBJU7279865").Length)


        '发送请求报文并获得响应正文
        Dim rep As HttpWebResponse = TryCast(req.GetResponse, HttpWebResponse)
        Dim read_rep As String = New StreamReader(rep.GetResponseStream, Encoding.UTF8).ReadToEnd '将响应报文读取为文本格式
        Console.WriteLine(read_rep)
    End Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值