C# C语言中的图像URL验证#
如何检查我的链接是否对IE和FF都有效?例如,此链接仅在FF中工作,IE浏览器中不显示任何图像。我检查了图像,颜色空间是RGB。这样就排除了图像空间问题C# C语言中的图像URL验证#,c#,validation,browser,hyperlink,C#,Validation,Browser,Hyperlink,如何检查我的链接是否对IE和FF都有效?例如,此链接仅在FF中工作,IE浏览器中不显示任何图像。我检查了图像,颜色空间是RGB。这样就排除了图像空间问题 谢谢。获取fiddler的副本,查看每个浏览器响应的差异。您可能会发现标题错误,FF正在更正,但IE没有 希望这有助于获得fiddler的副本,以查看每个浏览器响应的差异。您可能会发现标题错误,FF正在更正,但IE没有 希望这有帮助这里有一个类,可以让您验证任何类型的URI,并支持URI集合的多线程验证 using System; usin
谢谢。获取fiddler的副本,查看每个浏览器响应的差异。您可能会发现标题错误,FF正在更正,但IE没有
希望这有助于获得fiddler的副本,以查看每个浏览器响应的差异。您可能会发现标题错误,FF正在更正,但IE没有
希望这有帮助这里有一个类,可以让您验证任何类型的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();
}
}
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
Net系统;
使用系统线程;
命名空间URL验证
{
公共类UrlValidator
{
内部静态只读哈希表URLVerifications=新哈希表();
内部只读列表句柄=新列表();
内部无效验证EURLS()
{
var urlsToValidate=new[]{”http://www.ok12376876.com“,”http://www.ok.com“http://www.ok.com", "http://cnn.com" };
urlvifications.Clear();
foreach(urlsToValidate中的变量url)
检查url(url);
如果(Handles.Count>0)
WaitHandle.WaitAll(Handles.ToArray());
foreach(URLVerifications中的DictionaryEntry验证)
Console.WriteLine(验证值);
}
内部类RequestState
{
公共网络请求;
公众网络响应;
公共事件句柄;
}
私有无效检查url(字符串url)
{
var hashCode=url.GetHashCode();
var evt=新手动重置事件(错误);
句柄。添加(evt);
如果(!Uri.IsWellFormedUriString(url,UriKind.Absolute))
{
URL验证[hashCode]=“无效的URL。”;
evt.Set();
回来
}
if(!urlvifications.ContainsKey(hashCode))
urlvifications.Add(hashCode,null);
//创建指向所述URL的新webrequest。
var wreq=WebRequest.Create(url);
wreq.Timeout=5000;//每个线程超时5秒(异步调用忽略)
var state=newrequeststate{Request=wreq,Handle=evt};
//启动异步调用以获得响应。
var asynchresult=wreq.BeginGetResponse(RespCallback,state);
RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle,TimeoutCallback,state,5000,true);
}
私有静态void TimeoutCallback(对象状态,bool timedOut)
{
var reqState=(RequestState)状态;
if(timedOut)
{
var hashCode=reqState.Request.RequestUri.OriginalString.GetHashCode();
URLVerifications[hashCode]=“请求超时。”;
if(reqState.Request!=null)
Request.Abort();
}
}
私有静态void RespCallback(IAsyncResult asynchronousResult)
{
ManualResetEvent evt=null;
int hashCode=0;
尝试
{
var reqState=(RequestState)asynchronousResult.asynchstate;
hashCode=reqState.Request.RequestUri.OriginalString.GetHashCode();
evt=请求状态句柄;
reqState.Response=reqState.Request.EndGetResponse(异步结果);
var resp=((HttpWebResponse)reqState.Response).StatusCode;
URLVerifications[hashCode]=resp.ToString();
}
捕获(WebE例外)
{
if(hashCode!=0&&string.IsNullOrEmpty((string)urlvifications[hashCode]))
URLVerifications[hashCode]=e.Response==null?e.Status.ToString():(int)((HttpWebResponse)e.Response).StatusCode+“:”+((HttpWebResponse)e.Response).StatusCode;
}
最后
{
如果(evt!=null)
evt.Set();
}
}
}
}
希望对您有所帮助,这里有一个类,可以让您验证任何类型的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();
}
}
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
Net系统;
使用系统线程;
命名空间URL验证
{
公共类UrlValidator
{
内部静态只读哈希表URLVerifications=新哈希表();
内部只读列表句柄=新列表();
内部无效验证EURLS()
{
var urlsToValidate=new[]{”http://www.ok12376876.com“,”http://www.ok.com“http://www.ok.com", "http://cnn.com" };
urlvifications.Clear();
foreach(urlsToValidate中的变量url)
检查url(url);
如果(Handles.Count>0)
WaitHandle.WaitAll(Handles.ToArray());
foreach(URLVerifications中的DictionaryEntry验证)
Console.WriteLine(验证值);
}
内部类RequestState
{
公共网络请求;
公众网络响应;
公共事件句柄;
}
私有无效检查url(字符串url)
{
var hashCode=url.GetHashCode();
var evt=新手动重置事件(错误);
句柄。添加(evt);
如果(!Uri.IsWellFormedUriString(url,UriKind.Absolute))
{
URL验证[hashCode]=“无效的URL。”;
evt.Set();
回来
}
如果(!urlvifications.ContainsKey(hashC