none
VB.net 如何实现 HTTP的POST和GET请求,并读取请求后的网页文本 RRS feed

全部回复

  • MSDN 有范例
    如何:使用 WebRequest 类发送数据
    http://msdn.microsoft.com/zh-cn/library/debx8sh9(VS.80).aspx

     

    • 已标记为答案 493595708 2009年10月6日 3:57
    2009年10月5日 7:38
  • 您好,使用方法见楼上,然后用正则表达式来分析数据。
    2009年10月5日 8:37
  • 用以下代码:

    Imports System
    Imports System.IO
    Imports System.Net
    Imports System.Text
    Namespace Examples.System.Net
        Public Class WebRequestPostExample
            Public Shared Sub Main()
                ' Create a request using a URL that can receive a post. 
                Dim request As WebRequest = WebRequest.Create("http://ptlogin2.qq.com/login")
                ' Set the Method property of the request to POST.
                request.Method = "POST"
                ' Create POST data and convert it to a byte array.
                Dim u, p, y As String
                u = "XXXXX" 'QQ号码
                p = "XXXXX" '加密后的QQ密码
                y = "XXX" '验证码
                Dim postData As String = "u=" & u & "&p=" & p & "&verifycode=" & y & "&aid=15000101&u1=http%3A%2F%2Fimgcache.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3Fpara%3Dizone&fp=loginerroralert&h=1&ptredirect=1&ptlang=0&from_ui=1&dumy="
                Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
                ' Set the ContentType property of the WebRequest.
                request.ContentType = "application/x-www-form-urlencoded"
                ' Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length
                ' Get the request stream.
                Dim dataStream As Stream = request.GetRequestStream()
                ' Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length)
                ' Close the Stream object.
                dataStream.Close()
                ' Get the response.
                Dim response As WebResponse = request.GetResponse()
                ' Display the status.
                Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
                ' Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream()
                ' Open the stream using a StreamReader for easy access.
                Dim reader As New StreamReader(dataStream)
                ' Read the content.
                Dim responseFromServer As String = reader.ReadToEnd()
                ' Display the content.
                Console.WriteLine(responseFromServer)
                Console.Read()
                ' Clean up the streams.
                reader.Close()
                dataStream.Close()
                response.Close()
            End Sub
        End Class
    End Namespace
    返回:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>腾讯登录页面</title>
    <style type="text/css">
    <!--
    body,td,th {
        font-family: "宋体",Verdana, Arial;
    	font-size: 12px;text-align: left;}
    .btn{
    border:1px solid #eeeeee; height:20px; width:50px; font-family: "宋体",Verdana, Arial ;font-size: 12px; text-align: center;
    FILTER: progid:DXImageTransform.Microsoft.Gradient(startColorStr=#ffffff,endColorStr=#C6C5D7)}
    -->
    </style>
    </head>
    <body>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td align="center"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" style="border:1px solid #0D7CCB">
    <tr>
    <td height="23" align="left" valign="middle" bgcolor="#0D7CCB"><span style="font-size:14px; font-weight:bold; color:#ffffff; padding:10px">提示信息</span></td>
    </tr>
    <tr>
    <td align="center" valign="middle"><label></label>
    <table style="padding:10px" width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td width="100%" align="left" valign="top"><span style="font-size:13px; line-height:17px;">
    系统繁忙,请稍后重试(50)。</span></td>
    </tr>
    </table></td>
    </tr>
    <tr>
    <td height="30" valign="bottom"><div align="center"><a style="CURSOR:hand"><input style="CURSOR:hand"  name="Submit" type="button"  class="btn" onclick=javascript:window.history.back() value="确 定" /></a></div></td>
    </tr>
    <tr>
    <td height="15" align="center">&nbsp;</td>
    </tr>
    </table></td>
    </tr>
    </table>
    </body>
    </html>
    用其他编程语言POST一样的代码,都不是这个结果,这是为什么呢?
    我是包子!
    2009年10月5日 9:39