这是我的代码,使用HttpWebRequest自动填充Web表单并提交。
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()
在此代码中,我使用了yellowpages的邮件表格作为示例。是的它确实让我填写并提交,但我想要另一种选择。除了WebClient还有其他替代品,因为我已经尝试过它可以发送POST请求吗?我已经阅读了有关System.Net.Sockets的主题,它能够发送POST请求,但我不知道从哪里开始。任何提示都会很乐意接受。我真的发现HttpWebRequest和WebClient在发送POST请求时速度很慢。
答案 0 :(得分:2)
您好,您可以尝试使用这种方式
Using sendto As New Net.WebClient
Dim param As New Specialized.NameValueCollection
param.Add("param1", "value1")
param.Add("param2", "value2")
Dim response_bytes = sendto.UploadValues(yourUrl, "POST", param)
Dim response_body = (New Text.UTF8Encoding).GetString(response_bytes)
End Using