VB.NET 中的 HTTP GET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/92522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTP GET in VB.NET
提问by notandy
What is the best way to issue a http get in VB.net? I want to get the result of a request like http://api.hostip.info/?ip=68.180.206.184
在 VB.net 中发出 http get 的最佳方法是什么?我想得到像http://api.hostip.info/?ip=68.180.206.184这样的请求的结果
回答by hangy
In VB.NET:
在 VB.NET 中:
Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString("http://api.hostip.info/?ip=68.180.206.184")
In C#:
在 C# 中:
System.Net.WebClient webClient = new System.Net.WebClient();
string result = webClient.DownloadString("http://api.hostip.info/?ip=68.180.206.184");
回答by Wolfwyrd
You can use the HttpWebRequest class to perform a request and retrieve a response from a given URL. You'll use it like:
您可以使用 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 is detailed at: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx
HttpWebRequest 的详细信息位于:http: //msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx
A second option is to use the WebClient class, this provides an easier to use interface for downloading web resources but is not as flexible as HttpWebRequest:
第二种选择是使用 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
More info on the webclient can be found at: http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx
有关 webclient 的更多信息,请访问:http: //msdn.microsoft.com/en-us/library/system.net.webclient.aspx
回答by chrissie1
Use the WebRequestclass
使用WebRequest类
This is to get an image:
这是获取图像:
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
回答by Oliver Mellet
The easiest way is System.Net.WebClient.DownloadFile
or DownloadString
.
最简单的方法是System.Net.WebClient.DownloadFile
或DownloadString
。
回答by Nick Berardi
Try this:
尝试这个:
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
}
Sorry about the C#, I know you asked for VB, but I didn't have time to convert.
抱歉 C#,我知道您要求使用 VB,但我没有时间进行转换。
回答by Dario Solera
You should try the HttpWebRequestclass.
您应该尝试HttpWebRequest类。
回答by sanket parikh
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