如何检查网址是否包含Http:// [英] How Do I Check Whether An Url Consists Of Http://

查看:86
本文介绍了如何检查网址是否包含Http://的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理基于搜索引擎优化的模块的要求时,我需要验证用户是否输入前缀 Http:// 的URL,如果他输入了全部好的...但如果他没有在 Http:// 前面加上他的网址,我的代码必须将其作为前缀添加到用户输入的网址.....我还需要检查网址是否包含< b> WWW (带前缀)...如果不是我的代码必须将此 WWW 添加到用户输入的URL中....我在这方面做了很多努力,但我可以解决它......我会更广泛地欢迎任何有关实现我的要求的建议或建议......使用java脚本...... :)

这里是我的控制器代码验证所提供网址的站点地图和机器人



In dealing a requirement of Search Engine Optimization based module i drag my self in a requirement of verifying whether user enters URL with prefix of Http:// or not...if he enters it's all ok...but if he not prefix his URL with Http:// my code must add that as prefix to entered URL by user.....and also i need to check whether URL contains WWW(with prefix)...if not my code must add this WWW to entered URL by user....I had done a really hard work on this but i can't fix it....it will be more broad welcome from my side for any advice's or suggestions towards achieving my requirement ...using java script...:)
here is my controller code to verify the sitemap and robots of the provided url

public string GetAllSemData(string givenurl)
        {
            var retval = string.Empty;
            Seoproperties obj = new Seoproperties();
            StringBuilder sb = new StringBuilder();
            Dictionary<string, string> results = new Dictionary<string, string>();
            #region Sitemap.xml and robots.txt

            string[] names = new string[2] { "/sitemap.xml", "/robots.txt" };
            sb.AppendLine("<table id='tblLists' class='gridtable' width='100%'>");
            sb.AppendLine("<thead><tr>");
            sb.AppendLine("<th>SEO </th>");
            sb.AppendLine("<th>RESULTS</th>");
            sb.AppendLine("</tr></thead>");
            sb.AppendLine("<tbody>");
            foreach (var item in names)
            {
               // string addHttp = "http://";
               try
                {
                    WebRequest request = HttpWebRequest.Create("" + givenurl + "" + item + "");
                        //HttpWebRequest.Create("http://" + givenurl + "" + item + "");
                    request.Method = "HEAD"; // Just get the document headers, not the data.
                    request.Credentials = System.Net.CredentialCache.DefaultCredentials;
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            retval = (item + " exists!");
                            if (retval == "/sitemap.xml exists!")
                            {
                                //results.Add("XML Sitemap", "Great,you site has an XML Sitemap");
                                sb.AppendLine("<tr>");
                                sb.AppendLine("<td><img src='images/1382554024_ok-green.png' alt='ok' />XML Sitemap</td>");
                                sb.AppendLine("<td>Great,you site has an XML Sitemap</td>");
                                sb.AppendLine("</tr>");
                            }
                            else
                            {
                                sb.AppendLine("<tr>");
                                sb.AppendLine("<td><img src='images/1382554024_ok-green.png' alt='ok' />robots.txt</td>");
                                sb.AppendLine("<td>Great,your site has an robots.txt file</td>");
                                sb.AppendLine("</tr>");
                                // results.Add("robots.txt", "Great,your site has an robots.txt file");
                            }
                        }
                        else
                        {
                            // Some other HTTP response - probably not good.min
                            // Check its StatusCode and handle it.
                        }
                    }
                }
                catch (WebException ex)
                {
                    //HttpWebResponse webResponse = new HttpWebResponse();

                    HttpWebResponse webResponse = (HttpWebResponse)ex.Response;
                    // Determine the cause of the exception, was it 404?
                    if (webResponse.StatusCode == HttpStatusCode.NotFound)
                    {
                        MessageBox.Show(webResponse.StatusDescription);
                        retval = (item + "   does'nt exists!");
                        if (retval == "/sitemap.xml   does'nt exists!")
                        {
                            sb.AppendLine("<tr>");
                            sb.AppendLine("<td><img src='images/cross_circle.png' />XML Sitemap</td>");
                            sb.AppendLine("<td>Your Website does not have an XML Sitemap kindly create it.</td>");
                            sb.AppendLine("</tr>");
                            //results.Add("XML Sitemap", "Your Website does not have an XML Sitemap kindly create it.");
                        }
                        else
                        {
                            sb.AppendLine("<tr>");
                            sb.AppendLine("<td><img src='images/cross_circle.png' />robots.txt</td>");
                            sb.AppendLine("<td>Your Website does not have Robots.txt file kindly create it.</td>");
                            sb.AppendLine("</tr>");
                            //results.Add("robots.txt", "Your Website does not have Robots.txt file kindly create it.");
                        }
                    }
                    else
                    {
                        // Handle differently...
                        retval = (ex.Message);
                    }
                }
            }



这里是我验证网址的JavaScript代码




here is my JavaScript code to verify the url

function SubmitUrl() {

       var addHttp = 'http://';
       //if (!givenurl.match(/^[]a-zA-z]+:\/\//)) {
       //    givenurl = 'http://' + givenurl;

       //}



       var givenurl = addHttp+$("#txtUrl").val();

       $.ajax({
           url: "Analysis/Checkfilesexitsornot?givenurl=" + givenurl,
           type: 'POST',
           cache: false,
           data: '',
           success: Checkingfilesexits,
           failure: function myfunction(response) {
               alert(response.d);
           }
       });

       return false;
   }


我的JavaScript代码中的
我已经获取了一个变量并将http://添加到给定的URL变量中但我需要检查两者Http://& WWW ..检查并添加到给定的URL ...此代码的o?p将显示有关给定URL网站的信息(标题,内容,机器人,XML站点地图,表格中给定URL的网页描述)他我已经在sb(字符串构建器)中添加了表格并添加了Seo,Results .... 2ND表是MetaData,给定URL的网站结果...


in my JavaScript code i had taken a variable and added http:// to the given URL variable but i need to check both Http:// & WWW.. to check and add to the given Url...the o?p of this code will be displaying information about given URL website (title,content,robots,XML Sitemap,description of the web page of given URL in a table)he i had taken table in sb(string builder)and adding the columns of Seo,Results....2ND table is MetaData,Results of the website of given Url...

推荐答案

(#txtUrl)。val();
("#txtUrl").val();


.ajax({
url:Analysis / Checkfilesexitsornot?givenurl =+ givenurl,
类型:'POST',
缓存:false,
数据:'',
成功:Checkingfilesexits,
失败:函数myfunction(响应){
alert(response.d);
}
});

返回false;
}
.ajax({ url: "Analysis/Checkfilesexitsornot?givenurl=" + givenurl, type: 'POST', cache: false, data: '', success: Checkingfilesexits, failure: function myfunction(response) { alert(response.d); } }); return false; }


我的JavaScript代码中的
我已经获取了一个变量并将http://添加到给定的URL变量中但我需要检查两者Http://& WWW ..检查并添加到给定的URL ...此代码的o?p将显示有关给定URL网站的信息(标题,内容,机器人,XML站点地图,表格中给定URL的网页描述)他我已经在sb(字符串构建器)中添加了表格并添加了Seo,Results .... 2ND表是MetaData,给定URL的网站结果...


in my JavaScript code i had taken a variable and added http:// to the given URL variable but i need to check both Http:// & WWW.. to check and add to the given Url...the o?p of this code will be displaying information about given URL website (title,content,robots,XML Sitemap,description of the web page of given URL in a table)he i had taken table in sb(string builder)and adding the columns of Seo,Results....2ND table is MetaData,Results of the website of given Url...


试试这个:



Try this :

string url=Request.URL;

        if (!url.Contains("http://"))
        {

        }


这篇关于如何检查网址是否包含Http://的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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