vb.net - VB.net使用套接字发送HTTP POST请求

标签 vb.net sockets post webrequest

我这里有一个使用SOCKETS的有效HTTP请求,但是我不知道如何发送POST请求。

这是我的代码:

            Dim hostName As String
            Dim hostPort As Integer
            Dim response As Integer
            Dim iphe As IPHostEntry = Dns.GetHostEntry("www.yellowpages.com")
            hostName = iphe.AddressList(0).ToString()
            hostPort = 80
            response = 0

            Dim host As IPAddress = IPAddress.Parse(hostName)
            Dim hostep As New IPEndPoint(host, hostPort)
            Dim sock As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            sock.Connect(hostep)

            Dim request = "GET /nationwide/mip/choice-hotels-international-462092189/send_email?lid=161004592 HTTP/1.1" & vbCr & vbLf &
                          "Host: 208.93.105.105" & vbCr & vbLf &
                          "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36" & vbCr & vbLf &
                          "Connection: keep-alive" & vbCr & vbLf &
                          "Content-Type: application/x-www-form-urlencoded" & vbCr & vbLf &
                          "Content-Length: 0" & vbCr & vbLf & vbCr & vbLf

            response = sock.Send(Encoding.UTF8.GetBytes(request))
            response = sock.Send(Encoding.UTF8.GetBytes(vbCr & vbLf))

            sock.Close()

我想用这些数据填写我在代码中提到的Web表单:
email%5Bto_address%5D=test@mail.com&email%5Bfrom_name%5D=Test Name&email%5Bfrom_address%5D=test@mail.com&email%5Bnote%5D=Hello

我该怎么做?我已经使用以下代码使用HttpWebRequest成功完成了此操作:
        Dim cweb As String = "http://www.yellowpages.com/novato-ca/mip/creative-memories-consultant-senior-director-461725587/send_email?lid=171673036"
        Dim POST As String = "&email%5Bto_address%5D=recipient@email.com&email%5Bfrom_name%5D=Test Name&email%5Bfrom_address%5D=sender@mail.com&email%5Bnote%5D=Hello There"       

        Dim request As HttpWebRequest
        Dim response As HttpWebResponse

        request = CType(WebRequest.Create(cweb), HttpWebRequest)
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36"
        request.AllowAutoRedirect = True
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = POST.Length
        request.Method = "POST"
        request.KeepAlive = True

        Dim requestStream As Stream = request.GetRequestStream()
        Dim postBytes As Byte() = Encoding.ASCII.GetBytes(POST)
        requestStream.Write(postBytes, 0, postBytes.Length)
        requestStream.Close()

        response = CType(request.GetResponse(), HttpWebResponse)
        response.Close()

但是我想通过使用SOCKETS重新创建这个概念。

最佳答案

如果您可以使用套接字发送GET请求,那么您当然可以发送POST请求。

但是,您必须正确格式化它。您可以检查RFC以获得整个HTTP规范。

POST样本:

Dim request = "POST /nationwide/mip/choice-hotels-international-462092189/send_email?lid=161004592 HTTP/1.1" & Environment.Newline &
                          "Host: 208.93.105.105" & Environment.Newline &
                          "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36" & Environment.Newline &
                          "Connection: keep-alive" & Environment.Newline &
                          "Content-Type: application/x-www-form-urlencoded" & Environment.Newline &
                          "Content-Length: 22" & Environment.Newline & Environment.Newline &
              "this%20is%20the%20data"

关于vb.net - VB.net使用套接字发送HTTP POST请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18909173/

相关文章:

c# - C# 和 VB.Net 之间对象装箱/比较引用的差异

wcf - Widdle服务的POST方法在Fiddler中失败

java - Android 扫描本地子网

web-services - 在一个 POST 命令中卷取多个数据

ruby - 用 Ruby 模拟网页导航

vb.net - vb.net "expression expected"是什么意思

.net - 如何在 Visual Studio 中为我的自定义控件设置要编辑的默认事件?

vb.net - <Flags> 和 <FlagsAttribute()> 之间的区别

Java TCP 客户端在 Eclipse 之外工作时无响应

php - 是否可以在 PHP 中使用 TCP/IP 套接字访问本地打印机?