C#中的图像URL验证

时间:2010-10-12 13:09:17

标签: c# validation browser hyperlink

如何检查我的image链接是否对IE和FF都有效?例如,此链接仅在FF中工作,IE浏览器中不显示任何图像。我检查了图像,颜色空间是RGB。因此排除了图像空间问题。

感谢。

2 个答案:

答案 0 :(得分:2)

获取fiddler的副本,以查看每个浏览器的响应差异。您可能会发现标头错误,FF正在纠正,但IE不是。

http://www.fiddler2.com/fiddler2/

希望这有帮助

答案 1 :(得分:2)

这是一个允许您验证任何类型URI并支持URI集合的多线程验证的类

using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Threading;

namespace UrlValidation
{
    public class UrlValidator
    {
        internal static readonly Hashtable URLVerifications = new Hashtable();
        internal readonly List<ManualResetEvent> Handles = new List<ManualResetEvent>();

        internal void ValidateUrls()
        {
            var urlsToValidate = new[] { "http://www.ok12376876.com", "http//:www.ok.com", "http://www.ok.com", "http://cnn.com" };
            URLVerifications.Clear();
            foreach (var url in urlsToValidate)
                CheckUrl(url);
            if (Handles.Count > 0)
                WaitHandle.WaitAll(Handles.ToArray());

            foreach (DictionaryEntry verification in URLVerifications)
                Console.WriteLine(verification.Value);
        }

        internal class RequestState
        {
            public WebRequest Request;
            public WebResponse Response;
            public ManualResetEvent Handle;
        }

        private void CheckUrl(string url)
        {
            var hashCode = url.GetHashCode();
            var evt = new ManualResetEvent(false);
            Handles.Add(evt);

            if (!Uri.IsWellFormedUriString(url, UriKind.Absolute))
            {
                URLVerifications[hashCode] = "Invalid URL.";
                evt.Set();
                return;
            }

            if (!URLVerifications.ContainsKey(hashCode))
                URLVerifications.Add(hashCode, null);
            // Create a new webrequest to the mentioned URL.   
            var wreq = WebRequest.Create(url);
            wreq.Timeout = 5000; // 5 seconds timeout per thread (ignored for async calls)
            var state = new RequestState{ Request = wreq, Handle = evt };
            // Start the Asynchronous call for response.
            var asyncResult = wreq.BeginGetResponse(RespCallback, state);
            ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, TimeoutCallback, state, 5000, true);
        }

        private static void TimeoutCallback(object state, bool timedOut)
        {
            var reqState = (RequestState)state;
            if (timedOut)
            {
                var hashCode = reqState.Request.RequestUri.OriginalString.GetHashCode();
                URLVerifications[hashCode] = "Request timed out.";
                if (reqState.Request != null)
                    reqState.Request.Abort();
            }
        }

        private static void RespCallback(IAsyncResult asynchronousResult)
        {
            ManualResetEvent evt = null;
            int hashCode = 0;
            try
            {
                var reqState = (RequestState)asynchronousResult.AsyncState;
                hashCode = reqState.Request.RequestUri.OriginalString.GetHashCode();
                evt = reqState.Handle;
                reqState.Response = reqState.Request.EndGetResponse(asynchronousResult);
                var resp = ((HttpWebResponse)reqState.Response).StatusCode;
                URLVerifications[hashCode] = resp.ToString();
            }
            catch (WebException e)
            {
                if (hashCode != 0 && string.IsNullOrEmpty((string)URLVerifications[hashCode]))
                    URLVerifications[hashCode] = e.Response == null ? e.Status.ToString() : (int)((HttpWebResponse)e.Response).StatusCode + ": " + ((HttpWebResponse)e.Response).StatusCode;
            }
            finally
            {
                if (evt != null)
                    evt.Set();
            }
        }
    }
}

希望有所帮助