我有一个URL
,它返回数据数组。
例如,如果我有网址http://test.com?id=1
,则会返回3,5,6,7
等值...
有什么方法可以处理这个URL并获取返回的值而无需去浏览器(处理应用程序中的url)?
由于
答案 0 :(得分:24)
非常简单:
using System.Net;
...
var response = new WebClient().DownloadString("http://test.com?id=1");
答案 1 :(得分:2)
string urlAddress = "YOUR URL";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
readStream = new StreamReader(receiveStream);
else
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
string data = readStream.ReadToEnd();
response.Close();
readStream.Close();
}
这应该可以解决问题。
它返回主页的html源代码并将其填入string data
。
您现在可以使用该字符串。
来源:http://www.codeproject.com/Questions/204778/Get-HTML-code-from-a-website-C
答案 2 :(得分:2)
这是一个我经常用于类似目的的简单函数(VB.NET):
Public Shared Function GetWebData(url As String) As String
Try
Dim request As WebRequest = WebRequest.Create(url)
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
Dim dataStream As Stream = response.GetResponseStream()
Dim readStream As StreamReader = New StreamReader(dataStream)
Dim data = readStream.ReadToEnd()
readStream.Close()
dataStream.Close()
response.Close()
Return data
Catch ex As Exception
Return ""
End Try
End Function
要使用它,请将URL传递给它,它将返回URL的内容。