VB.NET笔记 POST舟道网一例

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

猜你喜欢

转载自blog.csdn.net/stupidzhuang/article/details/80216642