Click here to Skip to main content
15,891,621 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
textBox1.Text!= "@" ^ (\b(http | ftp | https):(\/\/|\\\\)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=% &:/ ~\+#]*[\w\-\@?^=%&/~\+#])?|\bwww\.[^\s])");


I want to check if the user has put a correct URL format but this does not seem to be working. I have tried other formats too but it does not seem to be checking the URL typed in.

What I have tried:

this is my code

C#
private void Button_Click(object sender, EventArgs e)
       {
           richTextBox1.Visible = true;
           richTextBox1.Clear();

            if (textBox1.TextLength == 0 || textBox1.Text!= "@" ^ (\b(http | ftp | https):(\/\/|\\\\)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=% &:/ ~\+#]*[\w\-\@?^=%&/~\+#])?|\bwww\.[^\s])");

           {
               string message2 = "Status Code";
               string caption2 = "300 Bad Request";
               MessageBoxButtons buttons2 = MessageBoxButtons.OK;
               DialogResult result2;
               result2 = MessageBox.Show(message2, caption2, buttons2);
               richTextBox1.Clear();
           }

           else
           {

                   HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(this.textBox1.Text);

                   using (HttpWebResponse myHttpWebResponse = (HttpWebResponse)myRequest.GetResponse())
                   {
                       if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
                       {
                           string message = "200 OK";
                           string caption = "Status Code";
                           MessageBoxButtons buttons = MessageBoxButtons.OK;
                           DialogResult result;

                           // Displays the MessageBox.
                           result = MessageBox.Show(message, caption, buttons);

                           Stream streamResponse = myHttpWebResponse.GetResponseStream();

                           // Get stream object
                           StreamReader streamRead = new StreamReader(streamResponse);

                           Char[] readBuffer = new Char[256];
                           // Read from buffer
                           int count = streamRead.Read(readBuffer, 0, 256);
                           while (count > 0)
                           {
                               // get string
                               String resultData = new String(readBuffer, 0, count);

                               // Write the data
                               richTextBox1.Text += (resultData);

                               // Read from buffer
                               count = streamRead.Read(readBuffer, 0, 256);

                           }// Release the response object resources.
                           streamRead.Close();
                           streamResponse.Close();
                           myHttpWebResponse.Close();
                       }
Posted
Updated 5-Oct-21 6:29am
v2
Comments
PIEBALDconsult 5-Oct-21 12:19pm    
Don't bother with a Regular Expression for that.
Have .net do it for you:
https://docs.microsoft.com/en-us/dotnet/api/system.uri.-ctor?view=net-5.0#System_Uri__ctor_System_String_

Catch and act on any exception thrown.
.net will have to check the string when you call WebRequest.Create anyway, so all you're doing is checking it twice, which is a waste of time.
candijen 5-Oct-21 12:29pm    
Yes WebRequestCreate does that but the whole program terminates, I want to stop that from happening and be able to show error message dialogues according to the error like bad request or forbidden page.
PIEBALDconsult 5-Oct-21 15:45pm    
Then catch the Exception and show a message.

1 solution

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