Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid Uri : The uri scheme is not valid

Tags:

c#

asp.net

I am trying to login to a website via WebRequest. I get an exception at this point :

WebRequest req = WebRequest.Create(formUrl.Trim());

string url,string username,string password come from a text box. This is the full code:

public void LoginToUrl(string url,string username, string password )
{
    formUrl = url;
    formParams = string.Format("username={0}&password={1}", username,password);                  

    WebRequest req = WebRequest.Create(formUrl.Trim());//
    req.ContentType = "application/x-www-form-urlencoded";
    req.Method = "POST";
      bytes = Encoding.ASCII.GetBytes(formParams);
    req.ContentLength = bytes.Length;
    using (Stream os = req.GetRequestStream())
    {
        os.Write(bytes, 0, bytes.Length);
    }
    WebResponse resp = req.GetResponse();
    cookieHeader = resp.Headers["Set-cookie"];
}

This is the POST Data:

Host=internetlogin1.cu.edu.ng

User-Agent=Mozilla/5.0 (Windows NT 10.0; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0

Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language=en-US,en;q=0.5

Accept-Encoding=gzip, deflate

Refer this link

Connection=keep-alive

Content-Type=application/x-www-form-urlencoded

Content-Length=49
POSTDATA=dst=&popup=true&username=13ck015373&password=F3NB
like image 669
simdi ejinkeonye Avatar asked Feb 17 '16 12:02

simdi ejinkeonye


2 Answers

You should pass a valid URL to create a WebRequest. The error says that URL (that comes from textbox) dose not contains scheme ('http://' or 'https://') or it is invalid.

Enter this URL in text-box (don't forget http or https):

http://internetlogin1.cu.edu.ng or https://internetlogin1.cu.edu.ng

like image 61
Talebian Avatar answered Sep 22 '22 04:09

Talebian


If there are the parameters of url-string then you need to add them through '?' and '&' chars

like image 33
Code Avatar answered Sep 25 '22 04:09

Code