代码之家  ›  专栏  ›  技术社区  ›  notandy

VB.NET中的HTTP GET

  •  35
  • notandy  · 技术社区  · 16 年前

    在vb.net中发布http get的最佳方法是什么?我想得到一个请求的结果 http://api.hostip.info/?ip=68.180.206.184

    7 回复  |  直到 16 年前
        1
  •  62
  •   hangy    12 年前

    VB.NET:

    Dim webClient As New System.Net.WebClient
    Dim result As String = webClient.DownloadString("http://api.hostip.info/?ip=68.180.206.184")
    

    C中:

    System.Net.WebClient webClient = new System.Net.WebClient();
    string result = webClient.DownloadString("http://api.hostip.info/?ip=68.180.206.184");
    
        2
  •  22
  •   Wolfwyrd    16 年前

    您可以使用httpwebrequest类执行请求并从给定的URL检索响应。你会像这样使用它:

    Try
        Dim fr As System.Net.HttpWebRequest
        Dim targetURI As New Uri("http://whatever.you.want.to.get/file.html")         
    
        fr = DirectCast(HttpWebRequest.Create(targetURI), System.Net.HttpWebRequest)
        If (fr.GetResponse().ContentLength > 0) Then
            Dim str As New System.IO.StreamReader(fr.GetResponse().GetResponseStream())
            Response.Write(str.ReadToEnd())
            str.Close(); 
        End If   
    Catch ex As System.Net.WebException
       'Error in accessing the resource, handle it
    End Try
    

    httpwebrequest的详细信息如下: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

    第二个选项是使用WebClient类,这提供了一个更易于使用的界面来下载Web资源,但不如httpwebrequest灵活:

    Sub Main()
        'Address of URL
        Dim URL As String = http://whatever.com
        ' Get HTML data
        Dim client As WebClient = New WebClient()
        Dim data As Stream = client.OpenRead(URL)
        Dim reader As StreamReader = New StreamReader(data)
        Dim str As String = ""
        str = reader.ReadLine()
        Do While str.Length > 0
            Console.WriteLine(str)
            str = reader.ReadLine()
        Loop
    End Sub
    

    有关WebClient的详细信息,请访问: http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx

        3
  •  5
  •   Jimi    6 年前

    使用 WebRequest

    这是为了获得图像:

    Try
        Dim _WebRequest As System.Net.WebRequest = Nothing
        _WebRequest = System.Net.WebRequest.Create(http://api.hostip.info/?ip=68.180.206.184)
    Catch ex As Exception
        Windows.Forms.MessageBox.Show(ex.Message)
        Exit Sub
    End Try
    
    Try
        _NormalImage = Image.FromStream(_WebRequest.GetResponse().GetResponseStream())
    Catch ex As Exception
        Windows.Forms.MessageBox.Show(ex.Message)
        Exit Sub
    End Try
    
        4
  •  2
  •   Siddharth Rout    12 年前

    最简单的方法是 System.Net.WebClient.DownloadFile DownloadString .

        5
  •  1
  •   Dario Solera    16 年前

    你应该试试 HttpWebRequest 班级。

        6
  •  1
  •   Nick Berardi    16 年前

    试试这个:

    WebRequest request = WebRequest.CreateDefault(RequestUrl);
    request.Method = "GET";
    
    WebResponse response;
    try { response = request.GetResponse(); }
    catch (WebException exc) { response = exc.Response; }
    
    if (response == null)
        throw new HttpException((int)HttpStatusCode.NotFound, "The requested url could not be found.");
    
    using(StreamReader reader = new StreamReader(response.GetResponseStream())) {
        string requestedText = reader.ReadToEnd();
    
        // do what you want with requestedText
    }
    

    抱歉,我知道你要的是vb,但我没有时间转换。

        7
  •  0
  •   sanket parikh    7 年前
    Public Function getLoginresponce(ByVal email As String, ByVal password As String) As String
        Dim requestUrl As String = "your api"
        Dim request As HttpWebRequest = TryCast(WebRequest.Create(requestUrl), HttpWebRequest)
        Dim response As HttpWebResponse = TryCast(request.GetResponse(), HttpWebResponse)
        Dim dataStream As Stream = response.GetResponseStream()
        Dim reader As New StreamReader(dataStream)
        Dim responseFromServer As String = reader.ReadToEnd()
        Dim result = responseFromServer
        reader.Close()
        response.Close()
        Return result
    End Function