pengembangan-web-mp-pd.com

HTTP DAPATKAN di VB.NET

Apa cara terbaik untuk mengeluarkan http di VB.net? Saya ingin mendapatkan hasil dari permintaan seperti http://api.hostip.info/?ip=68.180.206.184

35
notandy

Di VB.NET:

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

Dalam C #:

System.Net.WebClient webClient = new System.Net.WebClient();
string result = webClient.DownloadString("http://api.hostip.info/?ip=68.180.206.184");
62
hangy

Anda dapat menggunakan kelas HttpWebRequest untuk melakukan permintaan dan mengambil respons dari URL yang diberikan. Anda akan menggunakannya seperti:

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 dirinci di: http://msdn.Microsoft.com/en-us/library/system.net.httpwebrequest.aspx

Opsi kedua adalah menggunakan kelas WebClient, ini menyediakan antarmuka yang lebih mudah digunakan untuk mengunduh sumber daya web tetapi tidak sefleksibel 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

Info lebih lanjut tentang webclient dapat ditemukan di: http://msdn.Microsoft.com/en-us/library/system.net.webclient.aspx

22
Wolfwyrd

Gunakan WebRequest class

Ini untuk mendapatkan gambar:

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
5
chrissie1

Cara termudah adalah System.Net.WebClient.DownloadFile atau DownloadString.

2
Oliver Mellet

Anda harus mencoba kelas HttpWebRequest .

1
Dario Solera

Coba ini:

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
}

Maaf tentang C #, saya tahu Anda meminta VB, tetapi saya tidak punya waktu untuk mengonversi.

1
Nick Berardi
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
0
sanket parikh