我在visual c#2005中创建一个简单的程序,在Yahoo!上查找股票代码财务,下载历史数据,然后绘制指定股票代码的价格历史记录。
我知道获取数据所需的确切网址,如果用户输入现有的自动收报机代码(或者至少有一个有关Yahoo! Finance的数据),那就完全正常了。但是,如果用户编写了一个股票代码,我会遇到运行时错误,因为该程序试图从不存在的网页中提取数据。
我正在使用WebClient类,并使用DownloadString函数。我查看了WebClient类的所有其他成员函数,但没有看到任何可用于测试URL的内容。
我该怎么做?
答案 0 :(得分:127)
以下是此解决方案的另一个实现:
using System.Net;
///
/// Checks the file exists or not.
///
/// The URL of the remote file.
/// True : If the file exits, False if file not exists
private bool RemoteFileExists(string url)
{
try
{
//Creating the HttpWebRequest
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
//Setting the Request method HEAD, you can also use GET too.
request.Method = "HEAD";
//Getting the Web Response.
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//Returns TRUE if the Status code == 200
response.Close();
return (response.StatusCode == HttpStatusCode.OK);
}
catch
{
//Any exception will returns false.
return false;
}
}
来自:http://www.dotnetthoughts.net/2009/10/14/how-to-check-remote-file-exists-using-c/
答案 1 :(得分:102)
您可以发出"HEAD"请求而不是“GET”吗?
(编辑) - 哈哈!看起来我done this before!改为维基以避免指责代表。因此,要测试URL而不需要下载内容的费用:
// using MyClient from linked post
using(var client = new MyClient()) {
client.HeadOnly = true;
// fine, no content downloaded
string s1 = client.DownloadString("http://google.com");
// throws 404
string s2 = client.DownloadString("http://google.com/silly");
}
您应try
/ catch
围绕DownloadString
检查错误;没有错误?它存在......
使用C#2.0(VS2005):
private bool headOnly;
public bool HeadOnly {
get {return headOnly;}
set {headOnly = value;}
}
和
using(WebClient client = new MyClient())
{
// code as before
}
答案 2 :(得分:33)
这些解决方案非常好,但他们忘记了可能还有其他状态代码而不是200 OK。这是我在生产环境中用于状态监控等的解决方案。
如果目标页面上存在URL重定向或某些其他条件,则使用此方法返回true。此外,GetResponse()将抛出异常,因此您将无法获得StatusCode。您需要捕获异常并检查ProtocolError。
任何400或500状态代码都将返回false。所有其他人都归于真实 此代码很容易修改,以满足您对特定状态代码的需求。
/// <summary>
/// This method will check a url to see that it does not return server or protocol errors
/// </summary>
/// <param name="url">The path to check</param>
/// <returns></returns>
public bool UrlIsValid(string url)
{
try
{
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Timeout = 5000; //set the timeout to 5 seconds to keep the user from waiting too long for the page to load
request.Method = "HEAD"; //Get only the header information -- no need to download any content
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
int statusCode = (int)response.StatusCode;
if (statusCode >= 100 && statusCode < 400) //Good requests
{
return true;
}
else if (statusCode >= 500 && statusCode <= 510) //Server Errors
{
//log.Warn(String.Format("The remote server has thrown an internal error. Url is not valid: {0}", url));
Debug.WriteLine(String.Format("The remote server has thrown an internal error. Url is not valid: {0}", url));
return false;
}
}
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError) //400 errors
{
return false;
}
else
{
log.Warn(String.Format("Unhandled status [{0}] returned for url: {1}", ex.Status, url), ex);
}
}
catch (Exception ex)
{
log.Error(String.Format("Could not test url {0}.", url), ex);
}
return false;
}
答案 3 :(得分:8)
如果我正确理解您的问题,您可以使用这样的小方法来为您提供网址测试的结果:
WebRequest webRequest = WebRequest.Create(url);
WebResponse webResponse;
try
{
webResponse = webRequest.GetResponse();
}
catch //If exception thrown then couldn't get response from address
{
return 0;
}
return 1;
您可以将上述代码包装在方法中并使用它来执行验证。我希望这能回答你提出的问题。
答案 4 :(得分:4)
试试这个(确保使用System.Net):
public bool checkWebsite(string URL) {
try {
WebClient wc = new WebClient();
string HTMLSource = wc.DownloadString(URL);
return true;
}
catch (Exception) {
return false;
}
}
当调用checkWebsite()函数时,它会尝试获取源代码 传递给它的URL。如果它获得源代码,则返回true。如果不, 它返回false。
代码示例:
//The checkWebsite command will return true:
bool websiteExists = this.checkWebsite("https://www.google.com");
//The checkWebsite command will return false:
bool websiteExists = this.checkWebsite("https://www.thisisnotarealwebsite.com/fakepage.html");
答案 5 :(得分:2)
这个解决方案似乎很容易理解:
public static bool isValidURL(string url) {
WebRequest webRequest = WebRequest.Create(url);
WebResponse webResponse;
try
{
webResponse = webRequest.GetResponse();
}
catch //If exception thrown then couldn't get response from address
{
return false ;
}
return true ;
}
答案 6 :(得分:2)
这是另一个选项
public static bool UrlIsValid(string url)
{
bool br = false;
try {
IPHostEntry ipHost = Dns.Resolve(url);
br = true;
}
catch (SocketException se) {
br = false;
}
return br;
}
答案 7 :(得分:1)
我有一种更简单的方法来确定网址是否有效。
if (Uri.IsWellFormedUriString(uriString, UriKind.RelativeOrAbsolute))
{
//...
}
答案 8 :(得分:0)
Web服务器使用HTTP状态代码进行响应,指示请求的结果,例如200(有时202)表示成功,404(未找到等)(见here)。假设URL的服务器地址部分是正确的并且您没有获得套接字超时,则异常很可能告诉您HTTP状态代码不是200.我建议检查异常的类并查看异常是否携带HTTP状态代码。
IIRC - 有问题的调用会抛出WebException或后代。检查类名以查看哪个类,并在try块中包含调用以捕获条件。
答案 9 :(得分:0)
继已经给出的示例之后,我会说,最佳做法是将响应包装在像这样的使用中
public bool IsValidUrl(string url)
{
try
{
var request = WebRequest.Create(url);
request.Timeout = 5000;
request.Method = "HEAD";
using (var response = (HttpWebResponse)request.GetResponse())
{
response.Close();
return response.StatusCode == HttpStatusCode.OK;
}
}
catch (Exception exception)
{
return false;
}
}
答案 10 :(得分:0)
我总是发现异常处理起来要慢得多。
也许一种不那么密集的方式会产生更好,更快的结果?
public bool IsValidUri(Uri uri)
{
using (HttpClient Client = new HttpClient())
{
HttpResponseMessage result = Client.GetAsync(uri).Result;
HttpStatusCode StatusCode = result.StatusCode;
switch (StatusCode)
{
case HttpStatusCode.Accepted:
return true;
case HttpStatusCode.OK:
return true;
default:
return false;
}
}
}
然后使用:
IsValidUri(new Uri("http://www.google.com/censorship_algorithm"));
答案 11 :(得分:0)
WebRequest request = WebRequest.Create("http://www.google.com");
try
{
request.GetResponse();
}
catch //If exception thrown then couldn't get response from address
{
MessageBox.Show("The URL is incorrect");`
}