使用HttpWebRequest和Get方法的PHP连接

时间:2010-04-26 06:08:16

标签: vb.net web-scraping

我有一个脚本返回一个字符串:

http://example.com/script.php

PHP脚本:

$data = $_GET['q'];

$query = "SELECT * FROM `table` WHERE ID = '$data'";
$result = mysql_query($query);
$num = mysql_num_rows($result);

print $num;

我想使用此代码

将此脚本与VB连接
Dim con As String
con = "http://example.com/script.php?q=" & My.Settings.setq

Dim request = HttpWebRequest.Create(con)
request.Method = "GET"
Dim response = request.GetResponse()
Using reader = New StreamReader(response.GetResponseStream())
   msgbox(reader.ReadToEnd())
End Using

它不起作用。我怎么能这样做?

编辑:我找到了解决方案

这里

Dim uri As New Uri("http://example.com")
        Dim data As String = "data"
        If (uri.Scheme = uri.UriSchemeHttp) Then
  Dim request As HttpWebRequest = HttpWebRequest.Create(uri)
            request.Method = WebRequestMethods.Http.Post
            request.ContentLength = data.Length
            request.ContentType = "application/x-www-form-urlencoded"

            Dim writer As New StreamWriter(request.GetRequestStream())
            writer.Write(data)
            writer.Close()

            Dim response As HttpWebResponse = request.GetResponse()
            Dim reader As New StreamReader(response.GetResponseStream())
            Dim tmp As String = reader.ReadToEnd()
            response.Close()

1 个答案:

答案 0 :(得分:0)

Using wc As New System.Net.WebClient()
    MsgBox(wc.DownloadString(String.Format("http://example.com/script.php?q={0}", My.Settings.setq)))
End Using