我正试图通过WebRequest登录到一个网站。在这一点上我得到了一个例外:
WebRequest req = WebRequest.Create(formUrl.Trim());string url,string username,string password来自文本框。这是完整的代码:
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"];
}这是文章的数据:
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请参阅此链接
Connection=keep-alive
Content-Type=application/x-www-form-urlencoded
Content-Length=49
POSTDATA=dst=&popup=true&username=13ck015373&password=F3NB发布于 2016-02-17 14:02:25
您应该传递一个有效的URL来创建一个WebRequest。错误说明URL (来自文本框)不包含方案('http://‘或'https://'),或者它是无效的。
在文本框中输入此URL (不要忘记http或https):
http://internetlogin1.cu.edu.ng或https://internetlogin1.cu.edu.ng
发布于 2016-02-17 12:43:36
如果有url-string的参数,那么您需要通过“?”添加它们?和“&”字符
发布于 2017-09-14 19:37:34
我也有一个模式uri无效的问题。必须做以下几件事才能起作用。不知道为什么..。但fyi
作品:
Uri serverUri = new Uri("http://url.com/sub/somethingService");
var webRequest = (HttpWebRequest)WebRequest.Create(serverUri);不起作用的东西(有同样的错误):
var webRequest = (HttpWebRequest)WebRequest.Create("http://url.com/sub/somethingService");https://stackoverflow.com/questions/35456625
复制相似问题