如何向网站发送POST并在VB中收到回复? [英] How do I send a POST to a website and receive a response in VB?

查看:181
本文介绍了如何向网站发送POST并在VB中收到回复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个机器人,可以选择通过这个网站过滤消息,http://www.gizoogle.net/textilizer.php。

我想通过以下方式发送字符串发布并让它发回翻译。通过我的浏览器控制台查看网站,我看到文本是translatetext,它所在的框的名称叫做textarea。我以前从未真正做过这件事,完全不知道如何去做或从哪里开始。任何建议都会非常有用!这将在Visual Basic中编码,但我也知道C#。

解决方案

试试这个:

 Dim text As string =以下过程描述了将数据发送到服务器的步骤。此过程通常用于将数据发布到网页。 

Dim results As String =
Dim myrequest As HttpWebRequest = HttpWebRequest.Create(http://www.gizoogle.net/textilizer.php)
myrequest。 Method =POST
myrequest.Timeout = 1000
myrequest.ContentType =application / x-www-form-urlencoded

Dim bytedata as byte()= Encoding。 UTF8.GetBytes(translatetext =& text&translate = Tranzizzle + Dis + Shiznit)
myrequest.ContentLength = bytedata.Length

Dim requestStream As Stream = myrequest.GetRequestStream ()
requestStream.Write(bytedata,0,bytedata.Length)
requestStream.Close()

尝试
Dim resp As System.Net.HttpWebResponse = myrequest .GetResponse()

Dim sr As New System.IO.StreamReader(resp.GetResponseStream())
result = sr.ReadToEnd()
sr.Close()

Dim re As RegEx = new RegEx(< textarea。*?name =translatetext。*?>(。*?)< / textarea>)
Dim m As Match = re.Match(result)

如果m.Success
result = m.groups(1).Value
结束如果

Console.WriteLine(result)
Catch ex As WebException
如果ex.Status = WebExceptionStatus.Timeout然后
result =错误:请求已超时
否则
result =错误:+ ex.Message
结束如果
结束尝试


I'm writing a bot that will have the option to filter messages through this website, http://www.gizoogle.net/textilizer.php .
I want to send the string via post and have it send back the translation. Looking on the site through my browser console, I see the text is translatetext and the name of the box that it's in is called textarea. I've never actually done this before and have absolutely no idea how to go about it or where to start. Any advice would be immensely helpful! This will be coded in Visual Basic but I also know C#.

解决方案

Try this:

Dim text As string = "The following procedure describes the steps used to send data to a server. This procedure is commonly used to post data to a Web page."
 
Dim result As String = ""
Dim myrequest As HttpWebRequest = HttpWebRequest.Create("http://www.gizoogle.net/textilizer.php")
myrequest.Method = "POST"
myrequest.Timeout = 1000
myrequest.ContentType = "application/x-www-form-urlencoded"
 
Dim bytedata As byte() =  Encoding.UTF8.GetBytes("translatetext="& text &"translate=Tranzizzle+Dis+Shiznit")
myrequest.ContentLength = bytedata.Length
 
Dim requestStream As Stream = myrequest.GetRequestStream()
requestStream.Write(bytedata, 0, bytedata.Length)
requestStream.Close()
 
Try
   Dim resp As System.Net.HttpWebResponse = myrequest.GetResponse()
 
   Dim sr As New System.IO.StreamReader(resp.GetResponseStream())
   result = sr.ReadToEnd()
   sr.Close()
 
   Dim re As RegEx = new RegEx("<textarea.*?name=""translatetext"".*?>(.*?)</textarea>")
   Dim m As Match = re.Match(result)
   
   If m.Success
   		result = m.groups(1).Value
   End If
 
   Console.WriteLine(result)
Catch ex As WebException
   If ex.Status = WebExceptionStatus.Timeout Then
       result = "Error: The request has timed out"
   Else
       result = "Error: " + ex.Message
   End If
End Try


这篇关于如何向网站发送POST并在VB中收到回复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆