网址验证 [英] URL VALIDATION

查看:81
本文介绍了网址验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试验证网址.
我为此使用HttpWebRequest和HttpWebResponse,但在每种情况下我都遇到异常.

以下是我的验证代码

Hello everyone,

I am trying to validate url.
I''m using HttpWebRequest and HttpWebResponse for this but in each case i''m getting an exception.

Following is my code for validation

Uri _url = new Uri(TextUrl);
            MessageBox.Show(_url.ToString()); //jus to check url content
            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(_url);
                try
                {
                 HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
}


catch
                {
                    MessageBox.Show("this url is not valid");
                    TextUrl = "";
                    url.Focus();
}



当我以正确的语法(例如"http://www.gmail.com")输入网址时,
我正在自定义例外.

但是如果我输入的网址是"www.gmail.com"
其引发错误:sigh:
如果有人有任何想法请尽快答复.



When I''m entring url in proper syntax like "http://www.gmail.com"
I''m getting customized exception.

But if I''m entering url like "www.gmail.com"
its throwing an error :sigh:
Please reply soon if anyone have any ideas.

推荐答案

两个问题:

1.如果在文本框中输入www.gmail.com,则必须以编程方式检查URL格式并添加(http://)部分,以使输入的URL实际成为http://www.gmail.com之前调用WebRequest.Create(url);


2.我已经通过将url设置为"http://www.gmail.com"来测试了代码,并且效果很好(不抛出任何异常).您请尝试调试您的代码,并检查是否还有其他引发异常的代码.另外,您需要确保您的计算机启用了Internet连接,以便您的代码实际上能够访问Web请求.

另外,您应该修改try..catch部分,以便可以按如下方式查看确切的异常详细信息:

Two issues:

1. if you enter www.gmail.com in the text box, you have to check the URL format in programmatic way and add the (http://) portion so that, the input URL actually become http://www.gmail.com before calling WebRequest.Create(url);


2. I have tested the code with setting the url to "http://www.gmail.com" and it works quite fine (Doesn''t throw any exception). You please try to debug your code and check whether there is any other code that throws exception. Also, you need to make sure that your machine has Internet connection enable so that your code actually is able to hit a web request.

also, you should modify the try..catch portion so that, you can view the exact exception detail as follows:

try
{
..
}
catch(Exception ex)
{
    MessageBox.Show(ex.ToString());
}


这篇关于网址验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆