Armen Sarkisian
Armen Sarkisian

Reputation: 95

Post request to another URL from ASP.NET

I want to create a page in ASP.NET which will, send request to another non ASP.NET URL, and check something there, in my case it is to check, is the domain name free, or it is already in use. (Method = POST) As a result i want to get an HTML code, from which i will extract a result, using RegEx. The last part, I can do myself :)))

Upvotes: 2

Views: 2372

Answers (2)

Armen Sarkisian
Armen Sarkisian

Reputation: 95

My final code looks like this. As you said RegEx did not work, and i used simple IndexOf.

        string domain = tb_domain.Text;
        string level = ddl_level.SelectedValue;
        string html = "";

        using (var client = new WebClient())
        {
            var values = new NameValueCollection
            {
                { "whois_search", domain },
                { "domainEnd", level  },
            };


            byte[] res = client.UploadValues("http://registration.ge/index.php?page=11&lang=geo", values);
            for (int i = 0; i < res.Length; i++)
            {
                int a = Convert.ToInt32(res[i]);
                char c = Convert.ToChar(a);
                html += Convert.ToString(c);
            }

            int ind = html.IndexOf("Registrant");

            if (ind == -1)
            {
                lbl_result.Text = "The domain is free, you can register it";
                lbl_result.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lbl_result.Text = "The Domain is used";
                lbl_result.ForeColor = System.Drawing.Color.Red;
            }
        }

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

You could use a WebClient to send a POST request and fetch the contents of a remote resource:

using (var client = new WebClient())
{
    var values = new NameValueCollection
    {
        { "param1", "value 1" },
        { "param2", "value 2" },
    };
    byte[] result = client.UploadValues("http://example.com/foo", values);
    // TODO: process the results
}

from which i will extract a result, using RegEx. The last part, I can do myself :)

I hope you are aware about the general opinion on parsing HTML with Regex. So even if you can do this yourself, don't do it. Use a HTML parser such as HTML Agility Pack to process the results.

Upvotes: 3

Related Questions