简体   繁体   English

无效的 Uri : uri 方案无效

[英]Invalid Uri : The uri scheme is not valid

I am trying to login to a website via WebRequest.我正在尝试通过 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. string url,string username,string password来自文本框。 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:这是POST数据:

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

You should pass a valid URL to create a WebRequest.您应该传递一个有效的 URL 来创建 WebRequest。 The error says that URL (that comes from textbox) dose not contains scheme ('http://' or 'https://') or it is invalid.该错误表示 URL(来自文本框)不包含方案('http://' 或 'https://')或无效。

Enter this URL in text-box (don't forget http or https):在文本框中输入此 URL(不要忘记 http 或 https):

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

If there are the parameters of url-string then you need to add them through '?'如果有url-string的参数,那么你需要通过'?'添加它们。 and '&' chars和 '&' 字符

I had a schema uri invalid issue as well.我也有架构 uri 无效问题。 Had to do the following for it to work.必须执行以下操作才能使其工作。 Not sure why.. but fyi不知道为什么..但是仅供参考

Works:作品:

Uri serverUri = new Uri("http://url.com/sub/somethingService");
var webRequest = (HttpWebRequest)WebRequest.Create(serverUri);

What did not work (had this same error):什么不起作用(有同样的错误):

var webRequest = (HttpWebRequest)WebRequest.Create("http://url.com/sub/somethingService");

please add 'http://' or 'https://' to your url or check that not have ("") for example " http://YourUrl:YourPort " is not correct and should replace to http://YourUrl:YourPort .请在您的网址中添加“http://”或“https://”或检查没有 ("") 例如“ http://YourUrl:YourPort ”不正确,应替换为http://YourUrl :你的端口 you enter your url into notepad to you find problem你在记事本中输入你的网址,你会发现问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM