Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
1.11/5 (2 votes)
See more:
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

XML
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);
                    }
                }
            }

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;
   }

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...
Posted
Updated 16-Jul-14 3:20am
v2
Comments
CHill60 16-Jul-14 7:41am    
Post the code that you tried and explain how it didn't work
Naveen Kumar Malli 16-Jul-14 9:22am    
please watch my requirement.....

1 solution

Try this :

C#
string url=Request.URL;

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

        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900